-
Notifications
You must be signed in to change notification settings - Fork 3
/
joint.js
68 lines (56 loc) · 1.66 KB
/
joint.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
61
62
63
64
65
66
67
68
class Joint {
static offset = { x: 0, y: 0, z: 0};
static diameter = 8;
static mirrorH = false;
static mirrorV = false;
get x() { return this._x + Joint.offset.x + ((this._x - (canvasSize.x/2))*Joint.offset.z); }
get y() { return this._y + Joint.offset.y + ((this._y - (canvasSize.y/2))*Joint.offset.z); }
set x(value) { this._x = value; }
set y(value) { this._y = value; }
constructor(x, y, clr) {
this._x = x;
this._y = y;
this.color = color(clr)
this._z = 0;
this.dragging = false;
this.mirrorJoint = null;
this.mirrorAnchor = null;
}
update() {
if (this.dragging) {
this._x = mouseX + this.offsetX;
this._y = mouseY + this.offsetY;
if (!this.mirrorJoint) return;
if (Joint.mirrorH && Joint.mirrorV) {
this.mirrorJoint._x = this.mirrorAnchor._x-(this._x - this.mirrorAnchor._x);
this.mirrorJoint._y = this.mirrorAnchor._y-(this._y - this.mirrorAnchor._y);
}
else if (Joint.mirrorH) {
this.mirrorJoint._x = this.mirrorAnchor._x-(this._x - this.mirrorAnchor._x);
this.mirrorJoint._y = this._y;
}
else if (Joint.mirrorV) {
this.mirrorJoint._y = this.mirrorAnchor._y-(this._y - this.mirrorAnchor._y);
this.mirrorJoint._x = this._x;
}
}
}
show() {
noStroke()
fill(this.color)
circle(this.x, this.y, Joint.diameter);
}
pressed() {
if (this.isOver()) {
this.dragging = true;
this.offsetX = this._x - mouseX;
this.offsetY = this._y - mouseY;
}
}
released() {
this.dragging = false;
}
isOver() {
return dist(mouseX, mouseY, this.x, this.y) < Joint.diameter;
}
}