2015-02-12 109 views
1

我目前正在研究在畫布上繪製矩形的應用程序。我可以完美地繪製矩形,但是當我嘗試更改鼠標的移動以使矩形更小時,會留下痕跡。當我將矩形的尺寸變小時,如何清除這些痕跡?下面是我使用的JavaScript代碼。提前致謝。如何在HMTL5畫布上繪製矩形時清除路徑

function drawSquare() { 
    // creating a square 
    var w = lastX - startX; 
    var h = lastY - startY; 
    var offsetX = (w < 0) ? w : 0; 
    var offsetY = (h < 0) ? h : 0; 
    var width = Math.abs(w); 
    var height = Math.abs(h); 

    context.beginPath(); 
    context.rect(startX + offsetX, startY + offsetY, width, height); 
    context.fillStyle = "gold"; 
    context.fill(); 
    context.lineWidth = 1; 
    context.strokeStyle = 'red'; 
    context.stroke(); 
    canvas.style.cursor = "default"; 
} 

function getMousePos(canvas, e) { 
    var rect = canvas.getBoundingClientRect(); 
    return { 
     x: e.pageX - canvas.offsetLeft, 
     y: e.pageY - canvas.offsetTop, 
    }; 
} 

function handleMouseDown(e) { 

    // get mouse coordinates 
    mouseX = parseInt(e.pageX - offsetX); 
    mouseY = parseInt(e.pageY - offsetY); 
    // set the starting drag position 
    lastX = mouseX; 
    lastY = mouseY; 

    isDown = true; 

    if (isChecBoxClicked == true) { 
     mouseIsDown = 1; 
     startX = lastX; 
     startY = lastY; 
     var pos = getMousePos(canvas, e); 

     startX = lastX = pos.x; 
     startY = lastY = pos.y; 

     drawSquare(); 

    } 
    else { 
     canvas.style.cursor = "default"; 
    } 
} 

function handleMouseUp(e) { 
    // clear the dragging flag 
    isDown = false; 
    canvas.style.cursor = "default"; 
    // get mouse coordinates 
    mouseX = parseInt(e.pageX - offsetX); 
    mouseY = parseInt(e.pageY - offsetY); 
    // set the starting drag position 
    lastX = mouseX; 
    lastY = mouseY; 

    if (isChecBoxClicked == true) 
    { 
     canvas.style.cursor = "crosshair"; 

     if (mouseIsDown !== 0) { 
      mouseIsDown = 0; 
      var pos = getMousePos(canvas, e); 
      lastX = pos.x; 
      lastY = pos.y; 
      drawSquare(); 
     } 
    } 
} 

function handleMouseMove(e) { 

    // if we're not dragging, exit 
    if (!isDown) { 
     return; 
    } 

    //if (defaultval == 1) { 
    // return; 
    //} 

    if (isChecBoxClicked == true) { 
     canvas.style.cursor = "crosshair"; 

     if (mouseIsDown !== 0) { 

      var pos = getMousePos(canvas, e); 
      lastX = pos.x; 
      lastY = pos.y; 
      drawSquare(); 
     }   
    } 
} 
+0

截圖可能會有所幫助。 – marsze 2015-02-12 09:18:18

+0

...和http://jsfiddle.net會更好。 – Teemu 2015-02-12 09:20:36

+1

@Teemu我會盡量讓jsfiddle啓動並運行。 – 2015-02-12 09:29:08

回答

1

畫布不會自行清除。至少不像您使用的2D環境。如果你不斷繪製它,新的圖形將被放置在舊的圖形上。您需要明確清除它:

context.clearRect(0, 0, canvas.width, canvas.height); 
+0

但是,這將清除整個畫布,畫布上可能會繪製其他內容,應該留在那裏。 – Teemu 2015-02-12 09:22:41

+0

你有錯誤順序的參數;它的startx,starty,寬度,高度,請參閱:https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D.clearRect – Kai 2015-02-12 09:29:15

+0

是的,這確實清除整個畫布,這是一個問題,因爲有一張當前裝載在那裏的圖像,是我在這些留下的痕跡上繪製的圖像。 – 2015-02-12 09:32:58

0

您可能必須清除畫布。如果你只畫一個正方形,你將不得不在drawSquare函數中這樣做。如果你正在繪製多個東西,你將不得不在一個更高的函數中重繪多個東西。

對於清除整個畫布,你可以使用下面的代碼:

context.clearRect (0 , 0 , canvas.width, canvas.height); 

也有很多的畫布庫將管理此爲您和優化重繪區域(他們可能只能清除一部分的畫布,所以有更少的像素重繪)

+0

我在已經加載到畫布上的圖像的頂部繪製了多個方塊。唯一的問題是更改鼠標位置時留下的痕跡。 – 2015-02-12 09:31:12

+0

你將不得不每次重繪圖像和所有的矩形。您只能刪除或添加畫布上的內容,而不能編輯。編輯你將不得不刪除和readd。 – orhanhenrik 2015-02-12 09:34:02

+0

@ Izzey我無法將其設置爲背景,因爲我需要放大圖像並將其拖動。 – 2015-02-12 10:00:15