1 回答

TA貢獻1850條經(jīng)驗 獲得超11個贊
我建議使用蒙版而不是更改圖像的像素。創(chuàng)建一個空圖像并將其作為掩碼關(guān)聯(lián)到圖像:
img = loadImage("back.jpg");
front = loadImage("front.jpg");
mask = createImage(img.width, img.height, RGB);
img.mask(mask);
如果您現(xiàn)在繪制兩個圖像,那么您只能“看到”front圖像:
image(front, 0, 0);
image(img, 0, 0);
設(shè)置遮罩的顏色 (255, 255, 255) 而不是改變 的像素front:
mask.pixels[loc] = color(255, 255, 255);
并將蒙版重新應(yīng)用于圖像
img.mask(mask);
釋放鼠標(biāo)按鈕時,必須將遮罩的像素改回 (0, 0, 0) 或簡單地創(chuàng)建一個新的空遮罩:
mask = createImage(img.width, img.height, RGB);
請參閱我將建議應(yīng)用于您的原始代碼的示例:
PImage img, front, mask;
int xstart, ystart, xend, yend;
int ray;
void setup() {
size(961, 534);
img = loadImage("back.jpg");
front = loadImage("front.jpg");
mask = createImage(img.width, img.height, RGB);
img.mask(mask);
xstart = 0;
ystart = 0;
xend = img.width;
yend = img.height;
ray = 50;
}
void draw() {
img.loadPixels();
front.loadPixels();
// loop over image pixels
for (int x = xstart; x < xend; x++) {
for (int y = ystart; y < yend; y++ ) {
int loc = x + y*img.width;
float dd = dist(mouseX, mouseY, x, y);
if (mousePressed && dd < 50) {
mask.pixels[loc] = color(255, 255, 255);
}
else {
if (!mousePressed) {
//mask = createImage(img.width, img.height, RGB);
mask.pixels[loc] = color(0, 0, 0);
}
}
}
}
mask.updatePixels();
img.mask(mask);
// show front image
image(front, 0, 0);
image(img, 0, 0);
}
添加回答
舉報