-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d.html
220 lines (212 loc) · 5.58 KB
/
2d.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html lang = 'en-US'>
<head>
<meta charset = 'utf-8' name = 'viewport' content = 'width = device-width, initial-scale = 1'>
<meta http-equiv = 'X-UA-Compatible' content = 'ie = edge'>
<title>cloth simulation</title>
<style>
body {
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id = 'canvas' style = 'background-color: #000;'></canvas>
<script>
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(x) {
if(x instanceof Vector) {
return new Vector(this.x + x.x, this.y + x.y);
}
return new Vector(this.x + x, this.y + x);
}
sub(x) {
if(x instanceof Vector) {
return new Vector(this.x - x.x, this.y - x.y);
}
return new Vector(this.x - x, this.y - x);
}
mul(x) {
if(x instanceof Vector) {
return new Vector(this.x * x.x, this.y * x.y);
}
return new Vector(this.x * x, this.y * x);
}
div(x) {
if(x instanceof Vector) {
return new Vector(this.x / x.x, this.y / x.y);
}
return new Vector(this.x / x, this.y / x);
}
set(v) {
this.x = v.x;
this.y = v.y;
}
norm() {
var l = this.length();
if(l == 0) {
return new Vector(0, 0);
}
return this.div(l);
}
length() {
return Math.hypot(this.x, this.y);
}
}
class Point extends Vector {
constructor(x, y, vx, vy, locked = false) {
super(x, y);
this.prev_x = x - vx * dt;
this.prev_y = y - vy * dt;
this.locked = locked;
}
update(dt) {
if(this.locked) {
return;
}
var x = this.x;
var y = this.y;
this.x += this.x - this.prev_x + gx * dt * dt;
this.y += this.y - this.prev_y + gy * dt * dt;
this.prev_x = x;
this.prev_y = y;
}
}
class Joint {
constructor(pointA, pointB) {
this.pointA = pointA;
this.pointB = pointB;
this.length = pointA.sub(pointB).length();
}
update(dt) {
var center = this.pointA.add(this.pointB).div(2);
var dir = this.pointA.sub(this.pointB).norm();
if(!this.pointA.locked) {
this.pointA.set(center.add(dir.mul(this.length / 2)));
}
if(!this.pointB.locked) {
this.pointB.set(center.sub(dir.mul(this.length / 2)));
}
}
render() {
ctx.lineWidth = 3;
ctx.strokeStyle = 'grey';
ctx.lineJoin = 'mitter';
ctx.beginPath();
ctx.moveTo(this.pointA.x, this.pointA.y);
ctx.lineTo(this.pointB.x, this.pointB.y);
ctx.stroke();
}
}
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
}
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
resize();
window.onresize = resize;
var gx = 0;
var gy = 980.665;
var num_points_x = 32;
var num_points_y = 32;
var physics_iterations = 1;
var joint_iterations = 16;
var dt = 1 / (60 * physics_iterations);
var points = [];
var joints = [];
var min = Math.min(width, height);
var max = Math.max(width, height);
if(max == width) {
var start_x = (width - height + 200) / 2;
var end_x = (width + height - 200) / 2;
var start_y = 100;
var end_y = height - 100;
} else {
var start_x = 100;
var end_x = width - 100;
var start_y = (height - width + 200) / 2;
var end_y = (height + width - 200) / 2;
}
for(var i = 0;i < num_points_x;i++) {
var norm_i = i / (num_points_x - 1);
var x = (end_x - start_x) * norm_i + start_x;
for(var j = 0;j < num_points_y;j++) {
var norm_j = j / (num_points_y - 1);
var y = (end_y - start_y) * norm_j + start_y;
points.push(new Point(x, y, 0, 0, norm_j == 0 && (norm_i == 0 || norm_i == 1)));
}
}
for(var i = 0;i < points.length;i++) {
if(i % num_points_y != num_points_y - 1) {
joints.push(new Joint(points[i], points[i + 1]));
}
if(points[i + num_points_y] != undefined) {
joints.push(new Joint(points[i], points[i + num_points_y]));
}
}
function render() {
ctx.clearRect(0, 0, width, height);
for(var joint of joints) {
joint.render();
}
for(var i = 0;i < physics_iterations;i++) {
for(var point of points) {
point.update(dt);
}
for(var j = 0;j < joint_iterations;j++) {
for(var joint of joints) {
joint.update(dt);
}
}
}
requestAnimationFrame(render);
}
requestAnimationFrame(render);
var passive = false;
try {
window.addEventListener('event-listener', null, Object.defineProperty({}, 'passive', {get: () => passive = {passive: true}}));
} catch(error) {}
var point_dragged = null;
var point_locked = false;
var mouse_moved = false;
window.addEventListener('mousedown', function(event) {
for(var point of points) {
var dx = point.x - event.clientX;
var dy = point.y - event.clientY;
if(dx * dx + dy * dy <= 100) {
point_dragged = point;
point_locked = point_dragged.locked;
point_dragged.locked = true;
break;
}
}
}, passive);
window.addEventListener('mouseup', function(event) {
if(point_dragged != null) {
if(mouse_moved) {
point_dragged.locked = point_locked;
} else {
point_dragged.locked = !point_locked;
}
point_dragged = null;
}
mouse_moved = false;
}, passive);
window.addEventListener('mousemove', function(event) {
if(point_dragged != null) {
mouse_moved = true;
point_dragged.x = event.clientX;
point_dragged.y = event.clientY;
}
}, passive);
</script>
</body>
</html>