【问题标题】:Moving image with the cursor使用光标移动图像
【发布时间】:2013-04-30 23:21:21
【问题描述】:

所以,我正在做一个处理项目,让我可以将图像用作光标,但我一直遇到光标图像问题,因为它一直在闪烁。我读到如果光标图像太大,它会不断闪烁。但是,我想知道是否有任何方法可以保持图像的大小,同时仍将其保持为光标。或者,

我想知道是否有代码可以让我按下图像并将其拖到屏幕上。 :/

这是我一直在使用的代码。

// Declaring a variable of type PImage
PImage img;  
PImage img2;

void setup() {
  size(815,514);
  // Make a new instance of a PImage by loading an image file
  img = loadImage("preamble.jpg");
  img2 = loadImage("blackgun.png");
}

void draw() {
  background(0);
  // Draw the image to the screen at coordinate (0,0)
  image(img,0,0);
  //using the image as the cursor
if (mouseX < 50) {
    cursor(img2);
  } else {
    cursor(img2);
  }
}

【问题讨论】:

    标签: image cursor draggable processing motion


    【解决方案1】:

    在reference 中,他们说:“建议将用作光标的图像设置为 16x16 或 32x32 像素”。你可以通过调用 resize 来做到这一点:

    img2 = loadImage("blackgun.png");
    img2.resize(32,32);
    

    这些行也没有任何意义:

    if (mouseX < 50) {
        cursor(img2);
      } else {
        cursor(img2);
      }
    

    无论哪种方式,您最终都会得到与光标图像相同的 img2。

    你可以使用:

    image(img, mouseX, mouseY);
    

    但光标会在图像上。

    这是一个简单而可怜的阻力...... 我在这里有一个更好的拖放的旧示例,它使用 rects() 而不是图像,但想法是相同的,您可以轻松地将其调整为使用图像:

    DragMe[] drags = new DragMe[40];
    
    void setup() {
      size(400, 400);
      for (int i = 0; i < drags.length; i++) {
        drags[i]  = new DragMe();
      }
    }
    
    void draw() {
      background(255);
      for (int i = 0; i < drags.length; i++) {
        drags[i].display();
        drags[i].update();
      }
    }
    
    
    
    void mousePressed() {
      for (int i = 0; i < drags.length; i++) {
        if (!drags[i].isOver())
          drags[i].dontMove = true;
        drags[i].offset_x = mouseX - drags[i].pos_x;
        drags[i].offset_y = mouseY - drags[i].pos_y;
      }
    }
    
    
    void mouseReleased() {
      for (int i = 0; i < drags.length; i++) {
        drags[i].locked = false;
        drags[i].dontMove = false;
      }
    }
    
    
    
    
    
    
    class DragMe {
      float pos_x, pos_y, SIZE = 20;
      float prev_x, prev_y;
      boolean locked;
      boolean dontMove;
      color c = color (0, 170, 170);
      float offset_x, offset_y;
    
      DragMe() {
        pos_x = random(width-SIZE);
        pos_y = random(height-SIZE);
      }
    
    
      void update() {
        if (isOver() && !locked && !dontMove || locked && !dontMove )
          c = color (170);
        else
          c = color (0, 170, 170);
    
        if (isClicked()) {
          locked = true;
        }
        if (locked && !dontMove) {
    
          pos_x =  mouseX - offset_x;
          pos_y =  mouseY - offset_y;
        }
      }
    
    
    
      void display() {
        fill(c);
        rect(pos_x, pos_y, SIZE, SIZE);
      }
    
      boolean isOver() {
        float right_x = pos_x + SIZE;
        float bottom_y = pos_y + SIZE;
        return mouseX >= pos_x && mouseX <= right_x &&
          mouseY >= pos_y && mouseY <= bottom_y;
      }
    
      boolean isClicked() {
        return isOver() && mousePressed && !dontMove;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2013-01-04
      • 2020-08-13
      • 1970-01-01
      相关资源
      最近更新 更多