canvas下载图片

canvas下载图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        canvas{ border: 1px solid black; }
    </style>
</head>
<body>
    <canvas id="myCanvas" width="500" height="500"></canvas>
    <a download="canvas.jpg" href="" onclick="downloadpic(this);">下载图片</a>
    <script>
        var canvas = document.getElementById("myCanvas");
        var ox = canvas.width / 2;
        var oy = canvas.height / 2;

        var ctx = canvas.getContext("2d");
        ctx.font = "42px serif";
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        ctx.fillText("Hello World", ox, oy);

        function downloadpic(e) {
            var image = canvas.toDataURL("image/jpg", 1);
            e.href = image;
        };
    </script>
</body>
</html>
上一篇:学一招,利用MySQL函数实现数据脱敏


下一篇:SQL计算字段