From 622d227ba1543855ce9d3e783fdd98812dc17453 Mon Sep 17 00:00:00 2001 From: limzykenneth Date: Wed, 19 Feb 2025 21:56:09 +0000 Subject: [PATCH] Attach p5.Element methods to WebGL canvas --- src/webgl/p5.RendererGL.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 29a548bed2..3e20dfa733 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -138,6 +138,20 @@ class RendererGL extends Renderer { this.elt.id = "defaultCanvas0"; this.elt.classList.add("p5Canvas"); + // Set and return p5.Element + this.wrappedElt = new Element(this.elt, this._pInst); + + // Extend renderer with methods of p5.Element with getters + for (const p of Object.getOwnPropertyNames(Element.prototype)) { + if (p !== 'constructor' && p[0] !== '_') { + Object.defineProperty(this, p, { + get() { + return this.wrappedElt[p]; + } + }) + } + } + const dimensions = this._adjustDimensions(w, h); w = dimensions.adjustedWidth; h = dimensions.adjustedHeight;