Skip to content Skip to sidebar Skip to footer

How Can I Override A Native Js Function ( Canvas's Getcontext() ) In Pure Javascript?

I'd like to when canvas element calls 'getContext('webgl')', return 'null' value.. I tried to find WebGLRenderingContext prototype and update it, but i can't find it. WebGLRenderin

Solution 1:

Messing with native prototypes? Well, I assume you know what you are doing. If so and you really want to overwrite getContext prototype method, you can achieve this with simple decorator for HTMLCanvasElement.prototype.getContext:

HTMLCanvasElement.prototype.getContext = function (orig) {
  return function(type) {
    return type !== "webgl" ? orig.apply(this, arguments) : null
  }
}(HTMLCanvasElement.prototype.getContext)

So for any context like 2d, 3d, it will work normally, but for "webgl" it will give null. No idea, why you need this, though.


Post a Comment for "How Can I Override A Native Js Function ( Canvas's Getcontext() ) In Pure Javascript?"