Munashedov

Untitled

Jul 2nd, 2021
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let creatures = [["dog", "mammal"],
  2.                  ["cat", "mammal"],
  3.                  ["bird", "avian"],
  4.                  ["bee", "insect"],
  5.                  ["wasp", "insect"]];
  6.  
  7. let mammals = creatures.filter(c => c[1] === "mammal");
  8.  
  9. let doOnce = true;
  10.  
  11. let colors = [];
  12. let balls = [];
  13.  
  14. let hoverColor;
  15. let selectedColor;
  16.  
  17. function setup() {
  18.   createCanvas(640,480);
  19.   frameRate(144);
  20.   noStroke();
  21.   textSize(50);
  22.   console.table(creatures);
  23.   console.table(mammals)
  24.  
  25.   hoverColor = color(50,200,50);
  26.   selectedColor = color(50,50,200);
  27.  
  28.   //balls = [new Ball(100,100,100, color(255))];
  29.   for(let i = 0; i < 10; i++){
  30.     colors[i] = color(random(255), random(255), random(255));
  31.   }
  32.   background(50);
  33.   console.log(balls[0]);
  34. }
  35.  
  36. class Ball {
  37.   x = 0;
  38.   y = 0;
  39.   r = 0;
  40.   fillColor = 0;
  41.   selected = false;
  42.   ox = 0;
  43.   oy = 0;
  44.  
  45.   constructor(x,y,r,c){
  46.     this.x = x;
  47.     this.y = y;
  48.     this.r = r;
  49.     this.fillColor = c;
  50.   }
  51.  
  52.   display(){
  53.     fill(this.fillColor);
  54.     if(this.hovered){ fill(hoverColor); }
  55.     if(this.selected){ fill(selectedColor); }
  56.     circle(this.x,this.y,this.r);
  57.   }
  58.   update(){
  59.     if(this.selected && mouseIsPressed){
  60.       this.x = mouseX + this.ox;
  61.       this.y = mouseY + this.oy;
  62.     } else {
  63.       this.selected = false;
  64.     }
  65.   }
  66.   grab(mx,my){
  67.     this.ox = this.x - mx;
  68.     this.oy = this.y - my;
  69.   }
  70.   release(){
  71.     this.ox = 0;
  72.     this.oy = 0;
  73.   }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. let currentColor = 0;
  80. let x = 10;
  81. function draw() {
  82.   background(0);
  83.   if(doOnce){
  84.     console.table(mammals);
  85.     doOnce = false;
  86.   }
  87.  
  88.   checkHover();
  89.   displayBallPlacement(newBallX, newBallY);
  90.  
  91.   balls.forEach(ball => ball.update());
  92.   balls.forEach(ball => ball.display());
  93. }
  94.  
  95. var ballStarted = false;
  96. var newBallX = 0;
  97. var newBallY = 0;
  98.  
  99. function beginBall(mx, my){
  100.   ballStarted = true;
  101.   newBallX = mx;
  102.   newBallY = my;
  103. }
  104.  
  105. function displayBallPlacement(mx, my){
  106.   let minSize = 10;
  107.  
  108.   if(ballStarted){
  109.     let r = dist(mx, my, mouseX, mouseY) * 2;
  110.     if(r > minSize){ fill(color(25,100,25)); }
  111.     if(r < minSize){ fill(color(100,25,25)); }
  112.     circle(mx, my, r);
  113.     if(!mouseIsPressed){
  114.       ballStarted = false;
  115.       if(r > minSize){
  116.         balls[balls.length] = (new Ball(mx,my,r,random(100,256)));
  117.       }
  118.     }
  119.   }
  120. }
  121.  
  122. let bgColor = 50;
  123. let brushSize = 5;
  124.  
  125. function clearBG(){
  126.   background(bgColor);
  127. }
  128. function randomBG(){
  129.   bgColor = (color(random(255), random(255), random(255)));
  130.   background(bgColor);
  131. }
  132.  
  133. function checkHover(){
  134.   if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height){
  135.     balls.forEach(b => dist(mouseX, mouseY, b.x, b.y) < b.r/2 ? b.hovered = true : b.hovered = false);
  136.   }
  137. }
  138.  
  139. function mousePressed(){
  140.   if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height){
  141.     if(balls.filter(b => b.hovered).length === 0){
  142.       beginBall(mouseX, mouseY);
  143.     } else {
  144.       balls.forEach(b => b.hovered ? b.selected = true : b.selected = false);
  145.       balls.forEach(b => b.selected ? b.grab(mouseX, mouseY) : b.release());
  146.     }
  147.   }
  148. }
  149.  
Add Comment
Please, Sign In to add comment