JS 将canvas画布保存到本地

Posted huas_psw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS 将canvas画布保存到本地相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        window.onload = function() {
            draw();
            var dlButton = document. getElementById ("baocun");
            bindButtonEvent (dlButton, "click", saveAsLocalImage);
        };

        function draw() {
            var canvas = document. getElementById ("thecanvas") ;
            var ctx = canvas.getContext("2d") ;
            ctx.fi11Style = "red";
            ctx.fillRect(20,20,100,100);
        }

        function bindButtonEvent (element, type, handler){
            if(element.addEventListener) {
                element. addEventListener (type, handler, false) ;
            } 
            else{
                element.attachEvent(on+type, handler) ;
            }     
        }      

        function saveImageInfo(){
            var mycanvas = document. getElementById("thecanvas") ;
            var image  = mycanvas . toDataURL ("image/png") ;
            var w=window. open(about:blank, image from canvas) ;
            w.document.write("<img src=‘ "+image+"‘ alt=‘from canvas‘/>") ;
        }

        function saveAsLocalImage () {
            var myCanvas = document .getElementById ("thecanvas") ;
            var image = myCanvas. toDataURL ("image/png") . replace ("image/png", "image/ octet-stream");
            window.location.href=image;
        }
    </script>

    <div>
        <canvas width="100" height="100" id="thecanvas"></canvas>
        <button id="baocun" atyle= "position: absolute;top: 120px:left: 33"></button>
    </div>
</body>
</html>

 

以上是关于JS 将canvas画布保存到本地的主要内容,如果未能解决你的问题,请参考以下文章

小程序--------------处理canvas导出图片模糊问题-------------劉

如何从 Three.js 画布中保存图像?

将 html5 Canvas 作为数据保存到 mysql 数据库

JS中canvas画布绘制中如何实现缩放,位移,旋转

发送“画布”生成的图像,不保存到文件

微信小程序将图片保存到本地的方法