Loading...
Logo
Processing Forum

how to move the canvas?

in General Discussion  •  Other  •  3 months ago  
In my application, I want to move the canvas automatically and continuously.
I have a thought that I can translate, background(black), and then redraw the whole thing in the PApplet.

But I don not know how to redraw  the whole thing !

I need help!

Thanks a lot

Replies(6)



hello!

Copy code
  1. float x, y;
  2. float xadd=2;

  3. void setup() {
  4.   size (600, 600);
  5. }

  6. void draw() {
  7.   background(0);
  8.   translate (x, y);
  9.   // all you want to paint comes here
  10.   stroke(255);
  11.   line (20, 20, 40, 40);
  12.   x+=xadd;
  13.   y+=.7;
  14.   if (x>width)
  15.     xadd=-1*abs(xadd);
  16. }


Thanks! It does the things what I wanted !

But here is the question:

How can I repaint the whole things that already in the PApplet no mater whether I know what are they?
See get() and image(), perhaps.
I will have a try.

Thanks!

this is PhiLhos idea
you need to hit a key then the canvas starts to move


Copy code
  1. //
  2. // pos of the moving canvas
  3. float x, y;
  4. // how the canvas moves:
  5. float xadd=2;
  6. float yadd=.77;
  7. //
  8. // has canvas started to move?
  9. boolean moveCanvas=false;
  10. // image of canvas
  11. PImage img;
  12. //
  13. void setup() {
  14.   size (600, 600);
  15.   background(0);
  16. } // func
  17. //
  18. void draw() {
  19.   if (moveCanvas) {
  20.     // move canvas 
  21.     background(0);
  22.     translate (x, y);
  23.     image(img, 0, 0);
  24.     x+=xadd;
  25.     y+=yadd;
  26.     if (x>width-100)
  27.       xadd=-1*abs(xadd);
  28.     if (y>width-100)
  29.       yadd=-1*abs(yadd);
  30.     if (x<-50)
  31.       xadd=abs(xadd);
  32.     if (y<-50)
  33.       yadd=abs(yadd);
  34.   }
  35.   else
  36.   {
  37.     // make canvas
  38.     background(0);
  39.     // all you want to paint comes here
  40.     stroke(255);
  41.     line (20, 20, 40, 40);
  42.     line (20, 220, 240, 40);
  43.     ellipse(100, 100, 90, 30);
  44.     text("hit a key to start", 40, 300);
  45.   }
  46. } // func
  47. //
  48. void keyPressed() {
  49.   moveCanvas=true;
  50.   img=get(0, 0, width, height);
  51.   x=0;
  52.   y=0;
  53. } // func
  54. //


Cool !

Your idea open my mind a lot !

Thanks !