admin管理员组文章数量:1415491
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?
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?
Share Improve this question edited Jul 21, 2019 at 3:47 user128511 asked Jul 4, 2019 at 9:22 Gianluca GhettiniGianluca Ghettini 11.7k24 gold badges99 silver badges168 bronze badges 02 Answers
Reset to default 5you 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');
}
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');
}
本文标签: javascriptCheck support for Offscreen canvasStack Overflow
版权声明:本文标题:javascript - Check support for Offscreen canvas - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745172951a2646085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论