老师,这样可以吗?颜色不一样呢
来源: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回答
同学你好,代码可做如下优化:
天线是有宽度的,可以设置lineWidth,如下:
脑袋的背景色可以改一下:
同学再试一下效果。
如果我的回答帮到了你,欢迎采纳,祝学习愉快!
好帮手慕久久
2020-09-27
同学你好,颜色值接近就可以了,老师给你改的这个颜色,要比你自己写的那个接近些,不用纠结这个颜色,效果正确就可以了。
祝学习愉快!
相似问题