Skip to content

Commit

Permalink
Merge pull request #6542 from davepagurek/fix/img-pixel-density
Browse files Browse the repository at this point in the history
Fix width/height not matching after get()
  • Loading branch information
limzykenneth authored Nov 24, 2023
2 parents c0acef8 + 897b9e1 commit a5fa292
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class Renderer extends p5.Element {
}

const region = new p5.Image(w*pd, h*pd);
region._pixelDensity = pd;
region.pixelDensity(pd);
region.canvas
.getContext('2d')
.drawImage(canvas, x, y, w * pd, h * pd, 0, 0, w*pd, h*pd);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/image/p5.Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,23 @@ suite('p5.Image', function() {
});
});
});

suite('p5.Graphics.get()', function() {
for (const density of [1, 2]) {
test(`width and height match at pixel density ${density}`, function() {
const g = myp5.createGraphics(10, 10);
g.pixelDensity(density);
g.rect(2, 2, 5, 5);

const img = g.get();
assert.equal(g.width, img.width);
assert.equal(g.height, img.height);
assert.equal(g.pixelDensity(), img.pixelDensity());

g.loadPixels();
img.loadPixels();
assert.deepEqual([...g.pixels], [...img.pixels]);
});
}
});
});

0 comments on commit a5fa292

Please sign in to comment.