2

で困ってい.rect()ます。を使用してグリッドを作成し、 I を使用する.drawImage(canvas...).stroke()、再び表示されます。その長方形を完全に削除するにはどうすればよいですか?

よろしく


ワーキングjsFiddle


var canvas = document.getElementById('board');
var context = canvas.getContext('2d'),

    wt = canvas.width,
    ht = canvas.height;

canvas.onmousedown = function (e) {
    e.preventDefault(); // disabling selecting
    context.strokeStyle = "red";
    context.lineWidth = 1;
    context.rect(wt / 2 - 50, ht / 2 - 100, 100, 200);
    context.fillText("<- why is it here? didn't \"clearRect()\" delete it?", 8, 8);
    context.stroke();
};

function grid() {
    var h = 2.5,
        p = 2.5;
    context.rect(0.5, 0.5, 5, 5);
    context.strokeStyle = "#f0f0f0";
    context.stroke(); // creating a 5x5 small rectangle in top left corner
    for (i = 0; i < wt; i += p) {
        p *= 2;
        context.drawImage(canvas, p, 0); // replicating it horizontally...
    }
    for (i = 0; i < ht; i += h) {
        h *= 2;
        context.drawImage(canvas, 0, h); // ... and vertically
    }
    context.clearRect(0, 0, 5, 5); // here I am deleting that stroke, because I don't need it anymore
    context.drawImage(canvas, 0, 55, 5.5, 5.5, 0, 0, 5.5, 5.5); // drawing it with drawImage();
}
grid();
4

1 に答える 1

4

clearRectで描画しているパスの一部ではなく、キャンバスの一部をクリアしますrect。ストロークが適用されたときに含まれbeginPath()ないように以前をクリアするために呼び出します。rect

この更新された例を参照してください

于 2013-07-06T17:22:19.480 に答える