码迷,mamicode.com
首页 > Web开发 > 详细

JS 将canvas画布保存到本地

时间:2018-12-10 22:01:03      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:head   rect   idt   lse   pos   stream   function   event   and   

<!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画布保存到本地

标签:head   rect   idt   lse   pos   stream   function   event   and   

原文地址:https://www.cnblogs.com/huaspsw/p/10099146.html

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