2017-07-10 24 views
0

我創建一個保存和重置按鈕的畫布。畫布支持免費抽獎,是簽名的必備條件。畫布將不會重置後fillRect

雖然我不能讓重置按鈕工作,保存按鈕的作品。代碼片段HTML:

<div> 
    <canvas id="theCanvas" width="300" height="150" style="-ms-touch-action: none; border: solid 1px #999"></canvas> 
    <div class="btn-group"> 
     <button onclick="resetCanvas();" class="btn btn-danger">Reset</button> 
     <button onclick="saveCanvas();" class="btn btn-primary">Opslaan</button> 
    </div>  
</div> 

代碼段的javascript:

var canvas, topmenu, context, touches = [], isWriting = false, lastContactPoint, currentTouchId; 
var requestAnimFrame = window.requestAnimationFrame || 
    window.mozRequestAnimationFrame || 
    window.webkitRequestAnimationFrame || 
    window.msRequestAnimationFrame || 
    function (callback) { 
     window.setTimeout(callback, 1000/60); 
    }; 

function draw() { 
    if (context) { 
     context.fillStyle = '#FFF'; 
     context.fillRect(0, 0, canvas.width, canvas.height); 
     var i; 
     for (i = 0; i < touches.length; i++) { 
      drawSegment(touches[i]); 
     } 
    } 
    requestAnimFrame(draw); 
} 

function drawSegment(segment) { 
    var i, firstTouch = true; 
    context.beginPath(); 
    for (i = 0; i < segment.length; i++) { 
     var touch = segment[i]; 
     if (firstTouch) { 
      firstTouch = false; 
      context.beginPath(); 
      context.moveTo(touch.x, touch.y); 
      continue; 
     } 
     context.lineTo(touch.x, touch.y); 
    } 

    context.strokeStyle = '#000'; 
    context.lineWidth = 3; 
    context.stroke(); 
    context.closePath(); 
} 

function addTouch(position) { 
    var touchArray = touches[touches.length - 1]; 
    touchArray.push(position); 
} 

requestAnimFrame(function() { 
    draw(); 
}); 

function load() { 
    topmenu = document.getElementsByClassName("navbar-fixed-top")[0] 
    canvas = document.getElementById('theCanvas'); 
    context = canvas.getContext('2d'); 
    canvas.addEventListener('touchstart', function (evt) { 
     evt.preventDefault(); 
     currentTouchId = evt.touches[0].identifier; 
     touches.push([]); 
     position = getPositionFromTarget(evt.touches[0], evt.touches[0].target); 
     addTouch(position); 
    }); 
    canvas.addEventListener('touchmove', function (evt) { 
     evt.preventDefault(); 
     var i, position; 
     for (i = 0; i < evt.changedTouches.length; i++) { 
      if (evt.changedTouches[i].identifier !== currentTouchId) 
       continue; 
      position = getPositionFromTarget(evt.changedTouches[i], evt.changedTouches[i].target); 
      addTouch(position); 
     } 
    }); 
    if (window.navigator.msPointerEnabled) { 
     canvas.addEventListener('MSPointerDown', function (evt) { 
      if (currentTouchId) 
       return; 
      currentTouchId = evt.pointerId; 
      touches.push([]); 
      var position = getPositionFromTarget(evt, evt.target); 
      addTouch(position); 
     }); 
     canvas.addEventListener('MSPointerMove', function (evt) { 
      if (evt.pointerId !== currentTouchId) 
       return; 
      var position = getPositionFromTarget(evt, evt.target); 
      addTouch(position); 
     }); 
     canvas.addEventListener('MSPointerUp', function (evt) { 
      currentTouchId = undefined; 
     }); 
    } 
    else { 
     canvas.addEventListener('mousedown', function (evt) { 
      var position = getPositionFromTarget(evt, evt.target); 
      touches.push([]); 
      addTouch(position); 
      isWriting = true; 
     }); 
     canvas.addEventListener('mousemove', function (evt) { 
      if (isWriting) { 
       var position = getPositionFromTarget(evt, evt.target); 
       addTouch(position); 
      } 
     }); 
     canvas.addEventListener('mouseup', function (evt) { 
      var position = getPositionFromTarget(evt, evt.target); 
      addTouch(position); 
      isWriting = false; 
     }); 
    } 
} 

function getPositionFromTarget(evt, target) { 
    return { 
     y: evt.pageY - (target.offsetTop + (topmenu.clientHeight - 3)), 
     x: evt.pageX - target.offsetLeft 
    }; 
} 

window.addEventListener('load', load); 

function resetCanvas() { 
    context.beginPath(); 
    context.clearRect(0, 0, canvas.width, canvas.height); 
    context.closePath(); 
} 

我讀過用beginPath方法和closePath一些解決方案,並試圖在功能resetCanvas和平局段。

然後我試圖從畫布除去聽衆,也許這可能是原因...雖然經過使用我刪除了聽衆:

//var new_element = canvas.cloneNode(true); 
//canvas.parentNode.replaceChild(new_element, canvas); 

我不能再畫畫,我是什麼失蹤?

回答

1

而不是畫一個白色的矩形,使用clearRect()清除/重置畫布的方法。

此外,您需要重置touches數組(因爲保存的段正在循環中繪製)

function resetCanvas() { 
    touches = []; //reset 'touches' array 
    context.clearRect(0, 0, canvas.width, canvas.height); //clear canvas 
} 
+0

同樣的結果,忘了提及。也不起作用:l – Ferryzijl

+0

hm。檢查編輯的答案 –

+1

是的,最後!那就是訣竅!謝謝 – Ferryzijl