最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check support for Offscreen canvas - Stack Overflow

programmeradmin1浏览0评论

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 0
Add a ment  | 

2 Answers 2

Reset to default 5

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');
}

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');
}

发布评论

评论列表(0)

  1. 暂无评论