Repository navigation
Expand file tree
/
Copy path_Engine.Point.js
More file actions
118 lines (89 loc) · 1.9 KB
/
Copy path_Engine.Point.js
File metadata and controls
118 lines (89 loc) · 1.9 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(function(
Engine,
Vector
){ 'use strict';
Engine.Point = function(id, x, y, shapeSize){
this.id = id;
this.shapeSize = shapeSize;
this.ref = new Vector(x, y);
this.pos = new Vector(
x * shapeSize.x,
y * shapeSize.y
);
this.target = this.pos.clone();
this.pos.x = shapeSize.x / 2;
this.pos.y = shapeSize.y / 2;
this.accel = Vector.coerce(this.accel);
this.vel = Vector.coerce(this.vel);
this.stiffness = Engine.getRandomFloat(150, 600);
this.friction = Engine.getRandomFloat(12, 18);
};
Engine.Point.prototype = {
radius: 1,
stiffness : 200,
friction : 13,
threshold : 0.03,
pos: {
x: 0,
y: 0
},
accel: {
x: 0,
y: 0
},
vel : {
x: 0,
y: 0
},
target: {
x: 0,
y: 0
},
resize: function(){
this.target.x = this.pos.x = this.ref.x * this.shapeSize.x;
this.target.y = this.pos.y = this.ref.y * this.shapeSize.y;
},
updateBreathingPhysics: function(){
this.stiffness = Engine.getRandomFloat(2, 4);
this.friction = Engine.getRandomFloat(1, 2);
},
updateTarget: function(newSize){
var diff;
this.target.x = this.ref.x * newSize.x;
this.target.y = this.ref.y * newSize.y;
diff = Vector.sub(newSize, this.shapeSize).div(2);
this.target.sub(diff);
this.target.add({
x: Engine.getRandomFloat(-3, 3),
y: Engine.getRandomFloat(-3, 3)
});
},
update: function(engine){
var newAccel;
newAccel = Vector.sub(this.target, this.pos)
.mult(this.stiffness)
.sub(Vector.mult(this.vel, this.friction));
this.accel.set(newAccel);
this.vel.add(Vector.mult(this.accel, engine.tick));
this.pos.add(
Vector.mult(this.vel, engine.tick)
);
newAccel = null;
return this;
},
draw: function(ctx, scale){
ctx.beginPath();
ctx.arc(
this.pos.x * scale,
this.pos.y * scale,
this.radius * scale,
0,
Math.PI * 2,
false
);
ctx.fillStyle = '#ffffff';
ctx.fill();
return this;
}
};
})(window.Engine, window.Vector);