Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supporting existing canvas elements for createGraphics #4563

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/core/p5.Graphics.js
Expand Up @@ -21,13 +21,21 @@ import * as constants from './constants';
* @param {Number} h height
* @param {Constant} renderer the renderer to use, either P2D or WEBGL
* @param {p5} [pInst] pointer to p5 instance
* @param {Object} canvas existing html canvas element
*/
p5.Graphics = function(w, h, renderer, pInst) {
p5.Graphics = function(w, h, renderer, pInst, canvas) {
const r = renderer || constants.P2D;

this.canvas = document.createElement('canvas');
if(canvas){
this.canvas = canvas;
}else {
this.canvas = document.createElement('canvas');
}

const node = pInst._userNode || document.body;
node.appendChild(this.canvas);
if(!canvas) {
node.appendChild(this.canvas);
}

p5.Element.call(this, this.canvas, pInst);

Expand Down
5 changes: 3 additions & 2 deletions src/core/rendering.js
Expand Up @@ -199,6 +199,7 @@ p5.prototype.noCanvas = function() {
* @param {Number} h height of the offscreen graphics buffer
* @param {Constant} [renderer] either P2D or WEBGL
* undefined defaults to p2d
@param {Object} canvas existing html canvas element
* @return {p5.Graphics} offscreen graphics buffer
* @example
* <div>
Expand All @@ -224,9 +225,9 @@ p5.prototype.noCanvas = function() {
* 4 grey squares alternating light and dark grey. White quarter circle mid-left.
*
*/
p5.prototype.createGraphics = function(w, h, renderer) {
p5.prototype.createGraphics = function(w, h, renderer, canvas) {
p5._validateParameters('createGraphics', arguments);
return new p5.Graphics(w, h, renderer, this);
return new p5.Graphics(w, h, renderer, this, canvas);
};

/**
Expand Down