老师,这样可以吗?颜色不一样呢

来源:3-11 编程练习

unbreakable_全栈

2020-09-25 18:56:22

<!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(300, 50)

        ctx.lineTo(350, 200)

        ctx.strokeStyle = 'yellow'

        ctx.stroke()

        ctx.beginPath();

        ctx.moveTo(500, 50)

        ctx.lineTo(450, 200)

        ctx.stroke()

        ctx.beginPath();

        ctx.fillStyle = 'orange'

        ctx.fillRect(300, 200, 200, 100) // 绘制一个矩形的边框

        ctx.fillStyle = 'yellow'

        ctx.fillRect(290, 220, 10, 20) // 绘制一个矩形的边框 耳朵

        ctx.fillStyle = 'yellow'

        ctx.fillRect(500, 220, 10, 20) // 绘制一个矩形的边框 耳朵

        ctx.arc(335, 230, 10, 0, 2*Math.PI, true) // 参数:圆心,圆心,半径,起始弧度,终止弧度,顺时针/逆时针

        ctx.fillStyle = '#000'

        ctx.fill()

        ctx.beginPath(); 

        ctx.arc(455, 230, 10, 0, 2*Math.PI, true) // 参数:圆心,圆心,半径,起始弧度,终止弧度,顺时针/逆时针

        ctx.fillStyle = '#000'

        ctx.fill()

        ctx.beginPath(); 


        ctx.fillStyle = '#F00'

        ctx.fillRect(380, 260, 40, 20) // 绘制一个矩形的边框 嘴巴

        ctx.fill()


        ctx.fillStyle = 'pink'

        ctx.fillRect(338, 300, 130, 200) // 绘制一个矩形的边框 肚子


        ctx.fillStyle = 'green'

        ctx.fillRect(178, 370, 160, 30) // 绘制一个矩形的边框

        ctx.fillStyle = 'green'

        ctx.fillRect(468, 370, 160, 30) // 绘制一个矩形的边框

        ctx.fillStyle = 'green'

        ctx.fillRect(360, 500, 30, 160) // 绘制一个矩形的边框

        ctx.fillStyle = 'green'

        ctx.fillRect(420, 500, 30, 160) // 绘制一个矩形的边框

    </script>

</body>


</html>


写回答

2回答

好帮手慕久久

2020-09-25

同学你好,代码可做如下优化:

  1. 天线是有宽度的,可以设置lineWidth,如下:

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

  2. 脑袋的背景色可以改一下:

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

同学再试一下效果。

如果我的回答帮到了你,欢迎采纳,祝学习愉快!

0
hnbreakable_全栈
h 确定脑袋的背景颜色修改成这个颜色吗
h020-09-27
共1条回复

好帮手慕久久

2020-09-27

同学你好,颜色值接近就可以了,老师给你改的这个颜色,要比你自己写的那个接近些,不用纠结这个颜色,效果正确就可以了。

祝学习愉快!

0

0 学习 · 6815 问题

查看课程