这张图片是绘制后通过画布保存下来的,也是最终画出来的效果;实现思路较为简单,挨个画五官,按照思路去实现也还是麻烦,毕竟要不断调试很多遍,好在这里面的线条没那么复杂,曲线也比较均匀,最麻烦的是眼眶,由四条曲线组成。
ctx.translate(-20, 55);
//左眉毛
ctx.beginPath();
ctx.fillStyle = "black";
ctx.moveTo(100, 0);
ctx.bezierCurveTo(120, -30, 160, -30, 180, 0);
ctx.bezierCurveTo(160, -50, 120, -50, 100, 0);
ctx.closePath();
ctx.fill();
ctx.save();
ctx.translate(220, 0);
//右眉毛
ctx.beginPath();
ctx.moveTo(100, 0);
ctx.bezierCurveTo(120, -30, 160, -30, 180, 0);
ctx.bezierCurveTo(160, -50, 120, -50, 100, 0);
ctx.closePath();
ctx.fill();
ctx.restore()
//左眼,外边
ctx.beginPath();
ctx.fillStyle = "rgb(245,200,110)";
ctx.moveTo(45, 45);
ctx.quadraticCurveTo(95, 10, 210, 46);
ctx.quadraticCurveTo(230, 62, 205, 82);
ctx.quadraticCurveTo(95, 68, 50, 82);
ctx.quadraticCurveTo(20, 72, 45, 45);
ctx.closePath();
ctx.fill();
//左眼,内边
ctx.beginPath();
ctx.fillStyle = "rgb(255,255,255)";
ctx.moveTo(50, 50);
ctx.quadraticCurveTo(100, 20, 200, 50);
ctx.quadraticCurveTo(220, 62, 200, 75);
ctx.quadraticCurveTo(100, 62, 50, 75);
ctx.quadraticCurveTo(35, 68, 50, 50);
ctx.closePath();
ctx.fill();
//左眼,眼珠
ctx.beginPath();
ctx.fillStyle = "rgb(100,40,0)";
ctx.arc(76, 54, 18, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
ctx.save();
ctx.translate(240, 0);
//右眼,外边
ctx.beginPath();
ctx.fillStyle = "rgb(245,200,110)";
ctx.moveTo(45, 45);
ctx.quadraticCurveTo(95, 10, 210, 46);
ctx.quadraticCurveTo(230, 62, 205, 82);
ctx.quadraticCurveTo(95, 68, 50, 82);
ctx.quadraticCurveTo(20, 72, 45, 45);
ctx.closePath();
ctx.fill();
//右眼,内边
ctx.beginPath();
ctx.fillStyle = "rgb(255,255,255)";
ctx.moveTo(50, 50);
ctx.quadraticCurveTo(100, 20, 200, 50);
ctx.quadraticCurveTo(220, 62, 200, 75);
ctx.quadraticCurveTo(100, 62, 50, 75);
ctx.quadraticCurveTo(35, 68, 50, 50);
ctx.closePath();
ctx.fill();
//右眼,眼珠
ctx.beginPath();
ctx.fillStyle = "rgb(100,40,0)";
ctx.arc(76, 54, 18, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
ctx.restore()
//嘴巴
ctx.beginPath();
ctx.fillStyle = "rgb(100,40,0)";
ctx.moveTo(80, 220);
ctx.bezierCurveTo(100, 370, 400, 370, 420, 220);
ctx.bezierCurveTo(410, 395, 80, 395, 80, 220);
ctx.closePath();
ctx.fill();
//左脸红
ctx.beginPath();
var ellipse_gradient = ctx.createLinearGradient(50, 50, 50, 135);
ellipse_gradient.addColorStop(0, "rgba(250,100,100,1)");
ellipse_gradient.addColorStop(1, "rgba(250,100,100,0)");
ctx.fillStyle = ellipse_gradient;
ctx.ellipse(110, 110, 40, 20, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
ctx.save();
ctx.translate(280, 0);
//右脸红
ctx.beginPath();
ctx.fillStyle = ellipse_gradient;
ctx.ellipse(110, 110, 40, 20, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
</script>