From 897b9e126bba249c9101e0552862aea8afafabc0 Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Wed, 8 Nov 2023 19:07:32 -0500 Subject: [PATCH] Fix width/height not matching after get() --- src/core/p5.Renderer.js | 2 +- test/unit/image/p5.Image.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/p5.Renderer.js b/src/core/p5.Renderer.js index c869f3e6b4..21ce3bd64b 100644 --- a/src/core/p5.Renderer.js +++ b/src/core/p5.Renderer.js @@ -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); diff --git a/test/unit/image/p5.Image.js b/test/unit/image/p5.Image.js index 2600be63ff..0e44d84a29 100644 --- a/test/unit/image/p5.Image.js +++ b/test/unit/image/p5.Image.js @@ -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]); + }); + } + }); });