Skip to content

Commit 62e2651

Browse files
authored
Merge pull request #8326 from VANSH3104/setSupport
Fix set() to support p5.Graphics objects
2 parents b4f23ee + 7b68cbf commit 62e2651

2 files changed

Lines changed: 82 additions & 4 deletions

File tree

src/core/p5.Renderer2D.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,16 +551,17 @@ class Renderer2D extends Renderer {
551551
// round down to get integer numbers
552552
x = Math.floor(x);
553553
y = Math.floor(y);
554-
if (imgOrCol instanceof Image) {
554+
if (imgOrCol instanceof Graphics || imgOrCol instanceof Image) {
555555
this.drawingContext.save();
556556
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
557557
this.drawingContext.scale(
558558
this._pixelDensity,
559559
this._pixelDensity
560560
);
561-
this.drawingContext.clearRect(x, y, imgOrCol.width, imgOrCol.height);
562-
this.drawingContext.drawImage(imgOrCol.canvas, x, y);
563-
this.drawingContext.restore();
561+
const width = imgOrCol.width;
562+
const height = imgOrCol.height;
563+
this.drawingContext.clearRect(x, y, width, height);
564+
this.drawingContext.drawImage(imgOrCol.canvas, x, y, width, height);
564565
} else {
565566
let r = 0,
566567
g = 0,

test/unit/core/rendering.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,81 @@ suite('Rendering', function() {
208208
expect(myp5.splineProperties()).toMatchObject({ tightness: 2 });
209209
});
210210
});
211+
212+
suite('set() with p5.Graphics', function() {
213+
let myp5;
214+
215+
beforeEach(function() {
216+
myp5 = new p5(function(p) {
217+
p.setup = function() {
218+
p.createCanvas(100, 100);
219+
p.pixelDensity(1);
220+
};
221+
});
222+
});
223+
224+
afterEach(function() {
225+
myp5.remove();
226+
});
227+
228+
test('set() works with p5.Graphics on main canvas', function() {
229+
myp5.background(0, 255, 0);
230+
const gfx = myp5.createGraphics(20, 20);
231+
gfx.background(255, 0, 0);
232+
myp5.set(10, 10, gfx);
233+
myp5.updatePixels();
234+
assert.deepEqual(myp5.get(15, 15), [255, 0, 0, 255]);
235+
assert.deepEqual(myp5.get(5, 5), [0, 255, 0, 255]);
236+
});
237+
238+
test('set() works with p5.Graphics on p5.Graphics', function() {
239+
const mainGfx = myp5.createGraphics(100, 100);
240+
mainGfx.background(255);
241+
const smallGfx = myp5.createGraphics(20, 20);
242+
smallGfx.background(0, 0, 255);
243+
mainGfx.set(10, 10, smallGfx);
244+
mainGfx.updatePixels();
245+
myp5.image(mainGfx, 0, 0);
246+
assert.deepEqual(myp5.get(15, 15), [0, 0, 255, 255]);
247+
});
248+
249+
test('set() clears area under p5.Graphics', function() {
250+
myp5.background(255);
251+
myp5.stroke(255, 0, 0);
252+
myp5.strokeWeight(2);
253+
for (let i = 0; i < 100; i += 10) {
254+
myp5.line(i, 0, i, 100);
255+
}
256+
const gfx = myp5.createGraphics(40, 40);
257+
gfx.background(0, 255, 0);
258+
myp5.set(30, 30, gfx);
259+
myp5.updatePixels();
260+
assert.deepEqual(myp5.get(35, 35), [0, 255, 0, 255]);
261+
});
262+
263+
test('set() works at edge (0,0)', function() {
264+
myp5.background(255);
265+
const gfx = myp5.createGraphics(30, 30);
266+
gfx.background(0, 0, 255);
267+
myp5.set(0, 0, gfx);
268+
myp5.updatePixels();
269+
assert.deepEqual(myp5.get(15, 15), [0, 0, 255, 255]);
270+
assert.deepEqual(myp5.get(35, 35), [255, 255, 255, 255]);
271+
});
272+
273+
test('set() behaves same for p5.Graphics and p5.Image', function() {
274+
const gfx = myp5.createGraphics(20, 20);
275+
gfx.background(255, 0, 0);
276+
const img = gfx.get();
277+
myp5.background(255);
278+
myp5.set(10, 10, gfx);
279+
myp5.updatePixels();
280+
const pixelFromGfx = myp5.get(15, 15);
281+
myp5.background(255);
282+
myp5.set(10, 10, img);
283+
myp5.updatePixels();
284+
const pixelFromImg = myp5.get(15, 15);
285+
assert.deepEqual(pixelFromGfx, pixelFromImg);
286+
});
287+
});
211288
});

0 commit comments

Comments
 (0)