1 回答

TA贡献1850条经验 获得超11个赞
我建议使用蒙版而不是更改图像的像素。创建一个空图像并将其作为掩码关联到图像:
img = loadImage("back.jpg");
front = loadImage("front.jpg");
mask = createImage(img.width, img.height, RGB);
img.mask(mask);
如果您现在绘制两个图像,那么您只能“看到”front图像:
image(front, 0, 0);
image(img, 0, 0);
设置遮罩的颜色 (255, 255, 255) 而不是改变 的像素front:
mask.pixels[loc] = color(255, 255, 255);
并将蒙版重新应用于图像
img.mask(mask);
释放鼠标按钮时,必须将遮罩的像素改回 (0, 0, 0) 或简单地创建一个新的空遮罩:
mask = createImage(img.width, img.height, RGB);
请参阅我将建议应用于您的原始代码的示例:
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);
}
添加回答
举报