Advertisement
Guest User

Processing Snowflake

a guest
Oct 31st, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1.  
  2. // Snowflakes: http://solemone.de/demos/snow-effect-processing/
  3. // Hue slider: http://cs1335-documentation.readthedocs.io/en/latest/hsbSlider.html
  4.  
  5. void setup() {
  6.   size(1000, 600, P3D);
  7.   smooth();
  8.   surface.setResizable(true);
  9.   colorMode(HSB);
  10.   stroke(0,0,0);
  11.  
  12.  
  13.   // Snowflakes setup
  14.   for(int i = 0; i < quantity; i++) {
  15.     flakeSize1[i] = round(random(minFlakeSize, maxFlakeSize));
  16.     xPosition[i] = random(0, width);
  17.     yPosition[i] = random(0, height);
  18.     direction[i] = round(random(0, 1));
  19.   }
  20.  
  21. }
  22.  
  23. int z = 0;
  24. // Snowflake variables
  25. int quantity = 300;
  26. float [] xPosition = new float[quantity];
  27. float [] yPosition = new float[quantity];
  28. int [] flakeSize1 = new int[quantity];
  29. int [] direction = new int[quantity];
  30. int minFlakeSize = 1;
  31. int maxFlakeSize = 5;
  32.  
  33.  
  34. // Snowflake variables
  35. float flakeSize = 30;
  36. float difference = 50;
  37. float layerTwo = 50;
  38. float layerThree = layerTwo + difference;
  39. float layerFour = layerTwo + (difference * 2);
  40.  
  41. // Slider variables
  42. float marginLeft = 20;
  43. float handle1 = marginLeft;
  44. float handle2 = marginLeft;
  45. float handle3 = marginLeft;
  46. float barWidth = 255;
  47. float barHeight = 6;
  48. float handleSize = 20;
  49.  
  50.  
  51. // Hue bar variables
  52. float _barWidth = 255.0; //slider-bar width;
  53. float _hueVal = _barWidth/2; //initial hueValue global value
  54.  
  55.  
  56. void draw() {
  57.   z++;
  58.   background(0);
  59.  
  60.   for(int i = 0; i < xPosition.length; i++) {
  61.     fill(random(0, 255), random(0, 255), random(0, 255));
  62.  
  63.     ellipse(xPosition[i], yPosition[i], flakeSize1[i], flakeSize1[i]);
  64.    
  65.     if(direction[i] == 0) {
  66.       xPosition[i] += map(flakeSize1[i], minFlakeSize, maxFlakeSize, .1, .5);
  67.     } else {
  68.       xPosition[i] -= map(flakeSize1[i], minFlakeSize, maxFlakeSize, .1, .5);
  69.     }
  70.    
  71.     yPosition[i] += flakeSize1[i] + direction[i];
  72.    
  73.     if(xPosition[i] > width + flakeSize1[i] || xPosition[i] < -flakeSize1[i] || yPosition[i] > height + flakeSize1[i]) {
  74.       xPosition[i] = random(0, width);
  75.       yPosition[i] = -flakeSize1[i];
  76.     }
  77.    
  78.   }
  79.  
  80.  
  81.   fill(255);
  82.   stroke(4);
  83.   rectMode(CORNER);
  84.  
  85.   // Scrollbars
  86.   rect(marginLeft, 20, barWidth, barHeight);
  87.   translate(0, 0, 2);
  88.   ellipse(handle1, 25, handleSize, handleSize);
  89.  
  90.   rect(marginLeft, 60, barWidth, barHeight);
  91.   translate(0, 0, 2);
  92.   ellipse(handle2, 65, handleSize, handleSize);
  93.  
  94.   // Hue bar
  95.   _hueVal= drawSlider(marginLeft, 100.0,_barWidth,barHeight,_hueVal);
  96.  
  97.  
  98.   // Snowflake
  99.   translate(width / 2, height / 2, -1);
  100.   rotate(radians(width - mouseX) * 0.5);
  101.  
  102.   // Middle
  103.   rectMode(CENTER);
  104.   stroke(0, 100);
  105.   fill(_hueVal,255,255);  //use the new _hueValue to set fill for drawing canvas object
  106.   ellipse(0, 0, flakeSize, flakeSize);
  107.  
  108.   // Spokes
  109.   for (int i = 0; i < 8; i++) {
  110.     rotate(radians(45));
  111.     ellipse(layerTwo, layerTwo, flakeSize, flakeSize);
  112.     rect(layerThree, layerThree, flakeSize, flakeSize);
  113.     ellipse(layerFour, layerFour, flakeSize, flakeSize);
  114.   }
  115. }
  116.  
  117.  
  118. void mouseDragged() {
  119.   if (mouseY > 10 && mouseY < 40 && mouseX > marginLeft && mouseX < (marginLeft + barWidth)) {
  120.     handle1 = mouseX;
  121.     flakeSize = mouseX;
  122.   }
  123.   if (mouseY > 50 && mouseY < 70 && mouseX > marginLeft && mouseX < (marginLeft + barWidth)) {
  124.     handle2 = mouseX;
  125.    
  126.     layerTwo = mouseX / 2;
  127.     layerThree = layerTwo + difference;
  128.     layerFour = layerTwo + (difference * 2);
  129.   }
  130.  
  131.   if (mouseY > 50 && mouseY < 130 && mouseX > marginLeft && mouseX < (marginLeft + barWidth)) {
  132.     handle3 = mouseX - 20;
  133.     handle3 = mouseX;
  134.   }
  135. }
  136.  
  137.  
  138. float drawSlider(float xPos, float yPos, float sWidth, float sHeight,float hueVal){
  139.   fill(255);
  140.   noStroke();
  141.  
  142.   float sliderPos = map(hueVal, 0.0,255.0, 0.0, sWidth); //find the current sliderPosition from hueVal
  143.  
  144.   for(int i = 0; i < sWidth; i++){  //draw 1 line for each hueValue from 0-255
  145.       float hueValue = map(i,0.0,sWidth,0.0,255.0);  //get hueVal for each i position //local variable
  146.       stroke(hueValue,255,255);
  147.       line(xPos+i,yPos,xPos+i,yPos+sHeight);
  148.   }
  149.   if(mousePressed && mouseX>xPos && mouseX<(xPos+sWidth) && mouseY>yPos-10 && mouseY <yPos+sHeight+10){
  150.      sliderPos=mouseX-xPos;
  151.      hueVal=map(sliderPos,0.0,sWidth,0.0,255.0); // get new hueVal based on moved slider
  152.   }
  153.   stroke(100);
  154.   fill(hueVal,255,255); //either new or old hueVal
  155.   translate(0, 0, 2);
  156.   ellipse(sliderPos + xPos-3, yPos + 3, handleSize, handleSize); //this is our slider indicator that moves
  157.  
  158.   return hueVal;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement