MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / Scale

Class Scale

code/draw_layout.js:82–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

80// that the circles drawn around nodes’ center points don't get cut off.
81
82class Scale {
83 constructor(layout, width, height) {
84 let xs = layout.map(node => node.x);
85 let ys = layout.map(node => node.y);
86 let minX = Math.min(...xs);
87 let minY = Math.min(...ys);
88 let maxX = Math.max(...xs);
89 let maxY = Math.max(...ys);
90
91 this.offsetX = minX; this.offsetY = minY;
92 this.scaleX = (width - 2 * nodeSize) / (maxX - minX);
93 this.scaleY = (height - 2 * nodeSize) / (maxY - minY);
94 }
95
96 // The `x` and `y` methods convert from graph coordinates into
97 // canvas coordinates.
98 x(x) {
99 return this.scaleX * (x - this.offsetX) + nodeSize;
100 }
101 y(y) {
102 return this.scaleY * (y - this.offsetY) + nodeSize;
103 }
104}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected