Skip to content

Commit 11519b6

Browse files
committed
fix and test masking with different densities
1 parent f841abb commit 11519b6

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/image/p5.Image.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,8 @@ p5.Image = class {
846846
* Masks part of an image from displaying by loading another
847847
* image and using its alpha channel as an alpha channel for
848848
* this image. Masks are cumulative, once applied to an image
849-
* object, they cannot be removed.
849+
* object, they cannot be removed. If the mask has a different
850+
* pixel density from this image, the mask will be scaled
850851
*
851852
* @method mask
852853
* @param {p5.Image} srcImage source image.
@@ -883,10 +884,6 @@ p5.Image = class {
883884
if (p5Image instanceof p5.Renderer) {
884885
maskScaleFactor = p5Image._pInst._pixelDensity;
885886
}
886-
if (p5Image instanceof p5.Image) {
887-
maskScaleFactor = p5Image._pixelDensity;
888-
}
889-
maskScaleFactor /= imgScaleFactor;
890887

891888
const copyArgs = [
892889
p5Image,

test/unit/image/p5.Image.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ suite('p5.Image', function() {
5050
});
5151
});
5252

53-
suite('p5.Image.prototype.mask', function() {
53+
suite.only('p5.Image.prototype.mask', function() {
5454
for (const density of [1, 2]) {
5555
test(`it should mask the image at pixel density ${density}`, function() {
5656
let img = myp5.createImage(10, 10);
@@ -86,6 +86,39 @@ suite('p5.Image', function() {
8686
});
8787
}
8888

89+
test('it should mask images of different density', function() {
90+
let img = myp5.createImage(10, 10);
91+
img.pixelDensity(1);
92+
img.loadPixels();
93+
for (let i = 0; i < img.height; i++) {
94+
for (let j = 0; j < img.width; j++) {
95+
let alpha = i < 5 ? 255 : 0;
96+
img.set(i, j, myp5.color(0, 0, 0, alpha));
97+
}
98+
}
99+
img.updatePixels();
100+
101+
let mask = myp5.createImage(20, 20);
102+
mask.loadPixels();
103+
for (let i = 0; i < mask.width; i++) {
104+
for (let j = 0; j < mask.height; j++) {
105+
let alpha = j < 10 ? 255 : 0;
106+
mask.set(i, j, myp5.color(0, 0, 0, alpha));
107+
}
108+
}
109+
mask.updatePixels();
110+
mask.pixelDensity(2);
111+
112+
img.mask(mask);
113+
img.loadPixels();
114+
for (let i = 0; i < img.width; i++) {
115+
for (let j = 0; j < img.height; j++) {
116+
let alpha = i < 5 && j < 5 ? 255 : 0;
117+
assert.strictEqual(img.get(i, j)[3], alpha);
118+
}
119+
}
120+
});
121+
89122
test('it should mask the animated gif image', function() {
90123
const imagePath = 'unit/assets/nyan_cat.gif';
91124
return new Promise(function(resolve, reject) {

0 commit comments

Comments
 (0)