这可能吗?
我正在尝试将一位老教授的演示游戏移植为可在网络上玩的格式以供娱乐,并且他已在 XPM 格式。
有没有办法将 XPM 文件直接加载到 HTML5 画布中?我可能可以将它们加载到图像编辑器中并进行转换......但我宁愿尽可能忠实于原始来源。
Is this possible?
I am trying to port an old professor's demo-game into a web-playable format for fun, and he had setup all the graphics in the XPM format.
Is there some way to load XPM files directly into an HTML5 canvas? I could probably get by with loading them into an image editor and converting...but I'd rather stay as true to the original source as possible.
发布评论
评论(3)
您可能可以在 JavaScript 中为 XPM 编写某种解析器,并使用 对这个问题的类似方法,但是我认为使用类似 ImageMagick 和 做一个关闭转换:
You could probably write some sort of parser for XPM in JavaScript and render canvas pixels using a similar approach to this question, however I think it'd be more efficient just to use something like ImageMagick and do a one off conversion:
我制作了一个小插件来做到这一点,还有很多需要改进的地方,但也许它可以帮助你......你可以在这里看到演示:http://cortezcristian.com.ar/xpm2canvas/
您还可以在这个小提琴中玩演示:http://jsfiddle.net/crisboot/aXt3G/
I made a little plugin to do this, there's a lot to improve but maybe it can help you... you can see the demo here: http://cortezcristian.com.ar/xpm2canvas/
You can also play with the demo in this fiddle: http://jsfiddle.net/crisboot/aXt3G/
IIRC,此类上下文中
canvas
元素的渲染上下文依赖于操作嵌入img
标记的src
属性。因此,大概只有当相关浏览器支持 XPM 文件时,它们才有可能工作。检查这一点的最佳方法是对其进行测试。此问题的接受答案包含一些应该有所帮助的代码:
在画布上导入图像 html5
IIRC, the rendering context for a
canvas
element in such a context relies on manipulating thesrc
attribute of an embeddedimg
tag. As such, presumably XPM files only stand a chance of working if the browser in question supports them.The best way to check this would be to test it. The accepted answer for this question contains some code that should help:
importing image on canvas html5