2015-06-04 92 views

回答

-1

var canvas = $("#paper")[0]; 
 
var c = canvas.getContext("2d"); 
 

 
var startX = 10; 
 
var startY = 10; 
 
var endX = 500; 
 
var endY = 10; 
 
var amount = 0; 
 
setInterval(function() { 
 
    amount += 0.05; // change to alter duration 
 
    if (amount > 1) amount = 1; 
 
    c.clearRect(0, 0, canvas.width, canvas.height); 
 
    c.strokeStyle = "black"; 
 
    c.moveTo(startX, startY); 
 
    // lerp : a + (b - a) * f 
 
    c.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount); 
 
    c.stroke(); 
 
}, 30);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<canvas id="paper" width="500" height="500"></canvas>

+0

不正確!您允許'setInterval'繼續運行。請使用'clearInterval'查看:http://stackoverflow.com/questions/1831152/how-to-stop-setinterval – markE

+0

爲什麼你需要添加jQuery腳本?據我所知,目前還沒有jQuery。 – nixon1333