[英]html5 canvas javascript copy picture
我有一个html5页面。 它使用JQuery和sketch.js(免费绘图)。 我不打算将它连接到任何Web服务器。 此时,我想将此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 /需要添加/以上。 别忘了<>
<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上面不要忘记<>
要将图像从一个画布移动到另一个画布,您只需使用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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.