码迷,mamicode.com
首页 > 其他好文 > 详细

canvas下载图片

时间:2019-02-08 18:39:06      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:context   cal   ref   UNC   ice   charset   width   ini   code   

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>

canvas下载图片

标签:context   cal   ref   UNC   ice   charset   width   ini   code   

原文地址:https://www.cnblogs.com/ye-hcj/p/10356406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!