Skip to content

Commit 5cba0fa

Browse files
committed
Add unit tests and improve set() handling for p5.Graphics
1 parent f26f436 commit 5cba0fa

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

src/core/p5.Renderer2D.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -541,25 +541,17 @@ class Renderer2D extends Renderer {
541541
// round down to get integer numbers
542542
x = Math.floor(x);
543543
y = Math.floor(y);
544-
if (imgOrCol instanceof Graphics) {
544+
if (imgOrCol instanceof Graphics || imgOrCol instanceof Image) {
545545
this.drawingContext.save();
546546
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
547547
this.drawingContext.scale(
548548
this._pixelDensity,
549549
this._pixelDensity
550550
);
551-
this.drawingContext.drawImage(imgOrCol.canvas, x, y);
552-
this.drawingContext.restore();
553-
} else if (imgOrCol instanceof Image) {
554-
this.drawingContext.save();
555-
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
556-
this.drawingContext.scale(
557-
this._pixelDensity,
558-
this._pixelDensity
559-
);
560-
this.drawingContext.clearRect(x, y, imgOrCol.width, imgOrCol.height);
561-
this.drawingContext.drawImage(imgOrCol.canvas, x, y);
562-
this.drawingContext.restore();
551+
const width = imgOrCol.width;
552+
const height = imgOrCol.height;
553+
this.drawingContext.clearRect(x, y, width, height);
554+
this.drawingContext.drawImage(imgOrCol.canvas, x, y, width, height);
563555
} else {
564556
let r = 0,
565557
g = 0,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import p5 from '../../../src/app.js';
2+
3+
suite('set() with p5.Graphics', function() {
4+
let myp5;
5+
6+
beforeEach(function() {
7+
myp5 = new p5(function(p) {
8+
p.setup = function() {
9+
p.createCanvas(100, 100);
10+
p.pixelDensity(1);
11+
};
12+
});
13+
});
14+
15+
afterEach(function() {
16+
myp5.remove();
17+
});
18+
19+
test('set() works with p5.Graphics on main canvas', function() {
20+
myp5.background(0, 255, 0);
21+
const gfx = myp5.createGraphics(20, 20);
22+
gfx.background(255, 0, 0);
23+
myp5.set(10, 10, gfx);
24+
myp5.updatePixels();
25+
assert.deepEqual(myp5.get(15, 15), [255, 0, 0, 255]);
26+
assert.deepEqual(myp5.get(5, 5), [0, 255, 0, 255]);
27+
});
28+
29+
test('set() works with p5.Graphics on p5.Graphics', function() {
30+
const mainGfx = myp5.createGraphics(100, 100);
31+
mainGfx.background(255);
32+
const smallGfx = myp5.createGraphics(20, 20);
33+
smallGfx.background(0, 0, 255);
34+
mainGfx.set(10, 10, smallGfx);
35+
mainGfx.updatePixels();
36+
myp5.image(mainGfx, 0, 0);
37+
assert.deepEqual(myp5.get(15, 15), [0, 0, 255, 255]);
38+
});
39+
40+
test('set() clears area under p5.Graphics', function() {
41+
myp5.background(255);
42+
myp5.stroke(255, 0, 0);
43+
myp5.strokeWeight(2);
44+
for (let i = 0; i < 100; i += 10) {
45+
myp5.line(i, 0, i, 100);
46+
}
47+
const gfx = myp5.createGraphics(40, 40);
48+
gfx.background(0, 255, 0);
49+
myp5.set(30, 30, gfx);
50+
myp5.updatePixels();
51+
assert.deepEqual(myp5.get(35, 35), [0, 255, 0, 255]);
52+
});
53+
54+
test('set() works at edge (0,0)', function() {
55+
myp5.background(255);
56+
const gfx = myp5.createGraphics(30, 30);
57+
gfx.background(0, 0, 255);
58+
myp5.set(0, 0, gfx);
59+
myp5.updatePixels();
60+
assert.deepEqual(myp5.get(15, 15), [0, 0, 255, 255]);
61+
assert.deepEqual(myp5.get(35, 35), [255, 255, 255, 255]);
62+
});
63+
64+
test('set() behaves same for p5.Graphics and p5.Image', function() {
65+
const gfx = myp5.createGraphics(20, 20);
66+
gfx.background(255, 0, 0);
67+
const img = gfx.get();
68+
myp5.background(255);
69+
myp5.set(10, 10, gfx);
70+
myp5.updatePixels();
71+
const pixelFromGfx = myp5.get(15, 15);
72+
myp5.background(255);
73+
myp5.set(10, 10, img);
74+
myp5.updatePixels();
75+
const pixelFromImg = myp5.get(15, 15);
76+
assert.deepEqual(pixelFromGfx, pixelFromImg);
77+
});
78+
});

0 commit comments

Comments
 (0)