用canvas画一个简易的机器猫头像

32 篇文章 0 订阅
5 篇文章 0 订阅

用canvas画一个机器猫的头像,原图如下:
机器猫头像
代码实现效果如下:
代码实现的机器猫头像
代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业2</title>
    <style>
        #canvas{
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: 100px auto;
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="620" height="620"></canvas>
    <script>
        var canvas=document.getElementById("canvas");
        var pen=canvas.getContext("2d");
        // 开始画大脸盘子
        pen.lineWidth=5;
        pen.strokeStyle="#4d4f50";
        pen.fillStyle="#07beea";
        pen.beginPath();
        pen.arc(315,315,188,0,2*Math.PI);
        pen.closePath();
        pen.stroke();
        pen.fill();
        // 开始画腮帮子
        pen.lineWidth=3;
        pen.fillStyle="#fff";
        pen.beginPath();
        pen.arc(315,340,123,0,2*Math.PI);
        pen.closePath();
        pen.stroke();
        pen.fill();
        // 开始画眼睛
        pen.lineWidth=4;
        pen.fillStyle="#fff";
        pen.beginPath();
        pen.strokeRect(245,226,65,35)
        pen.fillRect(245,226,65,35)
        pen.strokeRect(317,226,65,35)
        pen.fillRect(317,226,65,35)
        pen.closePath();
        // 开始画眼珠子
        pen.fillStyle="#000000";
        pen.beginPath();
        pen.arc(278,243,12,0,2*Math.PI);
        pen.arc(350,243,12,0,2*Math.PI);
        pen.closePath();
        pen.fill();
        // 开始画大红鼻子
        pen.lineWidth=2;
        pen.strokeStyle="#000";
        pen.fillStyle="#ff0000";
        pen.beginPath();
        pen.arc(313,295,26,0,2*Math.PI);
        pen.closePath();
        pen.fill();
        pen.stroke();
        // 画小嘴中线
        pen.beginPath();
        pen.moveTo(313,320)
        pen.lineTo(313,420)
        pen.closePath();
        pen.stroke();
        // 画微笑
        pen.beginPath();
        pen.arc(315,330,89,30*Math.PI/180,150*Math.PI/180);
        pen.stroke();
        // 画胡须
        pen.beginPath();
        // 左边
        pen.moveTo(275,328);
        pen.lineTo(193,328);
        pen.moveTo(275,338);
        pen.lineTo(193,358);
        pen.moveTo(275,318);
        pen.lineTo(193,300);
        // 右边
        pen.moveTo(439,328);
        pen.lineTo(357,328);
        pen.moveTo(439,358);
        pen.lineTo(357,338);
        pen.moveTo(439,300);
        pen.lineTo(357,318);
        pen.closePath();
        pen.stroke();
    </script>
</body>
</html>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是代码实现: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>橙留香头像</title> </head> <body> <canvas id="avatar" width="300" height="300"></canvas> <script> const canvas = document.getElementById('avatar'); const ctx = canvas.getContext('2d'); // 头像背景 ctx.fillStyle = '#FDB813'; ctx.fillRect(0, 0, canvas.width, canvas.height); // 橙留香头像的脸 ctx.fillStyle = '#F7CBA0'; ctx.beginPath(); ctx.arc(canvas.width / 2, canvas.height / 2, 120, 0, 2 * Math.PI); ctx.fill(); // 橙留香头像的眼睛 ctx.fillStyle = 'white'; ctx.beginPath(); ctx.arc(canvas.width / 2 - 45, canvas.height / 2 - 20, 30, 0, 2 * Math.PI); ctx.fill(); ctx.beginPath(); ctx.arc(canvas.width / 2 + 45, canvas.height / 2 - 20, 30, 0, 2 * Math.PI); ctx.fill(); ctx.fillStyle = '#292F33'; ctx.beginPath(); ctx.arc(canvas.width / 2 - 45, canvas.height / 2 - 20, 15, 0, 2 * Math.PI); ctx.fill(); ctx.beginPath(); ctx.arc(canvas.width / 2 + 45, canvas.height / 2 - 20, 15, 0, 2 * Math.PI); ctx.fill(); // 橙留香头像的嘴巴 ctx.fillStyle = '#C73D3A'; ctx.beginPath(); ctx.arc(canvas.width / 2, canvas.height / 2 + 40, 50, 0.2 * Math.PI, 0.8 * Math.PI); ctx.fill(); // 橙留香头像的鼻子 ctx.fillStyle = '#292F33'; ctx.beginPath(); ctx.arc(canvas.width / 2, canvas.height / 2, 30, 0.4 * Math.PI, 0.6 * Math.PI); ctx.fill(); ctx.beginPath(); ctx.arc(canvas.width / 2, canvas.height / 2 + 10, 5, 0, 2 * Math.PI); ctx.fill(); // 橙留香头像的脖子 ctx.fillStyle = '#F7CBA0'; ctx.fillRect(canvas.width / 2 - 60, canvas.height / 2 + 100, 120, 80); // 橙留香头像的衣服 ctx.fillStyle = '#C73D3A'; ctx.beginPath(); ctx.moveTo(canvas.width / 2 - 120, canvas.height / 2 + 180); ctx.lineTo(canvas.width / 2 + 120, canvas.height / 2 + 180); ctx.lineTo(canvas.width / 2 + 100, canvas.height / 2 + 300); ctx.lineTo(canvas.width / 2 - 100, canvas.height / 2 + 300); ctx.closePath(); ctx.fill(); </script> </body> </html> ``` 效果如下: ![avatar](https://img-blog.csdnimg.cn/2021070717022814.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值