我想让它在左下角
const width = cvs.width
ctx.fillStyle = 'red'
ctx.fillRect(0, height - 10, 20, 10)
画两个接起来,然后用变量来标识它们,然后找规律。
多弄几个再找。
w = 20
h = 10
ctx.fillStyle = 'red'
ctx.fillRect(0, height - h, w, h)
ctx.fillRect(0, height - 4 * h - 6, w, h)
用循环写出来。
let w = 20
let h = 10
let margin = 2 // 间隔
let size = 4 // 方块数目
ctx.fillStyle = 'red'
for (let i = 0; i <= size; i++) {
ctx.fillRect(0, height - (i + 1) * h - margin * i, w, h)
}
假设有一个 255 以内的随机数据,我们要映射到一个 20 的范围,获取百分比,乘以最大值即可。
let maxSize = 20 // -> 255
currentSize = Math.ceil((ramdom / 255) * maxSize)
ctx.clearRect(0, 0, width, height)
for (let i = 0; i <= currentSize; i++) {
ctx.fillRect(0, height - (i + 1) * h - margin * i, w, h)
}
requestAnimationFrame(Run)
}