キャンバスタグを使用して図形を描画できるようにしようとしましたが、新しい線を描画すると他の線はすべて消えます。 Try It (copy+paste to the textarea and click the "Edit and Click Me >>" button)。私は、この問題が5つの最も一般的なブラウザ(IE7,8とIE9betaを含む)に存在することを言及する必要があります。<canvas>修復の問題
コード:
<!DOCTYPE html>
<html>
<head>
<!-- ... -->
<!-- (a script used to support canvas in IE7,8) --><!--[if lte IE 8]>
<script type="text/javascript" src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script>
<![endif]-->
<script type="text/javascript">//<![CDATA[
var cnvs, cntxt;
window.onload = function() {
cnvs = document.getElementById("cnvs");
cntxt = cnvs.getContext("2d");
//...
};
var lastX, lastY;
function beginLine(x, y) {
cntxt.moveTo(x, y);
cntxt.save();
lastX = x;
lastY = y;
cnvs.setAttribute("onmousemove", "preview(event.clientX, event.clientY);");
cnvs.setAttribute("onmouseup", "closeLine(event.clientX, event.clientY);");
}
function closeLine(x, y) {
cntxt.lineTo(x, y);
cntxt.stroke();
cntxt.save();
cnvs.removeAttribute("onmousemove");
cnvs.removeAttribute("onmouseup");
}
function preview(x, y) {
cntxt.beginPath();
cntxt.clearRect(0, 0, cnvs.width, cnvs.height);
cntxt.restore();
cntxt.moveTo(lastX, lastY);
cntxt.lineTo(x, y);
cntxt.stroke();
}
//]]></script>
</head>
<body>
<!-- ... -->
<canvas id="cnvs" style="position:absolute;top:0;left:0;border:1px black solid;" onmousedown="beginLine(event.clientX, event.clientY);"></canvas>
<p style="margin-top:200px;">(click and drag the mouse to draw a line)</p>
</body>
</html>
それはおそらく保存/復元間違いだが、私はそれを見つけることができません。
私は2つの異なるフォーラム試してみました:
を誰もが問題が何であるかを伝えることができませんでした。
特に、変換の保存(回転、平行移動、縮尺)を「保存」することがあります。 – Phrogz