Advertisement
GilesCartmel

ConstrainExample

Aug 17th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. /* Constrain Example (c)2015 Giles Cartmel */
  2.  
  3. int borderX, borderY;
  4.  
  5. void setup() {
  6.   size(800,600);
  7.   borderX = width/5;
  8.   borderY = height/5;
  9. }
  10.  
  11. void draw() {
  12.   background(0);
  13.  
  14.   int x1, y1, x2, y2;
  15.   x1 = borderX;
  16.   y1 = borderY;
  17.   x2 = width-borderX;
  18.   y2 = height-borderY;
  19.  
  20.   // draw a rectangle based on 10% border of the canvas
  21.   rectMode(CORNER);
  22.   fill(20);
  23.   stroke(80);
  24.   rect(x1,y1,x2-x1,y2-y1);
  25.  
  26.   // draw an ellipse at the mouse position
  27.   // constraining it's centre to within the rectangle
  28.   ellipseMode(RADIUS);
  29.   fill(128,255,128,128);
  30.   stroke(255);
  31.   ellipse(constrain(mouseX, x1, x2)
  32.          ,constrain(mouseY, y1, y2)
  33.          ,borderX/5
  34.          ,borderY/5
  35.          );
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement