用画布写一个哆啦A梦
首先要画一个脑袋
// 1.大脑袋
context.beginPath();
context.arc(300, 300, 250, 0, Math.PI * 2);
context.lineWidth = "5";
context.stroke();
context.fillStyle = "rgb(34,118,207)";
context.fill();
然后画一个脸带子
context.beginPath();
context.ellipse(300, 380, 200, 170, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
大嘴巴子也不能忘了
// 3.大嘴巴子
context.beginPath();
context.arc(300, 460, 50, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "black";
context.fill();
context.beginPath();
context.ellipse(300, 470, 48, 40, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "red";
context.fill();
麻烦点的眼睛也要画
// 4.大眼珠子
context.beginPath();
context.ellipse(248, 230, 50, 60, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
context.beginPath();
context.ellipse(270, 230, 20, 30, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "black";
context.fill();
context.beginPath();
context.ellipse(270, 230, 5, 10, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
context.beginPath();
context.ellipse(352, 230, 50, 60, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
context.beginPath();
context.ellipse(330, 230, 20, 30, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "black";
context.fill();
context.beginPath();
context.ellipse(330, 230, 5, 10, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
最后鼻子和胡子
// 5.大鼻子
context.beginPath();
context.lineWidth = "3";
context.arc(300, 290, 30, 0, 2 * Math.PI);
context.stroke();
context.fillStyle = "red";
context.fill();
// 6.画胡子
context.lineWidth="5";
huzi(125,294,230,335);
huzi(113,370,222,366);
huzi(125,434,222,398);
huzi(376,335,465,282);
huzi(385,369,490,354);
huzi(385,400,488,420);
全部代码
<!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>Document</title>
<style>
canvas {
display:block;
margin:0 auto;
background: black
}
</style>
</head>
<body>
<canvas id="my_canvas"></canvas>
<script>
var oCanvas = document.getElementById("my_canvas");
oCanvas.width = 600;
oCanvas.height = 600;
var context = oCanvas.getContext("2d");
// document.onclick = function (ev) {
// console.log(ev.pageX, ev.pageY)
// }
// 1.大脑袋
context.beginPath();
context.arc(300, 300, 250, 0, Math.PI * 2);
context.lineWidth = "5";
context.stroke();
context.fillStyle = "rgb(34,118,207)";
context.fill();
// 2.大脸蛋子
// context.scale(1,0.9);
// context.beginPath();
// context.arc(300,410,200,0,Math.PI*2);
// context.lineWidth="5";
// context.stroke();
// context.ellipse(x,y,r1,r2,旋转的角度,起始角度,结束角度)
context.beginPath();
context.ellipse(300, 380, 200, 170, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
// 3.大嘴巴子
context.beginPath();
context.arc(300, 460, 50, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "black";
context.fill();
context.beginPath();
context.ellipse(300, 470, 48, 40, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "red";
context.fill();
// 4.大眼珠子
context.beginPath();
context.ellipse(248, 230, 50, 60, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
context.beginPath();
context.ellipse(270, 230, 20, 30, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "black";
context.fill();
context.beginPath();
context.ellipse(270, 230, 5, 10, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
context.beginPath();
context.ellipse(352, 230, 50, 60, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
context.beginPath();
context.ellipse(330, 230, 20, 30, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "black";
context.fill();
context.beginPath();
context.ellipse(330, 230, 5, 10, 0, 0, Math.PI * 2);
context.stroke();
context.fillStyle = "white";
context.fill();
// 5.大鼻子
context.beginPath();
context.lineWidth = "3";
context.arc(300, 290, 30, 0, 2 * Math.PI);
context.stroke();
context.fillStyle = "red";
context.fill();
// 6.画胡子
context.lineWidth="5";
huzi(125,294,230,335);
huzi(113,370,222,366);
huzi(125,434,222,398);
huzi(376,335,465,282);
huzi(385,369,490,354);
huzi(385,400,488,420);
// 画胡子的方法
function huzi(x1, y1, x2, y2) {
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.stroke();
}
</script>
</body>
</html>