Skip to content

Commit 08533ef

Browse files
authored
Merge pull request #3835 from stalgiag/glGraphicsClear
Make WebGL canvases clear alpha by default + add unit tests for this
2 parents 0dd3d1d + 32da107 commit 08533ef

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

src/webgl/p5.RendererGL.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ p5.RendererGL.prototype = Object.create(p5.Renderer.prototype);
164164

165165
p5.RendererGL.prototype._setAttributeDefaults = function(pInst) {
166166
var defaults = {
167-
alpha: false,
167+
alpha: true,
168168
depth: true,
169169
stencil: true,
170170
antialias: false,
@@ -254,7 +254,7 @@ p5.RendererGL.prototype._resetContext = function(options, callback) {
254254
* The available attributes are:
255255
* <br>
256256
* alpha - indicates if the canvas contains an alpha buffer
257-
* default is false
257+
* default is true
258258
* <br><br>
259259
* depth - indicates whether the drawing buffer has a depth buffer
260260
* of at least 16 bits - default is true

test/unit/webgl/p5.RendererGL.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,76 @@ suite('p5.RendererGL', function() {
231231
});
232232
});
233233

234+
suite('GL Renderer clear()', function() {
235+
var pg;
236+
var pixel;
237+
test('webgl graphics background draws into webgl canvas', function(done) {
238+
myp5.createCanvas(50, 50, myp5.WEBGL);
239+
myp5.background(0, 255, 255, 255);
240+
pg = myp5.createGraphics(25, 50, myp5.WEBGL);
241+
pg.background(0);
242+
myp5.image(pg, -myp5.width / 2, -myp5.height / 2);
243+
pixel = myp5.get(0, 0);
244+
assert.deepEqual(pixel, [0, 0, 0, 255]);
245+
done();
246+
});
247+
248+
test('transparent GL graphics with GL canvas', function(done) {
249+
myp5.createCanvas(50, 50, myp5.WEBGL);
250+
pg = myp5.createGraphics(25, 50, myp5.WEBGL);
251+
myp5.background(0, 255, 255);
252+
pg.clear();
253+
myp5.image(pg, -myp5.width / 2, -myp5.height / 2);
254+
pixel = myp5.get(0, 0);
255+
assert.deepEqual(pixel, [0, 255, 255, 255]);
256+
done();
257+
});
258+
259+
test('semi-transparent GL graphics with GL canvas', function(done) {
260+
myp5.createCanvas(50, 50, myp5.WEBGL);
261+
pg = myp5.createGraphics(25, 50, myp5.WEBGL);
262+
myp5.background(0, 255, 255);
263+
pg.background(100, 100, 100, 100);
264+
myp5.image(pg, -myp5.width / 2, -myp5.height / 2);
265+
pixel = myp5.get(0, 0);
266+
assert.deepEqual(pixel, [39, 194, 194, 194]);
267+
done();
268+
});
269+
270+
test('webgl graphics background draws into 2D canvas', function(done) {
271+
myp5.createCanvas(50, 50);
272+
myp5.background(0, 255, 255, 255);
273+
pg = myp5.createGraphics(25, 50, myp5.WEBGL);
274+
pg.background(0);
275+
myp5.image(pg, 0, 0);
276+
pixel = myp5.get(0, 0);
277+
assert.deepEqual(pixel, [0, 0, 0, 255]);
278+
done();
279+
});
280+
281+
test('transparent GL graphics with 2D canvas', function(done) {
282+
myp5.createCanvas(50, 50);
283+
pg = myp5.createGraphics(25, 50, myp5.WEBGL);
284+
myp5.background(0, 255, 255);
285+
pg.clear();
286+
myp5.image(pg, 0, 0);
287+
pixel = myp5.get(0, 0);
288+
assert.deepEqual(pixel, [0, 255, 255, 255]);
289+
done();
290+
});
291+
292+
test('semi-transparent GL graphics with 2D canvas', function(done) {
293+
myp5.createCanvas(50, 50);
294+
pg = myp5.createGraphics(25, 50, myp5.WEBGL);
295+
myp5.background(0, 255, 255);
296+
pg.background(100, 100, 100, 100);
297+
myp5.image(pg, 0, 0);
298+
pixel = myp5.get(0, 0);
299+
assert.deepEqual(pixel, [39, 194, 194, 255]);
300+
done();
301+
});
302+
});
303+
234304
suite('blendMode()', function() {
235305
var testBlend = function(mode, intended) {
236306
myp5.blendMode(mode);
@@ -274,8 +344,8 @@ suite('p5.RendererGL', function() {
274344
test('blendModes change pixel colors as expected', function(done) {
275345
myp5.createCanvas(10, 10, myp5.WEBGL);
276346
myp5.noStroke();
277-
assert.deepEqual([133, 69, 191, 255], mixAndReturn(myp5.ADD, 255));
278-
assert.deepEqual([0, 0, 255, 255], mixAndReturn(myp5.REPLACE, 255));
347+
assert.deepEqual([133, 69, 191, 158], mixAndReturn(myp5.ADD, 255));
348+
assert.deepEqual([0, 0, 255, 122], mixAndReturn(myp5.REPLACE, 255));
279349
assert.deepEqual([133, 255, 133, 255], mixAndReturn(myp5.SUBTRACT, 255));
280350
assert.deepEqual([255, 0, 255, 255], mixAndReturn(myp5.SCREEN, 0));
281351
assert.deepEqual([0, 255, 0, 255], mixAndReturn(myp5.EXCLUSION, 255));

0 commit comments

Comments
 (0)