柱状图、颜色,随机生成
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: rgba(149, 175, 192, 0.3);
}
.canvas {
display: block;
margin: 30px auto;
border: 1px solid #cccccc;
background-color: rgba(199, 236, 238, 0.5);
}
</style>
</head>
<body>
<canvas class="canvas" width="700" height="700"></canvas>
<script>
let canvas = document.querySelector('canvas');
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(45, 60);
ctx.lineTo(50, 50);
ctx.lineTo(55, 60);
ctx.lineTo(50, 50);
ctx.lineTo(50, 450);
ctx.lineTo(450, 450);
ctx.lineTo(440, 445);
ctx.lineTo(450, 450);
ctx.lineTo(440, 455);
ctx.strokeStyle = 'black';
ctx.strokeWidth = 5;
ctx.stroke();
ctx.closePath();
for (let i = 0; i < 9; i++) {
let r = Math.random() * 256;
let g = Math.random() * 256;
let b = Math.random() * 256;
ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
let x = 70, y = 450, w = 20, h = -Math.random() * 400;
ctx.fillRect(x + 40 * i, y, w, h);
}
</script>
</body>
</html>