You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
11 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Tadpoles</title>
  6. <link rel="stylesheet" href="../css/style.css">
  7. <script type="text/javascript" src="../../dist/paper-full.js"></script>
  8. <script type="text/paperscript" canvas="canvas">
  9. // Adapted from Flocking Processing example by Daniel Schiffman:
  10. // http://processing.org/learning/topics/flocking.html
  11. var Boid = Base.extend({
  12. initialize: function(position, maxSpeed, maxForce) {
  13. var strength = Math.random() * 0.5;
  14. this.acceleration = new Point();
  15. this.vector = Point.random() * 2 - 1;
  16. this.position = position.clone();
  17. this.radius = 30;
  18. this.maxSpeed = maxSpeed + strength;
  19. this.maxForce = maxForce + strength;
  20. this.amount = strength * 10 + 10;
  21. this.count = 0;
  22. this.createItems();
  23. },
  24. run: function(boids) {
  25. this.lastLoc = this.position.clone();
  26. if (!groupTogether) {
  27. this.flock(boids);
  28. } else {
  29. this.align(boids);
  30. }
  31. this.borders();
  32. this.update();
  33. this.calculateTail();
  34. this.moveHead();
  35. },
  36. calculateTail: function() {
  37. var segments = this.path.segments,
  38. shortSegments = this.shortPath.segments;
  39. var speed = this.vector.length;
  40. var pieceLength = 5 + speed / 3;
  41. var point = this.position;
  42. segments[0].point = shortSegments[0].point = point;
  43. // Chain goes the other way than the movement
  44. var lastVector = -this.vector;
  45. for (var i = 1; i < this.amount; i++) {
  46. var vector = segments[i].point - point;
  47. this.count += speed * 10;
  48. var wave = Math.sin((this.count + i * 3) / 300);
  49. var sway = lastVector.rotate(90).normalize(wave);
  50. point += lastVector.normalize(pieceLength) + sway;
  51. segments[i].point = point;
  52. if (i < 3)
  53. shortSegments[i].point = point;
  54. lastVector = vector;
  55. }
  56. this.path.smooth();
  57. },
  58. createItems: function() {
  59. this.head = new Shape.Ellipse({
  60. center: [0, 0],
  61. size: [13, 8],
  62. fillColor: 'white'
  63. });
  64. this.path = new Path({
  65. strokeColor: 'white',
  66. strokeWidth: 2,
  67. strokeCap: 'round'
  68. });
  69. for (var i = 0; i < this.amount; i++)
  70. this.path.add(new Point());
  71. this.shortPath = new Path({
  72. strokeColor: 'white',
  73. strokeWidth: 4,
  74. strokeCap: 'round'
  75. });
  76. for (var i = 0; i < Math.min(3, this.amount); i++)
  77. this.shortPath.add(new Point());
  78. },
  79. moveHead: function() {
  80. this.head.position = this.position;
  81. this.head.rotation = this.vector.angle;
  82. },
  83. // We accumulate a new acceleration each time based on three rules
  84. flock: function(boids) {
  85. var separation = this.separate(boids) * 3;
  86. var alignment = this.align(boids);
  87. var cohesion = this.cohesion(boids);
  88. this.acceleration += separation + alignment + cohesion;
  89. },
  90. update: function() {
  91. // Update velocity
  92. this.vector += this.acceleration;
  93. // Limit speed (vector#limit?)
  94. this.vector.length = Math.min(this.maxSpeed, this.vector.length);
  95. this.position += this.vector;
  96. // Reset acceleration to 0 each cycle
  97. this.acceleration = new Point();
  98. },
  99. seek: function(target) {
  100. this.acceleration += this.steer(target, false);
  101. },
  102. arrive: function(target) {
  103. this.acceleration += this.steer(target, true);
  104. },
  105. borders: function() {
  106. var vector = new Point();
  107. var position = this.position;
  108. var radius = this.radius;
  109. var size = view.size;
  110. if (position.x < -radius) vector.x = size.width + radius;
  111. if (position.y < -radius) vector.y = size.height + radius;
  112. if (position.x > size.width + radius) vector.x = -size.width -radius;
  113. if (position.y > size.height + radius) vector.y = -size.height -radius;
  114. if (!vector.isZero()) {
  115. this.position += vector;
  116. var segments = this.path.segments;
  117. for (var i = 0; i < this.amount; i++) {
  118. segments[i].point += vector;
  119. }
  120. }
  121. },
  122. // A method that calculates a steering vector towards a target
  123. // Takes a second argument, if true, it slows down as it approaches
  124. // the target
  125. steer: function(target, slowdown) {
  126. var steer,
  127. desired = target - this.position;
  128. var distance = desired.length;
  129. // Two options for desired vector magnitude
  130. // (1 -- based on distance, 2 -- maxSpeed)
  131. if (slowdown && distance < 100) {
  132. // This damping is somewhat arbitrary:
  133. desired.length = this.maxSpeed * (distance / 100);
  134. } else {
  135. desired.length = this.maxSpeed;
  136. }
  137. steer = desired - this.vector;
  138. steer.length = Math.min(this.maxForce, steer.length);
  139. return steer;
  140. },
  141. separate: function(boids) {
  142. var desiredSeperation = 60;
  143. var steer = new Point();
  144. var count = 0;
  145. // For every boid in the system, check if it's too close
  146. for (var i = 0, l = boids.length; i < l; i++) {
  147. var other = boids[i];
  148. var vector = this.position - other.position;
  149. var distance = vector.length;
  150. if (distance > 0 && distance < desiredSeperation) {
  151. // Calculate vector pointing away from neighbor
  152. steer += vector.normalize(1 / distance);
  153. count++;
  154. }
  155. }
  156. // Average -- divide by how many
  157. if (count > 0)
  158. steer /= count;
  159. if (!steer.isZero()) {
  160. // Implement Reynolds: Steering = Desired - Velocity
  161. steer.length = this.maxSpeed;
  162. steer -= this.vector;
  163. steer.length = Math.min(steer.length, this.maxForce);
  164. }
  165. return steer;
  166. },
  167. // Alignment
  168. // For every nearby boid in the system, calculate the average velocity
  169. align: function(boids) {
  170. var neighborDist = 25;
  171. var steer = new Point();
  172. var count = 0;
  173. for (var i = 0, l = boids.length; i < l; i++) {
  174. var other = boids[i];
  175. var distance = this.position.getDistance(other.position);
  176. if (distance > 0 && distance < neighborDist) {
  177. steer += other.vector;
  178. count++;
  179. }
  180. }
  181. if (count > 0)
  182. steer /= count;
  183. if (!steer.isZero()) {
  184. // Implement Reynolds: Steering = Desired - Velocity
  185. steer.length = this.maxSpeed;
  186. steer -= this.vector;
  187. steer.length = Math.min(steer.length, this.maxForce);
  188. }
  189. return steer;
  190. },
  191. // Cohesion
  192. // For the average location (i.e. center) of all nearby boids,
  193. // calculate steering vector towards that location
  194. cohesion: function(boids) {
  195. var neighborDist = 100;
  196. var sum = new Point();
  197. var count = 0;
  198. for (var i = 0, l = boids.length; i < l; i++) {
  199. var other = boids[i];
  200. var distance = this.position.getDistance(other.position);
  201. if (distance > 0 && distance < neighborDist) {
  202. sum += other.position; // Add location
  203. count++;
  204. }
  205. }
  206. if (count > 0) {
  207. sum /= count;
  208. // Steer towards the location
  209. return this.steer(sum, false);
  210. }
  211. return sum;
  212. }
  213. });
  214. var heartPath = new Path('M514.69629,624.70313c-7.10205,-27.02441 -17.2373,-52.39453 -30.40576,-76.10059c-13.17383,-23.70703 -38.65137,-60.52246 -76.44434,-110.45801c-27.71631,-36.64355 -44.78174,-59.89355 -51.19189,-69.74414c-10.5376,-16.02979 -18.15527,-30.74951 -22.84717,-44.14893c-4.69727,-13.39893 -7.04297,-26.97021 -7.04297,-40.71289c0,-25.42432 8.47119,-46.72559 25.42383,-63.90381c16.94775,-17.17871 37.90527,-25.76758 62.87354,-25.76758c25.19287,0 47.06885,8.93262 65.62158,26.79834c13.96826,13.28662 25.30615,33.10059 34.01318,59.4375c7.55859,-25.88037 18.20898,-45.57666 31.95215,-59.09424c19.00879,-18.32178 40.99707,-27.48535 65.96484,-27.48535c24.7373,0 45.69531,8.53564 62.87305,25.5957c17.17871,17.06592 25.76855,37.39551 25.76855,60.98389c0,20.61377 -5.04102,42.08691 -15.11719,64.41895c-10.08203,22.33203 -29.54687,51.59521 -58.40723,87.78271c-37.56738,47.41211 -64.93457,86.35352 -82.11328,116.8125c-13.51758,24.0498 -23.82422,49.24902 -30.9209,75.58594z');
  215. var boids = [];
  216. var groupTogether = false;
  217. // Add the boids:
  218. for (var i = 0; i < 30; i++) {
  219. var position = Point.random() * view.size;
  220. boids.push(new Boid(position, 10, 0.05));
  221. }
  222. function onFrame(event) {
  223. for (var i = 0, l = boids.length; i < l; i++) {
  224. if (groupTogether) {
  225. var length = ((i + event.count / 30) % l) / l * heartPath.length;
  226. var point = heartPath.getPointAt(length);
  227. if (point)
  228. boids[i].arrive(point);
  229. }
  230. boids[i].run(boids);
  231. }
  232. }
  233. // Reposition the heart path whenever the window is resized:
  234. function onResize(event) {
  235. heartPath.fitBounds(view.bounds);
  236. heartPath.scale(0.8);
  237. }
  238. function onMouseDown(event) {
  239. groupTogether = !groupTogether;
  240. }
  241. function onKeyDown(event) {
  242. if (event.key == 'space') {
  243. var layer = project.activeLayer;
  244. layer.selected = !layer.selected;
  245. return false;
  246. }
  247. }
  248. </script>
  249. <style>
  250. body {
  251. background: black;
  252. }
  253. </style>
  254. </head>
  255. <body>
  256. <canvas id="canvas" resize></canvas>
  257. </body>
  258. </html>