/
Vector.js
60 lines (48 loc) · 1.32 KB
/
Vector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function Vector(options) {
Line.call(this, options);
}
Vector.prototype = {
__proto__: Line.prototype,
arrowWidth: 10,
arrowHeight: 15,
showPoint: false,
get x2() { return this.x; },
get y2() { return this.y; },
fromNode: function(node) {
var options = this.getBaseOptions(node);
this.mixin(options, {
scale: this.getFloat("scale", node),
x1: this.getFloat("offsetx", node),
y1: this.getFloat("offsety", node),
offset: this.getNode("offset", node),
copy: this.getNode("copy", node),
});
var p = new Vector(options);
this.resolveStyles(node, options);
// p.x2 += p.x1;
// p.y2 += p.y1;
p = Line.prototype.fromNode(node, p);
return p;
},
draw: function(ctx, graph) {
console.log("draw vector");
Line.prototype.draw.call(this, ctx, graph);
var p2 = graph.toScreenCoords({ x: this.x*this.scale, y: this.y*this.scale });
// Draw an arrow on the end of the vector
ctx.save();
this.setupDrawing(ctx);
ctx.translate(p2.x, p2.y);
ctx.rotate(-1*this.angle);
if (this.x2*this.scale < 0) {
ctx.rotate(Math.PI);
}
ctx.beginPath();
ctx.fillStyle = this.stroke || this.color;
ctx.moveTo(-this.arrowHeight, -this.arrowWidth/2);
ctx.lineTo(0, 0);
ctx.lineTo(-this.arrowHeight, this.arrowWidth/2);
ctx.fill();
ctx.restore();
},
}
graph.prototype.register("VECTOR", Vector);