Advertisement
wavenezia

main-dot-js

Oct 15th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. const windowWidth = 1280;
  2. const windowHeight = 720;
  3.  
  4. function pentatonic(n) {
  5. const f0 = 110;
  6. return f0 * Math.pow(2, n / 12);
  7. }
  8.  
  9. class ShapeType {
  10. constructor(type) {
  11. this.type = type;
  12. }
  13. }
  14.  
  15. class Shape {
  16. constructor(x, y) {
  17. this.x = x;
  18. this.y = y;
  19. }
  20.  
  21. draw(shapeType, radius = 10) {
  22. switch (shapeType.type) {
  23. case "point":
  24. fill("#ffffffae");
  25. stroke("#ffffffae");
  26. ellipse(this.x, this.y, radius, radius);
  27. break;
  28. }
  29. }
  30. }
  31.  
  32. class Point extends Shape {
  33. constructor(x, y) {
  34. super(x, y);
  35. this.x = x;
  36. this.y = y;
  37. }
  38. }
  39.  
  40. class Oscillator extends Point {
  41. constructor(x, y, radius, oscillatingFreq, noteFreq) {
  42. super(x, y, radius);
  43. this.x = x;
  44. this.y = y;
  45. this.r = radius;
  46. this.frequency = oscillatingFreq;
  47. this.noteFrequency = noteFreq;
  48. }
  49.  
  50. angle = -1;
  51. direction = 1;
  52. frequency = 0.1;
  53. p5Oscillator = new p5.Oscillator("triangle");
  54.  
  55. play() {
  56. this.p5Oscillator.freq(this.noteFrequency);
  57. this.p5Oscillator.amp(0.5, 0.05);
  58. this.p5Oscillator.start();
  59. setTimeout(() => this.p5Oscillator.amp(0, 0.05), 10);
  60. }
  61. }
  62.  
  63. const origin = new Point(windowWidth / 2, windowHeight);
  64. let oscillators = [];
  65. let start = false;
  66.  
  67. function setup() {
  68. createCanvas(windowWidth, windowHeight);
  69.  
  70. const oscillatorCount = 25;
  71. const pathSpacing = 10;
  72.  
  73. for (let i = 0; i < oscillatorCount; i++) {
  74. const radius = 100 + pathSpacing * i;
  75. oscillators.push(
  76. new Oscillator(
  77. origin.x - radius * Math.cos(0),
  78. origin.y - radius * Math.sin(0),
  79. radius,
  80. (oscillatorCount - i / 2) / pathSpacing,
  81. pentatonic(oscillatorCount - i)
  82. )
  83. );
  84. }
  85.  
  86. button = createButton("Start Your Drawing");
  87. button.mousePressed(() => (start = true));
  88. }
  89.  
  90. function draw() {
  91. background(40);
  92. fill(255);
  93.  
  94. if (!start) {
  95. return;
  96. }
  97.  
  98. drawPaths();
  99. drawOscillators();
  100. drawFps();
  101. }
  102.  
  103. function drawFps() {
  104. let fps = frameRate();
  105. fill(255);
  106. stroke(0);
  107. text("FPS: " + fps.toFixed(2), windowWidth - 100, 20);
  108. }
  109.  
  110. function drawPaths() {
  111. colorMode(HSB);
  112.  
  113. for (let i = 0; i < oscillators.length; i++) {
  114. const osc = oscillators[i];
  115. const hue = map(i, 0, oscillators.length, 300, 0);
  116.  
  117. noFill();
  118. stroke(hue, 100, 100);
  119. strokeWeight(1);
  120. ellipse(origin.x, origin.y, 2 * osc.r, 2 * osc.r);
  121. }
  122.  
  123. colorMode(RGB);
  124. }
  125.  
  126. function drawOscillators() {
  127. oscillators.forEach((oscillator) => {
  128. oscillator.x =
  129. origin.x - oscillator.r * Math.cos((oscillator.angle * PI) / 180);
  130. oscillator.y =
  131. origin.y - oscillator.r * Math.sin((oscillator.angle * PI) / 180);
  132.  
  133. if (oscillator.angle > 180) {
  134. let h1 = oscillator.angle - 180;
  135. oscillator.angle = 180 - h1;
  136. oscillator.direction = -1;
  137. oscillator.play();
  138. }
  139. if (oscillator.angle < 0) {
  140. let h2 = oscillator.angle * -1;
  141. oscillator.angle = 0 + h2;
  142. oscillator.direction = 1;
  143. oscillator.play();
  144. }
  145.  
  146. oscillator.angle += .75 * oscillator.direction * oscillator.frequency;
  147. print(oscillator.angle);
  148. });
  149.  
  150. oscillators.forEach((oscillator) => {
  151. oscillator.draw(new ShapeType("point"));
  152. });
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement