Skip to content

Commit aea0758

Browse files
authored
Merge pull request #2543 from mlarghydracept/loadPixels_cleanup
loadPixels cleanup
2 parents 3b862f0 + b2e0dcb commit aea0758

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/webgl/p5.RendererGL.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,6 @@ p5.RendererGL.prototype.get = function(x, y, w, h) {
604604
* Any pixel manipulation must be done directly to the pixels[] array.
605605
*
606606
* @method loadPixels
607-
* @param {Number} [x] starting pixel x position, defaults to 0
608-
* @param {Number} [y] starting pixel y position, defaults to 0
609-
* @param {Number} [w] width of pixels to load, defaults to sketch width
610-
* @param {Number} [h] height of pixels to load, defaults to sketch height
611607
*
612608
*/
613609

@@ -620,17 +616,29 @@ p5.RendererGL.prototype.loadPixels = function() {
620616
return;
621617
}
622618
var pd = this._pInst._pixelDensity;
623-
var x = arguments[0] || 0;
624-
var y = arguments[1] || 0;
625-
var w = arguments[2] || this.width;
626-
var h = arguments[3] || this.height;
619+
var x = 0;
620+
var y = 0;
621+
var w = this.width;
622+
var h = this.height;
627623
w *= pd;
628624
h *= pd;
629-
var pixels = new Uint8Array(
630-
this.GL.drawingBufferWidth * this.GL.drawingBufferHeight * 4
625+
//if there isn't a renderer-level temporary pixels buffer
626+
//make a new one
627+
if (typeof this.pixels === 'undefined') {
628+
this.pixels = new Uint8Array(
629+
this.GL.drawingBufferWidth * this.GL.drawingBufferHeight * 4
630+
);
631+
}
632+
this.GL.readPixels(
633+
x,
634+
y,
635+
w,
636+
h,
637+
this.GL.RGBA,
638+
this.GL.UNSIGNED_BYTE,
639+
this.pixels
631640
);
632-
this.GL.readPixels(x, y, w, h, this.GL.RGBA, this.GL.UNSIGNED_BYTE, pixels);
633-
this._pInst._setProperty('pixels', pixels);
641+
this._pInst._setProperty('pixels', this.pixels);
634642
};
635643

636644
//////////////////////////////////////////////
@@ -663,6 +671,12 @@ p5.RendererGL.prototype.resize = function(w, h) {
663671
// so we'll want to update them if the size of the screen changes.
664672
this._setDefaultCamera();
665673
}
674+
//resize pixels buffer
675+
if (typeof this.pixels !== 'undefined') {
676+
this.pixels = new Uint8Array(
677+
this.GL.drawingBufferWidth * this.GL.drawingBufferHeight * 4
678+
);
679+
}
666680
};
667681

668682
/**

0 commit comments

Comments
 (0)