【问题标题】:html5 canvas javascript copy picturehtml5 canvas javascript复制图片
【发布时间】:2014-03-04 02:37:40
【问题描述】:

我有一个 html5 页面。它使用 JQuery 和 sketch.js(免费绘图)。我不会将它连接到任何网络服务器。此刻,我想将此 javascript 用作桌面应用程序。我的目的是将图像从一个画布复制到同一页面中的另一个画布。徒手画东西后,我可以看到 url 被复制到 textarea,但点击 Show URL 链接后没有图片。有人可以给我提示如何将图像从一个画布复制到另一个画布吗?

/1/ 源码,一开始需要加html标签,别忘了

<head>
 <title>sketch and free hand drawing</title>
 <!--[if IE]>
   <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="image/png"/>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/sketch.js"></script>

/2/需要在上面加上/head。不要忘记

<body>
<script>
    function toDo(){
         console.log(document.getElementById("colors_sketch").toDataURL("image/png"));
       document.getElementById("cvs").value=document.getElementById("colors_sketch").toDataURL("image/png");
       document.getElementById("txt").value=document.getElementById("colors_sketch").toDataURL("image/png");
  }   
</script> 
<div class="tools">
   <a href="#colors_sketch" data-download="png" style="float: right; width: 100px;">Download</a>
</div>
<div class="tools">
   <a href="#cvs" style="float: right; width: 100px;" onclick="toDo();">show URL</a>
</div>

<textarea id="txt"></textarea>
<hr/>
<canvas id="colors_sketch" width="800" height="300">hello</canvas>
<hr/>
<canvas id="cvs" width="800" height="300"></canvas>
<script type="text/javascript">
   $(function() {
      $.each(['#f00', '#ff0', '#0f0', '#0ff', '#00f', '#f0f', '#000', '#fff'], function() {
        $('#colors_demo .tools').append("<a href='#colors_sketch' data-color='" + this + "' style='width: 10px; background: " + this + ";'></a> ");
      });
      $.each([3, 5, 10, 15], function() {
        $('#colors_demo .tools').append("<a href='#colors_sketch' data-size='" + this + "' style='background: #ccc'>" + this + "</a> ");
      });
      $('#colors_sketch').sketch();
   });

/3/ 上面需要加/script和/body,上面/html别忘了

【问题讨论】:

    标签: javascript html canvas


    【解决方案1】:

    要将图像从一个画布移动到另一个画布,您可以简单地使用 drawImage

    destinationContext.drawImage(sourceCanvas,0,0);
    

    来自源画布的像素将被绘制到目标画布上。​​

    演示:http://jsfiddle.net/m1erickson/2hb56/

    我尝试修改您的代码,但您似乎也遇到了设计问题。

    SketchJS 将擦除您复制的图像以准备自己的绘图(复制的图像被擦除)。

    将像素从一个画布复制到另一个画布的示例代码:

    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
    <script src="http://code.jquery.com/jquery.min.js"></script>
    <script src="sketch.min.js"></script>
    
    <style>
        body{ background-color: ivory; }
        canvas{border:1px solid red;}
    </style>
    
    <script>
        $(function(){
    
            // get references to canvases and contextx
            sourceCanvas=document.getElementById("source");
            sourceContext=sourceCanvas.getContext("2d");
            destinationCanvas=document.getElementById("destination");
            destinationContext=destinationCanvas.getContext("2d");
    
            // draw a test image into the source canvas
            var testImg=new Image();
            testImg.onload=function(){
                sourceContext.drawImage(testImg,0,0);
            }
            testImg.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house-icon.png";
    
            // when the button is clicked
            // copy the source canvas onto the destination canvas
            $("#copy").click(function(){
                destinationContext.drawImage(sourceCanvas,0,0);
            })
    
    
        }); // end $(function(){});
    </script>
    </head>
    <body>
        <canvas id="source" width=256 height=256></canvas><br>
        <button id="copy">Copy canvas above to canvas below</button><br>
        <canvas id="destination" width=256 height=256></canvas>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2012-03-13
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多