canvas.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  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
  17. THE SOFTWARE.
  18. */
  19. // settings
  20. var physics_accuracy = 3,
  21. mouse_influence = 20,
  22. mouse_cut = 5,
  23. gravity = 1200,
  24. cloth_height = 40,
  25. cloth_width = 76,
  26. start_y = 10,
  27. spacing = 7,
  28. tear_distance = 600;
  29. window.requestAnimFrame =
  30. window.requestAnimationFrame ||
  31. window.webkitRequestAnimationFrame ||
  32. window.mozRequestAnimationFrame ||
  33. window.oRequestAnimationFrame ||
  34. window.msRequestAnimationFrame ||
  35. function (callback) {
  36. window.setTimeout(callback, 1000 / 60);
  37. };
  38. var canvas,
  39. ctx,
  40. cloth,
  41. boundsx,
  42. boundsy,
  43. mouse = {
  44. down: false,
  45. button: 1,
  46. x: 0,
  47. y: 0,
  48. px: 0,
  49. py: 0
  50. };
  51. var Point = function (x, y) {
  52. this.x = x;
  53. this.y = y;
  54. this.px = x;
  55. this.py = y;
  56. this.vx = 0;
  57. this.vy = 0;
  58. this.pin_x = null;
  59. this.pin_y = null;
  60. this.constraints = [];
  61. };
  62. Point.prototype.update = function (delta) {
  63. if (mouse.down) {
  64. var diff_x = this.x - mouse.x,
  65. diff_y = this.y - mouse.y,
  66. dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y);
  67. if (mouse.button == 1) {
  68. if (dist < mouse_influence) {
  69. this.px = this.x - (mouse.x - mouse.px) * 1.8;
  70. this.py = this.y - (mouse.y - mouse.py) * 1.8;
  71. }
  72. } else if (dist < mouse_cut) this.constraints = [];
  73. }
  74. this.add_force(0, gravity);
  75. delta *= delta;
  76. nx = this.x + ((this.x - this.px) * .99) + ((this.vx / 2) * delta);
  77. ny = this.y + ((this.y - this.py) * .99) + ((this.vy / 2) * delta);
  78. this.px = this.x;
  79. this.py = this.y;
  80. this.x = nx;
  81. this.y = ny;
  82. this.vy = this.vx = 0
  83. };
  84. Point.prototype.draw = function () {
  85. if (this.constraints.length <= 0) return;
  86. var i = this.constraints.length;
  87. while (i--) this.constraints[i].draw();
  88. };
  89. Point.prototype.resolve_constraints = function () {
  90. if (this.pin_x != null && this.pin_y != null) {
  91. this.x = this.pin_x;
  92. this.y = this.pin_y;
  93. return;
  94. }
  95. var i = this.constraints.length;
  96. while (i--) this.constraints[i].resolve();
  97. if (this.x > boundsx) {
  98. this.x = 2 * boundsx - this.x;
  99. } else if (this.x < 1) {
  100. this.x = 2 - this.x;
  101. }
  102. if (this.y > boundsy) {
  103. this.y = 2 * boundsy - this.y;
  104. } else if (this.y < 1) {
  105. this.y = 2 - this.y;
  106. }
  107. };
  108. Point.prototype.attach = function (point) {
  109. this.constraints.push(
  110. new Constraint(this, point)
  111. );
  112. };
  113. Point.prototype.remove_constraint = function (lnk) {
  114. var i = this.constraints.length;
  115. while (i--)
  116. if (this.constraints[i] == lnk) this.constraints.splice(i, 1);
  117. };
  118. Point.prototype.add_force = function (x, y) {
  119. this.vx += x;
  120. this.vy += y;
  121. };
  122. Point.prototype.pin = function (pinx, piny) {
  123. this.pin_x = pinx;
  124. this.pin_y = piny;
  125. };
  126. var Constraint = function (p1, p2) {
  127. this.p1 = p1;
  128. this.p2 = p2;
  129. this.length = spacing;
  130. };
  131. Constraint.prototype.resolve = function () {
  132. var diff_x = this.p1.x - this.p2.x,
  133. diff_y = this.p1.y - this.p2.y,
  134. dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y),
  135. diff = (this.length - dist) / dist;
  136. if (dist > tear_distance) this.p1.remove_constraint(this);
  137. var px = diff_x * diff * 0.5;
  138. var py = diff_y * diff * 0.5;
  139. this.p1.x += px;
  140. this.p1.y += py;
  141. this.p2.x -= px;
  142. this.p2.y -= py;
  143. };
  144. Constraint.prototype.draw = function () {
  145. ctx.moveTo(this.p1.x, this.p1.y);
  146. ctx.lineTo(this.p2.x, this.p2.y);
  147. };
  148. var Cloth = function () {
  149. this.points = [];
  150. var start_x = canvas.width / 2 - cloth_width * spacing / 2;
  151. for (var y = 0; y <= cloth_height; y++) {
  152. for (var x = 0; x <= cloth_width; x++) {
  153. var p = new Point(start_x + x * spacing, start_y + y * spacing);
  154. x != 0 && p.attach(this.points[this.points.length - 1]);
  155. y == 0 && p.pin(p.x, p.y);
  156. y != 0 && p.attach(this.points[x + (y - 1) * (cloth_width + 1)])
  157. this.points.push(p);
  158. }
  159. }
  160. };
  161. Cloth.prototype.update = function () {
  162. var i = physics_accuracy;
  163. while (i--) {
  164. var p = this.points.length;
  165. while (p--) this.points[p].resolve_constraints();
  166. }
  167. i = this.points.length;
  168. while (i--) this.points[i].update(.016);
  169. };
  170. Cloth.prototype.draw = function () {
  171. ctx.beginPath();
  172. var i = cloth.points.length;
  173. while (i--) cloth.points[i].draw();
  174. ctx.stroke();
  175. };
  176. function update() {
  177. ctx.clearRect(0, 0, canvas.width, canvas.height);
  178. cloth.update();
  179. cloth.draw();
  180. requestAnimFrame(update);
  181. }
  182. function start() {
  183. canvas.onmousedown = function (e) {
  184. mouse.button = e.which;
  185. mouse.px = mouse.x;
  186. mouse.py = mouse.y;
  187. var rect = canvas.getBoundingClientRect();
  188. mouse.x = e.clientX - rect.left,
  189. mouse.y = e.clientY - rect.top,
  190. mouse.down = true;
  191. e.preventDefault();
  192. };
  193. canvas.onmouseup = function (e) {
  194. mouse.down = false;
  195. e.preventDefault();
  196. };
  197. canvas.onmousemove = function (e) {
  198. mouse.px = mouse.x;
  199. mouse.py = mouse.y;
  200. var rect = canvas.getBoundingClientRect();
  201. mouse.x = e.clientX - rect.left,
  202. mouse.y = e.clientY - rect.top,
  203. e.preventDefault();
  204. };
  205. canvas.oncontextmenu = function (e) {
  206. e.preventDefault();
  207. };
  208. boundsx = canvas.width - 1;
  209. boundsy = canvas.height - 1;
  210. ctx.strokeStyle = '#888';
  211. cloth = new Cloth();
  212. update();
  213. }
  214. window.onload = function () {
  215. canvas = document.getElementById('canvas');
  216. ctx = canvas.getContext('2d');
  217. canvas.width = 560;
  218. canvas.height = 350;
  219. start();
  220. };