2 Copyright (c) 2013 Suffick at Codepen (http://codepen.io/suffick) and GitHub (https://github.com/suffick)
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in
10 all copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 var physics_accuracy
= 3,
33 window
.requestAnimFrame
=
34 window
.requestAnimationFrame
||
35 window
.webkitRequestAnimationFrame
||
36 window
.mozRequestAnimationFrame
||
37 window
.oRequestAnimationFrame
||
38 window
.msRequestAnimationFrame
||
40 window
.setTimeout(callback
, 1000 / 60);
57 var Point = function (x
, y
) {
68 this.constraints
= [];
71 Point
.prototype.update = function (delta
) {
75 var diff_x
= this.x
- mouse
.x
,
76 diff_y
= this.y
- mouse
.y
,
77 dist
= Math
.sqrt(diff_x
* diff_x
+ diff_y
* diff_y
);
79 if (mouse
.button
== 1) {
81 if (dist
< mouse_influence
) {
82 this.px
= this.x
- (mouse
.x
- mouse
.px
) * 1.8;
83 this.py
= this.y
- (mouse
.y
- mouse
.py
) * 1.8;
86 } else if (dist
< mouse_cut
) this.constraints
= [];
89 this.add_force(0, gravity
);
92 nx
= this.x
+ ((this.x
- this.px
) * .99) + ((this.vx
/ 2) * delta
);
93 ny
= this.y
+ ((this.y
- this.py
) * .99) + ((this.vy
/ 2) * delta
);
101 this.vy
= this.vx
= 0
104 Point
.prototype.draw = function () {
106 if (this.constraints
.length
<= 0) return;
108 var i
= this.constraints
.length
;
109 while (i
--) this.constraints
[i
].draw();
112 Point
.prototype.resolve_constraints = function () {
114 if (this.pin_x
!= null && this.pin_y
!= null) {
121 var i
= this.constraints
.length
;
122 while (i
--) this.constraints
[i
].resolve();
124 if (this.x
> boundsx
) {
126 this.x
= 2 * boundsx
- this.x
;
128 } else if (this.x
< 1) {
133 if (this.y
> boundsy
) {
135 this.y
= 2 * boundsy
- this.y
;
137 } else if (this.y
< 1) {
143 Point
.prototype.attach = function (point
) {
145 this.constraints
.push(
146 new Constraint(this, point
)
150 Point
.prototype.remove_constraint = function (lnk
) {
152 var i
= this.constraints
.length
;
154 if (this.constraints
[i
] == lnk
) this.constraints
.splice(i
, 1);
157 Point
.prototype.add_force = function (x
, y
) {
163 Point
.prototype.pin = function (pinx
, piny
) {
168 var Constraint = function (p1
, p2
) {
172 this.length
= spacing
;
175 Constraint
.prototype.resolve = function () {
177 var diff_x
= this.p1
.x
- this.p2
.x
,
178 diff_y
= this.p1
.y
- this.p2
.y
,
179 dist
= Math
.sqrt(diff_x
* diff_x
+ diff_y
* diff_y
),
180 diff
= (this.length
- dist
) / dist
;
182 if (dist
> tear_distance
) this.p1
.remove_constraint(this);
184 var px
= diff_x
* diff
* 0.5;
185 var py
= diff_y
* diff
* 0.5;
193 Constraint
.prototype.draw = function () {
195 ctx
.moveTo(this.p1
.x
, this.p1
.y
);
196 ctx
.lineTo(this.p2
.x
, this.p2
.y
);
199 var Cloth = function () {
203 var start_x
= canvas
.width
/ 2 - cloth_width
* spacing
/ 2;
205 for (var y
= 0; y
<= cloth_height
; y
++) {
207 for (var x
= 0; x
<= cloth_width
; x
++) {
209 var p
= new Point(start_x
+ x
* spacing
, start_y
+ y
* spacing
);
211 x
!= 0 && p
.attach(this.points
[this.points
.length
- 1]);
212 y
== 0 && p
.pin(p
.x
, p
.y
);
213 y
!= 0 && p
.attach(this.points
[x
+ (y
- 1) * (cloth_width
+ 1)])
220 Cloth
.prototype.update = function () {
222 var i
= physics_accuracy
;
225 var p
= this.points
.length
;
226 while (p
--) this.points
[p
].resolve_constraints();
229 i
= this.points
.length
;
230 while (i
--) this.points
[i
].update(.016);
233 Cloth
.prototype.draw = function () {
237 var i
= cloth
.points
.length
;
238 while (i
--) cloth
.points
[i
].draw();
245 ctx
.clearRect(0, 0, canvas
.width
, canvas
.height
);
250 requestAnimFrame(update
);
255 canvas
.onmousedown = function (e
) {
256 mouse
.button
= e
.which
;
259 var rect
= canvas
.getBoundingClientRect();
260 mouse
.x
= e
.clientX
- rect
.left
,
261 mouse
.y
= e
.clientY
- rect
.top
,
266 canvas
.onmouseup = function (e
) {
271 canvas
.onmousemove = function (e
) {
274 var rect
= canvas
.getBoundingClientRect();
275 mouse
.x
= e
.clientX
- rect
.left
,
276 mouse
.y
= e
.clientY
- rect
.top
,
280 canvas
.oncontextmenu = function (e
) {
284 boundsx
= canvas
.width
- 1;
285 boundsy
= canvas
.height
- 1;
287 ctx
.strokeStyle
= '#888';
292 window
.onload = function () {
294 canvas
= document
.getElementById('canvas');
295 ctx
= canvas
.getContext('2d');