UNPKG

1.41 kBJavaScriptView Raw
1var Class = require('../core/class');
2var Path = require('../core/path');
3var Transform = require('../core/transform');
4
5module.exports = Class(Path, {
6
7 onReset: function(){
8 this.points = [];
9 this.left = this.top = Infinity;
10 this.right = this.bottom = -Infinity;
11 this.width = this.height = 0;
12 this.length = 0;
13 },
14
15 onMove: function(sx, sy, ex, ey){
16 this.points.push(this.length, ex, ey);
17 },
18
19 onLine: function(sx, sy, ex, ey){
20 var x = ex - sx, y = ey - sy;
21 this.points.push((this.length += Math.sqrt(x * x + y * y)), ex, ey);
22 this.left = Math.min(this.left, sx, x);
23 this.right = Math.max(this.right, sx, x);
24 this.top = Math.min(this.top, sy, y);
25 this.bottom = Math.max(this.bottom, sy, y);
26 this.width = this.right - this.left;
27 this.height = this.bottom - this.top;
28 },
29
30 point: function(lengthToPoint){
31 // TODO: Binary search, store last distance-and-index to make second look ups faster
32 var points = this.points, last = points.length - 3, next;
33 if (last < 3) return null;
34 for (var i = 3; i < last; i+=3)
35 if (points[i] >= lengthToPoint)
36 break;
37
38 var l = points[i],
39 x = points[i+1], y = points[i+2],
40
41 dl = l - points[i-3],
42 dx = x - points[i-2], dy = y - points[i-1];
43
44 var offset = (l - lengthToPoint) / dl,
45 cos = dx / dl, sin = dy / dl;
46 x -= dx * offset; y -= dy * offset;
47 return new Transform(cos, sin, -sin, cos, x, y);
48 }
49
50});
\No newline at end of file