我正在尝试重置里面的绿色弧线,drawValueArc()
以便每次单击更改按钮时,绿色弧线都会被删除并重新绘制。如何在不移除整个画布的情况下将其移除?另外,顺便说一句,我注意到Math.random() * 405 * Math.PI / 180
实际上并不总是产生适合灰色弧线的弧线,有时它比灰色弧线大,这是为什么呢?
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cx = 150;
var cy = 150;
var startRadians = 135 * Math.PI / 180;
var endRadians = 405 * Math.PI / 180;
//main arc
ctx.beginPath();
ctx.arc(cx, cy, 58, startRadians, endRadians, false);
ctx.strokeStyle="rgb(220,220,220)";
ctx.lineWidth = 38;
ctx.stroke();
$('#setRandomValue').click(function(){
drawValueArc(Math.random() * 405 * Math.PI / 180);
});
function drawValueArc(val){
//ctx.clearRect(0, 0, W, H);
ctx.beginPath();
ctx.arc(cx, cy, 58, startRadians, val, false);
ctx.strokeStyle = "green";
ctx.lineWidth = 38;
ctx.stroke();
}