forked from kasznar/wobbly-animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
95 lines (71 loc) · 1.7 KB
/
sketch.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
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
let mass = 2;
let length = 20;
let radius = 5;
let stiffness = 0.3;
let damping = 0.8;
let pointsX = 20;
let pointsY = 20;
let springs = [];
let points = [];
let img;
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
img = loadImage("rose.jpg");
fill(255, 126);
for (let y = 0; y < pointsY; y++) {
for (let x = 0; x < pointsX; x++) {
points.push(new Point(length * x, length * y));
}
}
for (let x = 0; x < pointsX; x++) {
for (let y = 0; y < pointsY; y++) {
if (0 < x) {
let pointA = pointAt(x, y);
let pointB = pointAt(x - 1, y);
springs.push(new Spring(pointA, pointB, length, stiffness, length, 0));
}
if (0 < y) {
let pointA = pointAt(x, y);
let pointB = pointAt(x, y - 1);
springs.push(new Spring(pointA, pointB, length, stiffness, 0, length));
}
}
}
}
function draw() {
background(0);
points.forEach(point => {
point.calculateNextPosition();
});
points.forEach(point => {
point.update();
});
drawShape();
points.forEach((point, i) => {
point.display(i);
});
}
function mousePressed() {
let mX = mouseX - windowWidth / 2;
let mY = mouseY - windowHeight / 2;
let distances = [];
let minDist;
let closestIndex;
points.forEach(point => {
distances.push(int(dist(mX, mY, point.x, point.y)));
});
minDist = Math.min(...distances);
distances.forEach((dist, index) => {
if (dist === minDist) {
closestIndex = index;
}
});
if (closestIndex !== undefined && isInsidePicture(mX, mY)) {
points[closestIndex].isAnchor = true;
}
}
function mouseReleased() {
points.forEach(point => {
point.isAnchor = false;
});
}