我在绘制切片地图并将切片宽度设置为$(window).width() / 10,将切片高度设置为$(window).height() / 10时遇到问题
画布在每个瓷砖之间绘制额外的线条
发布于 2020-01-04 01:26:31
下面是一个画布尺寸为500x503的示例。注意高度不能被10整除,所以我们得到了错误的背景水平线。而宽度可以被10整除,我们看不到这样的垂直线。
let canvas = document.querySelector('canvas');
let ctx = canvas.getContext("2d");
let width = canvas.width / 10;
let height = canvas.height / 10;
console.log(width, height);
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let x = 0; x < 10; x++)
for (let y = 0; y < 10; y++) {
ctx.fillStyle = (x + y) % 2 ? '#ffe' : '#eef';
ctx.fillRect(x * width, y * height, width, height);
}canvas {
outline: 1px solid;
}<canvas width=500 height=503></canvas>
https://stackoverflow.com/questions/59582502
复制相似问题