Categories
JavaScript Answers

How to detect that HTML5 canvas is supported with JavaScript?

Spread the love

To detect that HTML5 canvas is supported with JavaScript, we can try to create a canvas element.

For instance, we write

const isCanvasSupported = () => {
  const elem = document.createElement("canvas");
  return !!elem?.getContext?.("2d");
};

to define the isCanvasSupported function.

In it, we create a canvas with createElement.

Then we try to get its context with elem?.getContext?.("2d").

If it’s defined, then canvas is suppored.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *