In this sketch the HTMLCanvasElement prototype is modified to remember and report on the context type used.
/*
This modifies the HTMLCanvasElement protoype so that it remembers the context
type '2d' or 'webgl2' used with the getContext(...) method.
Taken from:
Source - https://stackoverflow.com/a/26983095
Retrieved 2026-02-06, License - CC BY-SA 3.0
and then modified by Quark for canvasGUI project.
*/
// Store the original 'getContext' function
HTMLCanvasElement.prototype["_getContext"] =
HTMLCanvasElement.prototype.getContext;
// Field to store the requested context type
HTMLCanvasElement.prototype["_contextType"] = '';
// Create new 'getContext' method that stores the context type
// before calling the original function getContext wrapper method
HTMLCanvasElement.prototype.getContext = function (type) {
this["_contextType"] = type;
return this["_getContext"](type);
};
// Retrieve the context type used when creating the context
HTMLCanvasElement.prototype["hasContext"] = function () {
return this["_contextType"];
};
let p5canvas;
function setup() {
p5canvas = createCanvas(480, 320, WEBGL);
}
function draw() {
background(160);
print(p5canvas.canvas.hasContext());
}
I did get some weird results when I ran it in Processing 4.5.1 it reported the context type as ‘webgl’ but in VSC it repoorts it as ‘webgl2’ which is the expected result.