画布绘图和视网膜显示:可行吗?
与phoneGap 一起使用Canvas 实现绘图。我们遇到的问题是画布需要特定的像素尺寸。这很好,只是 iPhone 4 的 Retina 显示屏(从 CSS/Webkit POV 来看仍然是 320 像素宽),尽管从技术上讲实际屏幕像素有 640 个。
是否有办法在 Webkit 上使用 Canvas 来适应视网膜显示屏,同时保持与非视网膜显示屏的兼容性?
Working with phoneGap implementing drawing with Canvas. The catch we've run into is that canvas expects specific pixel dimensions. This is fine except that the iPhone 4's Retina display, from a CSS/Webkit POV is still 320px wide, even though technically there are 640 actual screen pixels.
Is there anyway to accommodate the retina display using Canvas on Webkit while preserving compatibility with non-retina displays?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
上周我遇到了同样的问题,并发现了如何解决它 -
gist 上的完整代码,< a href="http://jsfiddle.net/4JH75/" rel="noreferrer">jsfiddle 上的演示
I sat with the same problem last week and discovered how to solve it -
Full code on gist, demo on jsfiddle
有一个内置的 polyfill 可以为您处理最基本的绘图操作,并消除自动为您处理此操作的浏览器(safari)和其他不自动处理此操作的浏览器之间的歧义。
https://github.com/jondavidjohn/hidpi-canvas-polyfill
您只需包含它即可在您的绘图代码之前,它应该自动为您提供良好的视网膜支持。
There is a drop-in polyfill that will take care of most basic drawing operations for you, and remove the ambiguity between browsers that handle this automatically for you (safari) and others that don't.
https://github.com/jondavidjohn/hidpi-canvas-polyfill
You simply include it before your drawing code and it should give you decent retina support automatically.
另一种选择是从 HTML 中删除此行,您将获得在手机上查看网页的默认宽度。 iPhone 上应为 980 像素。
然后你就可以这样做:
优点是不需要缩放并且渲染速度更快。
但它可能与设备拥有的物理像素数量不匹配。但在大多数情况下它运作良好。并非所有像素化都像 320px。但不如视网膜那么清晰。请记住,分辨率越高,延迟就越多。
Another option is to remove this line from your HTML you will get the default width for viewing a webpage on your phone. Should be 980px on iPhones.
Then you can just do:
The advantage is that you don't need to scale and it renders faster.
But it probably won't match the amount of physical pixels that the device has. It works well in most cases though. Not all pixellated like 320px. But not as clear as retina. Keep in mind that the higher res you go the more laggy it will be.
WebCode (http://www.webcodeapp.com) 是一款矢量绘图应用程序,可为您生成 JavaScript HTML5 Canvas 代码。该代码与 Retina 兼容,您可以查看他们是如何做到的。
WebCode (http://www.webcodeapp.com) is a vector drawing app that generates JavaScript HTML5 Canvas code for you. The code is Retina-compatible, you can check out how they do it.
TJ Holowaychuk 有一个非常好的 Polyfill:
https://www.npmjs.com/package/autoscale -画布
There is a very good polyfill by TJ Holowaychuk:
https://www.npmjs.com/package/autoscale-canvas
编辑:刚刚注意到我发布了错误的演示链接!
Retina(或其他 hdpi 显示器)画布分辨率绝对是可能的。这里有一个工作示例:
http://spencer-evans.com/share/github /canvas-resizer/
我也遇到过这个问题几次。接受的答案代码基本上是正确的,但您也可以使用库解决方案。我制作了一个来处理智能画布调整大小,以使元素更具响应性和更高的分辨率(如演示中所示)。
https://github.com/swevans/canvas-resizer
EDIT: Just noticed I posted the wrong link for the demo!
Retina (or other hdpi display) canvas resolution is definitely possible. There is a working example here:
http://spencer-evans.com/share/github/canvas-resizer/
I've also bumped into this a few times. The accepted answer code is essentially correct, but you could also use a library solution. I whipped one up to handle intelligent canvas re-sizing to make the element more responsive and higher resolution (as shown in the demo).
https://github.com/swevans/canvas-resizer