我编写了一个HTML页面,其中标题包含一个表单,您可以在其中输入三个数字。每个数字在圆环图中绘制一个环。如果输入零,则不绘制任何环。如果输入50,则绘制半个圆环。如果输入100,则绘制完整的环。有一个绿色外圈,一个橙色中圈和一个蓝色内圈。
当我将表单中的数字从0增加时,通过键入或单击增加输入字段中数字的按钮,然后单击"更新响铃",一切正常。但是,当我尝试通过降低输入字段中的数字来缩短任何环时,单击"更新响铃"不会缩短戒指。简而言之,我可以提高百分比值,但我无法减少它们。
以下是代码:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
</style>
<form id="frm1" action="/action_page.php">
Outer Ring Completion %: <input type="number" name="apercent" value="0" min="0" max="100">
Middle Ring Completion %: <input type="number" name="bpercent" value="0" min="0" max="100">
Inner Ring Completion %: <input type="number" name="cpercent" value="0" min="0" max="100">
</form>
<br>
<button onclick="ChangeNums()">Update rings</button>
<br>
</head>
<body onload="ChangeNums()">
<canvas id="myCanvas" width="600" height="600"></canvas>
<script>
var outerval;
var middleval;
var innerval;
function ChangeNums() {
var x = document.getElementById("frm1");
outerval = x.apercent.value;
middleval = x.bpercent.value;
innerval = x.cpercent.value;
RenderChart()
};
function RenderChart() {
//Outermost line
var canvas = document.getElementById('myCanvas');
var outerline = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 270;
var startAngle = 1.5 * Math.PI;
var endAngle = ((outerval - 25) / 50) * Math.PI;
//var endAngle = 0.25 * Math.PI;
var counterClockwise = false;
outerline.beginPath();
outerline.arc(x, y, radius, startAngle, endAngle, counterClockwise);
outerline.lineWidth = 30;
// line color
outerline.strokeStyle = 'green';
if(outerval != 0){
outerline.stroke();
}
//Middle line
var middleline = canvas.getContext('2d');
var radiusB = 240;
var startAngleB = 1.5 * Math.PI;
var endAngleB = ((middleval - 25) / 50) * Math.PI;
//var endAngleB = 0 * Math.PI;
var counterClockwiseB = false;
middleline.beginPath();
middleline.arc(x, y, radiusB, startAngleB, endAngleB, counterClockwiseB);
middleline.lineWidth = 30;
// line color
middleline.strokeStyle = 'orange';
if(middleval != 0){
middleline.stroke();
}
//Inner line
var innerline = canvas.getContext('2d');
var radiusC = 210;
var startAngleC = 1.5 * Math.PI;
var endAngleC = ((innerval - 25) / 50) * Math.PI;
//var endAngleC = 0.5 * Math.PI;
var counterClockwiseC = false;
innerline.beginPath();
innerline.arc(x, y, radiusC, startAngleC, endAngleC, counterClockwiseC);
innerline.lineWidth = 30;
// line color
innerline.strokeStyle = 'blue';
if(innerval != 0){
innerline.stroke();
}
}
</script>
</body>
</html>
答案 0 :(得分:2)
在重新渲染之前清除画布是最安全的处理方式。值正在变化,但它不会自动清除画布。
arguments
之前
outerline.clearRect(0, 0, canvas.width, canvas.height);
将完成这项工作