问答详情
源自:6-3 绘制一角弯月

月亮只出来一个弧

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>createMood</title>
</head>
<body>
<canvas id="canvas" style="border:1px solid #ddd;"></canvas>

<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
//以400 400 为圆心,300为半径,逆时针绘制半弧,月亮的外圆
context.arc(400,400,300,0.5*Math.PI,1.5*Math.PI,true);
context.moveTo(400,100);
//以1200 400 为圆心 画内弧
context.arcTo(1200,400,400,700,(400 - 100)*dis(400,100,1200,400)/(1200 - 400));
context.stroke();
}
function dis(x1,y1,x2,y2){
return Math.sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
</script>
</body>
</html>

http://img3.mukewang.com/6322d7760001276209681342.jpg

提问者:会飞的龙猫 2022-09-15 15:42

个回答

  • weibo_大王小二白胖子_0
    2022-10-25 10:10:35

    dis 方法中的 Math.sqrt 错了

    function dis (x1, y1, x2, y2) {

          return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

        }

    少了个括号把参数包住