Advertisement
libdo

Untitled

Oct 5th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var circles = [];
  2.  
  3. function setup() {
  4. createCanvas(400, 400);
  5. var count = 0;
  6. while (count < 20) {
  7. circles.push({
  8. x: random(width),
  9. y: height,
  10. vx: 0,
  11. vy: random(4),
  12. r: random(4,10),
  13. h: random(360)
  14. });
  15. count = count + 1;
  16. }
  17. }
  18.  
  19. function mouseDragged() {
  20. circles.push({
  21. x: mouseX,
  22. y: mouseY,
  23. vx: 0,
  24. vy: -random(4),
  25. r: random(4,10),
  26. h: random(360)
  27. });
  28. }
  29.  
  30. function draw() {
  31. background(255);
  32. noStroke();
  33.  
  34. circles.forEach(paint);
  35. circles.forEach(move);
  36. circles.forEach(bounce);
  37. }
  38.  
  39. function isClickedOn(circle) {
  40. if (dist(mouseX, mouseY, circle.x, circle.y) < circle.r) {
  41. circle.r = 0;
  42. }
  43. }
  44.  
  45. function mousePressed() {
  46. circles.forEach(isClickedOn);
  47. }
  48.  
  49. function paint(circle) {
  50. colorMode(HSB);
  51. fill(circle.h, 100, 100);
  52. ellipse(circle.x, circle.y, circle.r*2, circle.r*2);
  53. }
  54.  
  55. function move(circle) {
  56. circle.x += random(-2, 2); // vibration
  57. circle.x += circle.vx; // circle.x = circle.x + circle.vx
  58. circle.y += circle.vy;
  59. }
  60.  
  61. function bounce(circle) {
  62. if (circle.x > width || circle.x < 0) {
  63. circle.vx = - circle.vx;
  64. }
  65. if (circle.y > height || circle.y < 0) {
  66. circle.vy = - circle.vy;
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement