用JavaScript绘制可爱的猫咪 - 简单教程
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="catCanvas" width="400" height="400"></canvas>
<pre><code><script>
var canvas = document.getElementById("catCanvas");
var ctx = canvas.getContext("2d");
// 绘制猫的头部
ctx.beginPath();
ctx.arc(200, 200, 100, 0, 2 * Math.PI);
ctx.fillStyle = "gray";
ctx.fill();
// 绘制猫的眼睛
ctx.beginPath();
ctx.arc(160, 160, 20, 0, 2 * Math.PI);
ctx.fillStyle = "white";
ctx.fill();
ctx.beginPath();
ctx.arc(240, 160, 20, 0, 2 * Math.PI);
ctx.fillStyle = "white";
ctx.fill();
ctx.beginPath();
ctx.arc(160, 160, 5, 0, 2 * Math.PI);
ctx.fillStyle = "black";
ctx.fill();
ctx.beginPath();
ctx.arc(240, 160, 5, 0, 2 * Math.PI);
ctx.fillStyle = "black";
ctx.fill();
// 绘制猫的鼻子
ctx.beginPath();
ctx.moveTo(200, 170);
ctx.lineTo(180, 200);
ctx.lineTo(220, 200);
ctx.closePath();
ctx.fillStyle = "pink";
ctx.fill();
// 绘制猫的嘴巴
ctx.beginPath();
ctx.arc(200, 220, 40, 0.4 * Math.PI, 0.6 * Math.PI);
ctx.lineWidth = 5;
ctx.strokeStyle = "black";
ctx.stroke();
// 绘制猫的身体
ctx.beginPath();
ctx.moveTo(200, 300);
ctx.lineTo(100, 400);
ctx.lineTo(300, 400);
ctx.closePath();
ctx.fillStyle = "gray";
ctx.fill();
// 绘制猫的尾巴
ctx.beginPath();
ctx.moveTo(300, 400);
ctx.lineTo(350, 350);
ctx.lineTo(350, 450);
ctx.closePath();
ctx.fillStyle = "gray";
ctx.fill();
// 绘制猫的脚
ctx.beginPath();
ctx.arc(150, 400, 30, 0, 2 * Math.PI);
ctx.fillStyle = "gray";
ctx.fill();
ctx.beginPath();
ctx.arc(250, 400, 30, 0, 2 * Math.PI);
ctx.fillStyle = "gray";
ctx.fill();
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/pEgt 著作权归作者所有。请勿转载和采集!