# Use Webgl2 in p5js

**URL:** https://discourse.processing.org/t/use-webgl2-in-p5js/33695
**Category:** Project Guidance
**Created:** [November 23, 2021, 3:11pm UTC](https://discourse.processing.org/t/use-webgl2-in-p5js/33695 "2021-11-23T15:11:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [November 23, 2021, 3:11pm UTC](https://discourse.processing.org/t/use-webgl2-in-p5js/33695/1 "2021-11-23T15:11:17Z")

</div>

Is there any way (with example prefered), to use WEBGL2?  
I have to make quite advanced shaders, that at this point I would prefer to move to three.js then missing out on certain shader features.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 23, 2021, 5:45pm UTC](https://discourse.processing.org/t/use-webgl2-in-p5js/33695/2 "2021-11-23T17:45:54Z")

</div>

> [@clankill3r](#):
>
> Is there any way (with example preferred), to use WEBGL2?

This is the top part of the “hidden” method which creates the drawing context:

> <https://github.com/processing/p5.js/blob/v1.4.0/src/webgl/p5.RendererGL.js#L254-L258>

So it should be a matter of replacing that method w/ 1 that uses the string “webgl2” in place of “webgl”:

> **[HTMLCanvasElement: getContext() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext)**
>
> The
> HTMLCanvasElement.getContext() method returns a drawing
> context on the canvas, or null if the context identifier is not
> supported, or the canvas has already been set to a different context mode.

```auto
p5.RendererGL.prototype._initContext = function() {
  try {
    this.drawingContext =
      this.canvas.getContext('webgl2', this._pInst._glAttributes) ||
      this.canvas.getContext('webgl', this._pInst._glAttributes) ||
      this.canvas.getContext('experimental-webgl', this._pInst._glAttributes);
    if (this.drawingContext === null) {
      throw new Error('Error creating webgl context');
    } else {
      const gl = this.drawingContext;
      gl.enable(gl.DEPTH_TEST);
      gl.depthFunc(gl.LEQUAL);
      gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
      this._viewport = this.drawingContext.getParameter(
        this.drawingContext.VIEWPORT
      );
    }
  } catch (er) {
    throw er;
  }
};

```

I believe you can place that method re-assignment within callback **preload()**.

Alternatively within callback **setup()** before invoking **createCanvas()**.
