简体   繁体   English

JavaScript精灵表与普通和视网膜屏幕配合使用

[英]JavaScript sprite sheet use with normal and retina screens

I'm looking for some advice. 我正在寻找一些建议。 I've created a space invaders game which gets the spaceship and aliens from one sprite sheet. 我创建了一个太空侵略者游戏,该游戏可以从一张精灵纸上获取飞船和外星人。 I work on a 5k retina screen and all looks well. 我在5k视网膜屏幕上工作,看起来一切都很好。

The issue is when I go onto a non-retina screen the ship/aliens sprite locations look wrong with the alien starting half way through. 问题是,当我进入非视网膜屏幕时,飞船/外星人精灵的位置看起来很错误,而外星人则从中间穿过。 So an alien shows on screen as half of one alien and half of the other. 因此,一个外星人在屏幕上显示为一个外星人的一半,另一半。

I'm putting this down to a retina issue and looking how to resolve it. 我将其归结为视网膜问题,并寻求解决方法。 The sprite image is been called through image() constructor and using on load. Sprite图片是通过image()构造函数并在加载时使用的。 I then use drawImage to set the position on the sprite sheet to select the correct position on each alien. 然后,我使用drawImage设置精灵表上的位置,以选择每个外星人上的正确位置。 So no CSS used. 因此没有使用CSS。

Do I need two sprite sheets one retina and one non-retina or are they some code that JavaScript will check what kind of screen is been used? 我需要两个子画面一张,一个视网膜和一个非视网膜,还是它们是JavaScript将检查使用哪种屏幕的代码?

Is code needed or is this explanation good enough? 是否需要代码或此解释足够好吗?

Edit 编辑

I have seen that I have made an error my browser on the non-retina screen was zoomed in at 110% this has caused the issue. 我发现我在非视网膜屏幕上的浏览器放大了110%时出错,这引起了问题。 So looks like it's with zooming the browser that the issue appears. 因此,似乎是在缩放浏览器时出现了问题。

There are a number of ways you could approach this: 您可以通过多种方法来解决此问题:

  • Create two separate sprite sheets and use media queries to apply the correct styles based on device resolution media queries. 创建两个单独的Sprite工作表,并使用媒体查询根据设备分辨率的媒体查询应用正确的样式。 This is labor intensive, so I would recommend a tool to build the sheet and generate the CSS automatically like https://github.com/sprity/sprity 这是劳动密集型的,所以我建议使用一种工具来构建工作表并自动生成CSS,例如https://github.com/sprity/sprity

  • Use a single Retina sprite sheet, like you are currently doing. 像您当前正在使用的那样,使用一张Retina Sprite表。 I suspect the issue you are having is due to some CSS, which we would need to see to debug. 我怀疑您遇到的问题是由于某些CSS,我们需要对其进行调试。

  • Use an SVG sprite https://css-tricks.com/svg-sprites-use-better-icon-fonts/ 使用SVG精灵https://css-tricks.com/svg-sprites-use-better-icon-fonts/

  • Use inline SVG, which eliminates a network request. 使用嵌入式SVG,它可以消除网络请求。

Happy to provide more details about any of the above approaches. 很高兴提供有关上述任何方法的更多详细信息。

You can try spritify tool https://www.npmjs.com/package/spritify 您可以尝试使用spritify工具https://www.npmjs.com/package/spritify

There is demo app that show how to use it https://github.com/shadiabuhilal/spritify-example 有演示应用程序演示如何使用它https://github.com/shadiabuhilal/spritify-example

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM