请问一下老师,为什么后面的部位都继承了天线的颜色啊?

来源:3-11 编程练习

weixin_慕先生7015352

2020-10-25 19:45:59

# 具体遇到的问题

# 报错信息的截图

# 相关课程内容截图

# 尝试过的解决思路和结果

# 粘贴全部相关代码,切记添加代码注释(请勿截图)

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>canvas</title>

    <style>

        canvas{background-color:lightblue;}

    </style>

</head>

<body>

    <canvas id="canvas" width="800" height="800">

        您的浏览器不支持canvas

</canvas>

<script>

//在此处补充代码,可以将之前写好的机器人代码复制过来,也可以重新写一个


    var canvas=document.getElementById('canvas');

    var ctx=canvas.getContext('2d');

    

    // 天线

    ctx.moveTo(250,50);

    ctx.lineTo(350,200);

    ctx.lineWidth=3;

    ctx.strokeStyle='orange';

    

    ctx.moveTo(550,50);

    ctx.lineTo(450,200);

    ctx.stroke();

    // 头

    ctx.beginPath();

    ctx.strokeRect(250,200,300,150);

    ctx.fillStyle='pink';

    ctx.fillRect(250,200,300,150);

   

    // 眼睛

    ctx.beginPath();

    ctx.arc(350,250,25,0,2*Math.PI,true);

    ctx.fillStyle='black';

    ctx.stroke();

    ctx.fill();

    

    ctx.beginPath();

    ctx.arc(450,250,25,0,2*Math.PI,true);

    ctx.fillStyle='black';

    ctx.stroke();

    ctx.fill();

    // 耳朵

    ctx.beginPath();

    ctx.strokeRect(200,250,50,70);

    ctx.strokeRect(550,250,50,70);

    ctx.fillStyle='purple';

    ctx.fillRect(200,250,50,70)

    ctx.fillRect(550,250,50,70);

    

    // 嘴巴

    ctx.beginPath();

    ctx.strokeRect(350,300,100,25);

    ctx.fillStyle='red';

    ctx.fillRect(350,300,100,25);

    // 身体

    ctx.beginPath();

    ctx.strokeRect(300,350,200,200);

    ctx.fillStyle='blue';

    ctx.fillRect(300,350,200,200);

    // 手臂

    ctx.beginPath();

    ctx.strokeRect(200,400,100,50);

    ctx.fillStyle='green';

    ctx.fillRect(200,400,100,50);

    

    ctx.beginPath();

    ctx.strokeRect(500,400,100,50);

    ctx.fillStyle='green';

    ctx.fillRect(500,400,100,50);

    // 腿

     ctx.beginPath();

    ctx.strokeRect(350,550,40,100);

    ctx.fillStyle='yellow';

    ctx.fillRect(350,550,40,100);

    

    ctx.beginPath();

    ctx.strokeRect(410,550,40,100)

    ctx.fillStyle='yellow';

    ctx.fillRect(410,550,40,100);

    

</script>

</body>

</html>

在这里输入代码,可通过选择【代码语言】突出显示

写回答

1回答

好帮手慕久久

2020-10-26

同学你好,问题解答如下:

由于绘制天线时,设置了线条的颜色,而在绘制头、眼睛等部位时,又含有绘制线条的操作,比如strokeRect、stroke,但此时却没有额外设置线条颜色,导致之前的线条颜色没覆盖或修改,所以就继续使用了天线部位设置的线条颜色。

修改方式:头、眼睛等部位,直接使用fillStyle填充,不用绘制线条,具体修改,可参考如下例子:

http://img.mukewang.com/climg/5f962b8d09429a3104880218.jpg

http://img.mukewang.com/climg/5f962bc3097852f805160335.jpg

http://img.mukewang.com/climg/5f962bd909c24fb104520261.jpg

其他几个部位,同学自己调整一下。

祝学习愉快!

0

0 学习 · 6815 问题

查看课程