Home > Web Front-end > JS Tutorial > body text

How to check if the browser supports OffscreenCanvas in JavaScript?

WBOY
Release: 2023-08-24 10:25:09
forward
1503 people have browsed it

如何在 JavaScript 中检查浏览器是否支持 OffscreenCanvas?

In HTML, Canvas is very important when we want to display animation or 3D objects on a web page using only HTML and JavaScript.

offscreenCanvas allows users to render animations and graphics off-screen. This means that when we use the canvas, it interacts with the user through the main thread of the web application, but offscreenCanvas does not. Therefore, we can use offscreenCanvas to improve the performance of our application.

Before using offscreenCanvas on any browser, we need to check whether the browser supports it; otherwise, we need to use canvas.

So, we will learn two methods to check if offscreenCanvas is supported.

Method 1: Use typeof operator

The typeof operator allows developers to check the type of variables in JavaScript. Here we will check the type of offscreenCanvas. If the type of offscreenCanvas is not defined in a specific browser, it means that the browser does not support it

grammar

Users can follow the following syntax to check whether the browser supports offscreenCanvas or does not use the typeof operator -

if (typeof OffscreenCanvas === "undefined") {
   // not supported
} else {
   // supported
}
Copy after login

In the above syntax, we use offscreenCanvas as the operand of the typeof operator.

Example

In the example below, when the user clicks the button, it calls the isSupported() function. The isSupported() function uses the typeof operator to get the type of offscreenCanvas in a specific browser and uses an if-else statement to check if it is undefined or something.

<html>
<body>
   <h3>Using the <i> typeof </i> operator to check if OffscreenCanvas is supported by Browser or not</h3>
   <button onclick = "isSupported()"> Check OffscreenCanvas is supported </button>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      function isSupported() {
         if (typeof OffscreenCanvas === "undefined"){
            output.innerHTML +=
            "The OffscreenCanvas in this browser is not supported!";
         } else {
            output.innerHTML +=
            "The OffscreenCanvas in this browser is supported!";
         }
      }
   </script>
</body>
</html>
Copy after login

Method 2: Use the transferControlToOffscreen function of canvas

We can create a simple canvas using HTML. Afterwards, if we want to use OffScreenCanvas, we need to call the transferControlToOffscreen() function. If the browser supports OffScreenCanvas, every property and method of OffScreenCanvas is also supported.

So, we will check the type of transferControlToOffscreen() function, if it returns "function", it means that the browser supports OffScreenCanvas.

grammar

Users can check whether the browser supports OffScreenCanvas by checking the type of the transferControlToOffscreen() function according to the following syntax.

// Creating the canvas element
var canvas_sample = document.createElement("canvas");
if (typeof canvas_sample.transferControlToOffscreen !== "function") {
   //Browser doesn't support OffScreenCanvas
} else {
   //Browser supports OffScreenCanvas
}
Copy after login

In the above syntax, we create the canvas element and access transferControlToOffscreen() by taking it as a reference and checking its type.

Example

In this example, we create a canvas_sample element using JavaScript's createElement() method. After that, we use the transferControlToOffscreen() canvas method as the operand of the typeof operator to check its type.

Users can observe in the output that Chrome browser supports OffScreenCanvas and it returns "Function" as the type of TransferControlToOffscreen() method.

<html>
<body>
   <h3>Using the <i> transferControlToOffscreen </i> function to check if OffScreenCanvas is supported by Browser or not  </h3>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      function isSupported() {
         var canvas_sample = document.createElement("canvas");

         if (typeof canvas_sample.transferControlToOffscreen !== "function") {
            output.innerHTML +=
            "The OffScreenCanvas in this browser is not supported!";
         } else {
            output.innerHTML +=
            "The OffScreenCanvas in this browser is supported!";
         }
      }
      isSupported();
   </script>
</body>
</html>
Copy after login

Users learned two ways to check if the browser supports OffScreenCanvas. However, we use the typeof operator in both methods. Nonetheless, we check the type of OffScreenCanvas in the first method and the transferControlToOffscreen() function in the second method.

The above is the detailed content of How to check if the browser supports OffscreenCanvas in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!