简体   繁体   English

为什么在使用requestAnimationFrame移动精灵时,精灵会变得模糊?

[英]Why is my sprite blurred when using requestAnimationFrame to move it?

I created a basic 2D game using pure JavaScript. 我使用纯JavaScript创建了一个基本的2D游戏。 My problem is when the sprite stops moving it is sometimes blurred sometimes not. 我的问题是,当精灵停止移动时,有时会模糊,有时不会。 I use requestAnimationFrame to move the sprite and keydown/keyup events trigger moving. 我使用requestAnimationFrame移动精灵,而keydown/keyup事件触发移动。 When keyup is triggered the sprite stops moving and the default tile is set however sometimes it is showing blurred. 触发抠像后,精灵将停止移动,并设置了默认图块,但有时显示为模糊。 https://arpadvas.github.io/untitled_game_project/ https://arpadvas.github.io/untitled_game_project/

Since you didn't posted an minimal code example, and that I don't want to go through the raw code you linked to, this will stay as a guess (an educated one). 由于您没有发布一个最小的代码示例,并且我不想浏览您链接到的原始代码,因此这只是一个猜测(有教养的一个)。

Generally, this happens when you draw your sprites on floating coordinates. 通常,在浮动坐标上绘制精灵时会发生这种情况。
Since there is no way to draw on half a pixel, the pixel being the smallest unit in canvas, to smoothen drawings, the browser will by default create antialias artifacts, turning some pixels less opaque so that your eyes think it is on half a pixel. 由于无法绘制半个像素(该像素是画布中的最小单位)来平滑绘图,因此浏览器默认会创建抗锯齿伪像,使某些像素的不透明度降低,从而使您的眼睛认为它位于半个像素上。 While this usually works well with realistic photographs, it doesn't at all with pixel-art. 尽管这通常适用于逼真的照片,但对于像素艺术却根本不适用。

The solution then is either to round all your coordinates, or if you are lazy, to set the imageSmoothingEnabled property of your context. 然后的解决方案是舍入所有坐标,或者如果您很懒,则设置上下文的imageSmoothingEnabled属性。

 var img = new Image(); img.onload = draw; function draw(){ i = .316252; blurred.width = round.width = noAntiAlias.width = img.width +20; blurred.height = round.height = noAntiAlias.height = img.height +20; blurred.getContext('2d').drawImage(img, 10+i, 20+i); round.getContext('2d').drawImage(img, 10, 20); var nA = noAntiAlias.getContext('2d'); nA.mozImageSmoothingEnabled = nA.msImageSmoothingEnabled = nA.webkitImageSmoothingEnabled = nA.imageSmoothingEnabled = false; noAntiAlias.getContext('2d').drawImage(img, 10+i, 20+i); }; img.src = "https://dl.dropboxusercontent.com/s/4e90e48s5vtmfbd/aaa.png"; 
 <canvas id="blurred"></canvas> <canvas id="round"></canvas> <canvas id="noAntiAlias"></canvas> 

Ps : this made me realize that somehow my FF doesn't smooth this particular image I used... If someone can confirm in comments, I'd be glad to dig into this further if needed. 附言:这让我意识到我的FF无法使我使用的特定图像变得平滑...如果有人可以在评论中进行确认,我将很高兴在需要时对此进行深入研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在使用requestAnimationFrame时使用此表达式 - why is this expression used when using requestAnimationFrame 为什么使用requestAnimationFrame时图像消失了? - Why does image disappear when using requestAnimationFrame? 为什么我的精灵/实体不会直线移动? - Why won't my sprite/entity move in a straight line? 当我离开选项卡时,requestAnimationFrame() 不会在 Firefox 停止吗? - Does requestAnimationFrame() not stop in Firefox when I move away from the tab? 使用 requestAnimationFrame 时应该如何组织更新/绘制逻辑? - How should I organize my update/draw logic when using requestAnimationFrame? 使用requestAnimationFrame - using requestAnimationFrame 使用鼠标坐标制作动画时的requestAnimationFrame - requestAnimationFrame when using mouse cordinates to make animation 什么时候使用 requestAnimationFrame? 如果我们有 css 过渡,为什么我们需要 requestAnimationFrame ? - When to use requestAnimationFrame? and why we need requestAnimationFrame if we have css transition? 为什么我的多个画布动画(requestAnimationFrame)仅对一个元素起作用? - Why my multiple canvas animation(requestAnimationFrame) is working only for one element? 垂直滚动时水平移动和水平设置精灵动画 - move and animate sprite horizontally when scrolling vertically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM