webglVersion variable is not defined

Hello,

I tried this

function setup() {
  createCanvas(800, 600);
}


function draw() {
  print(webglVersion);
}

with or without WEBGL in the createCanvas() function

and I have in both cases

Uncaught ReferenceError: webglVersion is not defined

reference

I’m using the Processing editor with the stable p5js plugin (the new p5js plugin doesn’t work in linux for now !)

In the web editor, it works !

Is there a way to do this in the Processing editor now ?

P.S. I need my new PstarEngine project (for 2D AND 3D) to know the context

:slight_smile:

N.B. if that’s not possible for now, I can still configure it manually in the code !

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.

This worked with p5js v1.11.10 and v2.2.2

thank you, I’m going to see if I can use this soon.