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.