-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspring1.js
More file actions
79 lines (69 loc) · 2.24 KB
/
spring1.js
File metadata and controls
79 lines (69 loc) · 2.24 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
window.onload = function () {
const PI = Math.PI;
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = (canvas.width = window.innerWidth),
height = (canvas.height = window.innerHeight),
springPoint = {x :width / 2, y: height / 2},
springPoint2 = {x :utils.randomRange(0,width), y: utils.randomRange(0,height)},
weight = particle.create(
Math.random() * width,
Math.random() * height,
0,
0
),
k = 0.2,
springLength = 80;
weight.radius = 5;
weight.friction = 0.9;
weight.addSpring(springPoint,k,springLength);
weight.addSpring(springPoint2,k,20);
document.addEventListener("mousemove", function (event) {
springPoint.x = (event.clientX);
springPoint.y = (event.clientY);
});
render();
function render() {
context.clearRect(0, 0, width, height);
//springPoint is fixed and weight is our object, so springForce vector will try to pull weight towards springPoint
//so , to create a springForce vector with magintude as differnce between pos and direction as weigth to springPoint
//subtract weight(a) from spring point(c) to get springForce(b)
//b = c-a;
// let distance = springPoint.subtract(weight);
// distance.setLength(distance.getLength() - springLength);
// let springForce = distance.multiply(k);
// weight.accelerate(springForce);
// weight.springTo( springPoint,k, springLength);
weight.update();
//dot
context.beginPath();
context.arc(springPoint.x, springPoint.y, 1, 0, 2 * PI);
context.fill();
//dot
context.beginPath();
context.arc(springPoint2.x, springPoint2.y, 1, 0, 2 * PI);
context.fill();
//line connecting
context.beginPath();
context.moveTo(springPoint.x, springPoint.y);
context.lineTo(weight.x, weight.y);
context.stroke();
//line connecting
context.beginPath();
context.moveTo(springPoint2.x, springPoint2.y);
context.lineTo(weight.x, weight.y);
context.stroke();
//weight
context.beginPath();
context.arc(
weight.x,
weight.y,
weight.radius,
0,
2 * PI
);
context.fill();
//call render method everytime frame gets updated
requestAnimationFrame(render);
}
};