我有一个简单的 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
});
似乎我无法使用辅助选项,当我拖动它时,我想在原始位置保留一个圆的副本。仅当我删除助手选项时,可拖动才会起作用。这只发生在 Canvas 上,如果我使用 css 绘制圆圈则不会。 fiddle 是 here .谢谢!
最佳答案
不幸的是,当您克隆 Canvas 元素时,这不会复制图像数据。您可能希望将 Canvas 数据导出为数据 URL 并克隆图像。
fiddle :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
});
关于javascript - 如何使用助手 'clone' 拖动 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19039746/