5

我有一个简单的 HTML 画布

<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
});

似乎我不能使用辅助选项,当我拖动它时,我想在原始位置保留一个圆圈的副本。仅当我删除辅助选项时,可拖动的才会起作用。这仅发生在画布上,如果我使用 css 绘制圆圈则不会。小提琴在这里。谢谢!

4

2 回答 2

3

不幸的是,当您克隆画布元素时,这不会复制图像数据。您可能希望将画布数据导出为数据 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
});
于 2013-09-26T22:35:34.870 回答
2

那是因为克隆只克隆了 canvas元素,而不是它的内容(canvas 在这方面是一个特殊的元素)。您可以通过在画布上做标记来查看这方面的证据。

您需要将原始画布中的内容重新绘制到克隆的实例上:

/// you need to get these objects in advance, then:
clonedContext.drawImage(originalCanvas, 0, 0);
于 2013-09-26T22:34:53.647 回答