【发布时间】:2018-05-13 15:19:28
【问题描述】:
我在学校项目的画布上绘制精灵时遇到问题。我的代码:
treeImage = new Image();
treeImage.src = "sprites/treeSprites.png";
function rocks() { //to create the rock
this.x = 1920 * Math.random(); //random location on the width of the field
this.y = ground[Math.round(this.x/3)]; //ground is an array that stores the height of the ground
this.draw = function() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(Math.tan((ground[Math.floor(this.x/3)]-ground[Math.floor(this.x/3)+1])/-3));
//^rotating based on its position on the ground^
ctx.drawImage(treeImage, 200, 50, 50, 50, -25, -50, 50, 50);
ctx.restore();
}
}
len = rockArray.length; //every frame
for (var i = 0;i<len;i++) {
rockArray[i].draw();
}
我只从图像中请求 50×50px。正好在 50×50 之外有黑线(不应该干扰,因为我只要求黑线内的正方形)但是当我画岩石时,黑色轮廓是可见的。 (由于其他原因,我无法删除黑线。)
我猜当我加载图像时 JavaScript 存储的图像变得模糊,然后当我从图像中请求该部分时,周围的线条也是可见的,因为模糊将线条“扩散”到我的正方形中请求。
有什么办法可以防止这种情况发生吗?
【问题讨论】:
-
好像是防锯齿的。
-
您使用的是高 dpi 显示器(如 4k 或视网膜)吗?另外,尝试调整 yohr 坐标。
-
@Kaiido 1920*1080,这也是我画布的大小,由于 Ceres 的回答,我现在正在四舍五入所有坐标
标签: javascript image image-processing canvas drawimage