Reputation: 11628
I have an HTML/JavaScript SPA and I want to check whether Offscreen canvas is supported by the Browser or not.
I tried with
var canvasTest = document.createElement('canvas-test');
if(typeof canvasTest.transferControlToOffscreen === "function")
{
return true;
}
else
{
return false;
}
but it doesn't work (i.e. it returns false even though the Browser actually supports it)
I'm using this code to check if the function exists or not: How to check if function exists in JavaScript?
Upvotes: 2
Views: 2109
Reputation:
you can check the prototype of the canvas which is a much more lightweight way to check than creating a canvas element.
if (HTMLCanvasElement.prototype.transferControlToOffscreen) {
console.log('support for transferring control of an offscreen canvas exists');
} else {
console.log('support for transferring control of an offscreen canvas does NOT exist');
}
Note that OffscreenCanvas
and support for transferring a canvas to an offscreen canvas are technically not the same thing. It's possible though probably 0% chance of possibility for a browser to support OffscreenCanvas
and not support transferring a canvas's control to one. So technically to check for OffscreenCanvas
support you'd do this
if (typeof OffscreenCanvas !== 'undefined') {
console.log('OffscreenCanvas is supported');
} else {
console.log('OffscreenCanvas is NOT supported');
}
Upvotes: 5
Reputation: 788
The problem with your code is that you create a canvas-test
dom element, which is not the same as canvas
.
Try this.
var canvasTest = document.createElement('canvas');
if(typeof canvasTest.transferControlToOffscreen === "function")
{
console.log('true');
}
else
{
console.log('false');
}
Upvotes: 4