Advertisement
leeloptimist

Bouncing Ball v0.3

Mar 1st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. autowatch = 1;
  2. outlets = 2;
  3. var m = mgraphics;
  4. m.init();
  5. m.relative_coords = 0;
  6. m.autofill = 0;
  7. m.redraw();
  8.  
  9. var js_width = box.rect[2] - box.rect[0];
  10. var js_height = box.rect[3] - box.rect[1];
  11. var voice_max = 32;
  12. var voice_current = 1;
  13. var x = [];
  14. var y = [];
  15. var x_inc = [];
  16. var y_inc = [];
  17. var width = [];
  18. var rate = 6;
  19. var R = [];
  20. var G = [];
  21. var B = [];
  22.  
  23. for (i = 0; i < voice_max; i++){
  24. x[i] = Math.random();
  25. y[i] = Math.random();
  26. x_inc[i] = Math.random() / js_width * rate;
  27. y_inc[i] = Math.random() / js_height * rate;
  28. R[i] = Math.random();
  29. G[i] = Math.random();
  30. B[i] = Math.random();
  31. if (x[i] > y[i]){
  32. width[i] = Math.random() * (1 - x[i]);
  33. }
  34. else if (x[i] < y[i]){
  35. width[i] = Math.random() * (1 - y[i]);
  36. }
  37. }
  38.  
  39. function random(){
  40. for (i = 0; i < voice_max; i++){
  41. x[i] = Math.random();
  42. y[i] = Math.random();
  43. x_inc[i] = Math.random() / js_width * rate;
  44. y_inc[i] = Math.random() / js_height * rate;
  45. R[i] = Math.random();
  46. G[i] = Math.random();
  47. B[i] = Math.random();
  48. if (x[i] > y[i]){
  49. width[i] = Math.random() * (1 - x[i]);
  50. }
  51. else if (x[i] < y[i]){
  52. width[i] = Math.random() * (1 - y[i]);
  53. }
  54. }
  55. m.redraw();
  56. }
  57.  
  58. function bang(){
  59. for (i = 0; i < voice_max; i++){
  60. x[i] += x_inc[i];
  61. y[i] += y_inc[i];
  62. if (x[i] < 0 || x[i] > 1 - width[i]){
  63. x_inc[i] *= -1;
  64. }
  65. if (y[i] < 0 || y[i] > 1- width[i]){
  66. y_inc[i] *= -1;
  67. }
  68. }
  69. for (i = 0; i < voice_current; i++){
  70. outlet(0, i, x[i], y[i]);
  71. if (x[i] < 0 || x[i] > 1 - width[i] || y[i] < 0 || y[i] > 1- width[i]){
  72. outlet(1, 'bang');
  73. }
  74. }
  75. m.redraw();
  76. }
  77.  
  78. function voice(n){
  79. voice_current = n % 64;
  80. m.redraw();
  81. }
  82.  
  83. function paint(){
  84. for (i = 0; i < voice_current; i++){
  85. m.set_source_rgba(R[i], G[i], B[i], 0.3); //ellipse fill
  86. m.ellipse(x[i] * js_width, y[i] * js_height, width[i] * js_width, width[i] * js_height);
  87. m.fill();
  88. m.set_source_rgba(1, 1, 1, 0.5); //ellipse stroke
  89. m.ellipse(x[i] * js_width, y[i] * js_height, width[i] * js_width, width[i] * js_height);
  90. m.stroke();
  91. m.set_source_rgba(1, 1, 1, 0.5); //up line
  92. m.move_to(js_width / 2, 0);
  93. m.line_to((x[i] + width[i] / 2) * js_width, y[i] * js_height);
  94. m.stroke();
  95. m.move_to(js_width / 2, js_height); // down line
  96. m.line_to((x[i] + width[i] / 2) * js_width, (y[i] + width[i]) * js_height);
  97. m.stroke();
  98. }
  99. }
  100.  
  101. function onresize(x, y){
  102. js_width = x;
  103. js_height = y;
  104. m.redraw();
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement