Wrap around issue- trying to change the speed.
in
Programming Questions
•
1 year ago
I am new to processing and am trying to create an image that wraps around the screen.
I have created two buttons to enable the speed of the movement to be altered.
However, only the increase speed button is working and it only works when it the mouse is held down on it. Ideally, I would like to increase the speed by a small amount each time the button was clicked. And similarly, decrease the speed with each click.
Any help would be very much appreciated thank you!
Here is my code:
- PImage panorama, image2;
- int dimension, loc1, loc2;
- int speed;
- void setup() {
- panorama = loadImage ("Panorama.jpg");
- dimension = (panorama.width*panorama.height);
- size (panorama.width, panorama.height+200);
- image (panorama, 0, 0);
- image2 = createImage (panorama.width, panorama.height, RGB);
- }
- void draw() {
- {
- background (255);
- speed= 1;
- if (dist(panorama.width/2+100, panorama.height+50, mouseX, mouseY) < 75/2) {
- if (mousePressed) {
- speed= speed +10;
- }
- if (dist(panorama.width/2-100, panorama.height+50, mouseX, mouseY) < 75/2) {
- if (mousePressed) {
- if (speed >0) {
- speed= speed -1;
- }
- }
- }
- }
- panorama.loadPixels();
- image2.loadPixels();
- for (int y=0; y < panorama.height; y++) {
- for (int x = 0; x < panorama.width; x++) {
- //define the locations (1 pixel shift)
- loc1 = x + y*panorama.width;
- loc2 = (x-speed) +y*panorama.width;
- //if loc2 is inside the range (0-dim), go on!
- if ((loc2 > 0)&(loc2 < dimension)) {
- //copy panorama to image 2, moved over one pixel
- image2.pixels[loc2]=panorama.pixels[loc1];
- }
- //else skip this line
- else {
- }
- //end of for loop
- }
- }
- panorama.updatePixels();
- image2.updatePixels();
- panorama= image2;
- image (panorama, 0, 0);
- if (dist(panorama.width/2-100, panorama.height+50, mouseX, mouseY) < 75/2) {
- stroke(0);
- cursor(HAND);
- if (mousePressed) {
- strokeWeight(3);
- }
- }
- else {
- cursor(ARROW);
- noStroke();
- strokeWeight(1);
- }
- rectMode(CENTER);
- //draw two squares
- fill(255);
- rect(panorama.width/2-100, panorama.height+50, 75, 75);
- fill(0);
- rect(panorama.width/2-100, panorama.height+50, 50, 10);
- if (dist(panorama.width/2+100, panorama.height+50, mouseX, mouseY) < 75/2) {
- stroke(0);
- cursor(HAND);
- if (mousePressed) {
- strokeWeight(3);
- }
- }
- else {
- cursor(ARROW);
- noStroke();
- strokeWeight(1);
- }
- fill(255);
- rect(panorama.width/2+100, panorama.height+50, 75, 75);
- //plus sign
- fill(0);
- rect(panorama.width/2+100, panorama.height+50, 50, 10);
- rect(panorama.width/2+100, panorama.height+50, 10, 50);
- }
- }
1