How to determine whether the browser supports canvas

1. If the page has a canvas, it is necessary to inform the user to replace or update the browser. In this case replacement element may be performed by adding between <canvas> tag

< Canvas the above mentioned id = "c1" > Your browser does not support canvas, replace or update your browser </ Canvas >
<canvas id="c1">
        <img src="/canvasError.jpg" />
</canvas>

 

2. If you provide a second set of program logic or behavior does not support the canvas browser, you need to determine whether the browser supports the canvas in js script.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <canvas id="c1" width="300" height="300"></canvas>
    <script>
        determines whether the node has a canvas getContext method, if not, then the browser does not support the canvas//Draw (canvasElement) {
            function
            if (!canvasElement.getContext) return;
            var ctx = canvasElement.getContext("2d");
            // code begin
        }
        var canvasElement = document.getElementById("c1");
        draw(canvasElement);
    </script>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/aisowe/p/11568568.html