我有一个简单的HTML Canvas
<div class='circle'>
<canvas id="myCanvas" width="100" height="100">Your browser does not support the HTML5 canvas tag.</canvas>
</div>
与风格
.circle {
height: auto;
width: auto;
}
和脚本
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(50, 50, 50, 0, 2 * Math.PI);
ctx.fill();
$('.circle').draggable({
helper: 'clone' // Remove this line to make it draggable
});
似乎无法在将圆拖曳时将圆环副本保留在原始位置的地方使用helper选项。仅当我删除helper选项时,该Dragable才起作用。这仅发生在 Canvas 上,如果我使用css绘制圆圈,则不会发生这种情况。小提琴是here。谢谢!
最佳答案
不幸的是,当您克隆 Canvas 元素时,它不会复制图像数据。您可能需要将 Canvas 数据导出为数据URL,然后克隆图像。
小提琴:http://jsfiddle.net/gwwar/Bdpq9/2/
<div class='circle'>
</div>
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(50, 50, 50, 0, 2 * Math.PI);
ctx.fill();
var url = c.toDataURL();
var img = document.createElement("img");
img.setAttribute("src",url);
$(".circle").append(img);
$('.circle').draggable({
helper: 'clone' // Remove this line to make it draggable
});