将 XPM 图形导入 HTML5 画布

发布于 2024-11-16 09:42:54 字数 230 浏览 0 评论 0 原文

这可能吗?

我正在尝试将一位老教授的演示游戏移植为可在网络上玩的格式以供娱乐,并且他已在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_蜘蛛 2024-11-23 09:42:54

您可能可以在 JavaScript 中为 XPM 编写某种解析器,并使用 对这个问题的类似方法,但是我认为使用类似 ImageMagick做一个关闭转换

mogrify -format png *.xpm

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:

mogrify -format png *.xpm
过去的过去 2024-11-23 09:42:54

我制作了一个小插件来做到这一点,还有很多需要改进的地方,但也许它可以帮助你......你可以在这里看到演示:http://cortezcristian.com.ar/xpm2canvas/

您还可以在这个小提琴中玩演示:http://jsfiddle.net/crisboot/aXt3G/

<script src="./js/libs/jquery-1.7.1.min.js"></script>
  <script src="./js/jquery.xpm2canvas.js"></script>
  <script>
        var pseudoXMP = [
            /* <Values> */
            /* <width/cols> <height/rows> <colors> <char on pixel>*/
            "40 40 6 1",
            /* <Colors> */
            "     c none",
            ".    c #ffffff",
            "X    c #dadab6",
            "o    c #6c91b6",
            "O    c #476c6c",
            "+    c #000000",
            /* <Pixels> */
            "                                        ",
            "                                        ",
            "                                        ",
            "        .      .X..XX.XX      X         ",
            "        ..   .....X.XXXXXX   XX         ",
            "        ... ....X..XX.XXXXX XXX         ",
            "   ..   ..........X.XXXXXXXXXXX   XX    ",
            "   .... ........X..XX.XXXXXXXXX XXXX    ",
            "   .... ..........X.XXXXXXXXXXX XXXX    ",
            "   ooOOO..ooooooOooOOoOOOOOOOXX+++OO++  ",
            "   ooOOO..ooooooooOoOOOOOOOOOXX+++OO++  ",
            "   ....O..ooooooOooOOoOOOOOOOXX+XXXX++  ",
            "   ....O..ooooooooOoOOOOOOOOOXX+XXXX++  ",
            "   ..OOO..ooooooOooOOoOOOOOOOXX+++XX++  ",
            "    ++++..ooooooooOoOOOOOOOOOXX+++ +++  ",
            "     +++..ooooooOooOOoOOOOOOOXX+++  +   ",
            "      ++..ooooooooOoOOOOOOOOOXX+++      ",
            "        ..ooooooOooOOoOOOOOOOXX+++      ",
            "        ..ooooooooOoOOOOOOOOOXX+++      ",
            "        ..ooooooOooOOoOOOOOOOXX+++      ",
            "        ..ooooooooOoOOOOOOOOOXX+++      ",
            "         ..oooooOooOOoOOOOOOXX+++       ",
            "         ..oooooooOoOOOOOOOOXX+++       ",
            "          ..ooooOooOOoOOOOOXX+++        ",
            "          ..ooooooOoOOOOOOOXX++++       ",
            "        ..o..oooOooOOoOOOOXX+XX+++      ",
            "       ...o..oooooOoOOOOOXX++XXX++      ",
            "      ....OO..ooOooOOoOOXX+++XXXX++     ",
            "     ...oo..+..oooOoOOOXX++XXooXXX++    ",
            "    ...ooo..++..OooOOoXX+++XXooOXXX+    ",
            "   ..oooOOXX+++....XXXX++++XXOOoOOXX+   ",
            "   ..oooOOXX+++ ...XXX+++++XXOOooOXX++  ",
            "   ..oooOXXX+++  ..XX+++  +XXOOooOXX++  ",
            "   .....XXX++++             XXXXXXX++   ",
            "    ....XX++++              XXXXXXX+    ",
            "     ...XX+++                XXXXX++    ",
            "                                        ",
            "                                        ",
            "                                        ",
            "                                        "];
        $(document).ready(function(){
            $('#xmp2canvas').xpm2canvas({xpm:pseudoXMP});
        });
  </script>

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/

<script src="./js/libs/jquery-1.7.1.min.js"></script>
  <script src="./js/jquery.xpm2canvas.js"></script>
  <script>
        var pseudoXMP = [
            /* <Values> */
            /* <width/cols> <height/rows> <colors> <char on pixel>*/
            "40 40 6 1",
            /* <Colors> */
            "     c none",
            ".    c #ffffff",
            "X    c #dadab6",
            "o    c #6c91b6",
            "O    c #476c6c",
            "+    c #000000",
            /* <Pixels> */
            "                                        ",
            "                                        ",
            "                                        ",
            "        .      .X..XX.XX      X         ",
            "        ..   .....X.XXXXXX   XX         ",
            "        ... ....X..XX.XXXXX XXX         ",
            "   ..   ..........X.XXXXXXXXXXX   XX    ",
            "   .... ........X..XX.XXXXXXXXX XXXX    ",
            "   .... ..........X.XXXXXXXXXXX XXXX    ",
            "   ooOOO..ooooooOooOOoOOOOOOOXX+++OO++  ",
            "   ooOOO..ooooooooOoOOOOOOOOOXX+++OO++  ",
            "   ....O..ooooooOooOOoOOOOOOOXX+XXXX++  ",
            "   ....O..ooooooooOoOOOOOOOOOXX+XXXX++  ",
            "   ..OOO..ooooooOooOOoOOOOOOOXX+++XX++  ",
            "    ++++..ooooooooOoOOOOOOOOOXX+++ +++  ",
            "     +++..ooooooOooOOoOOOOOOOXX+++  +   ",
            "      ++..ooooooooOoOOOOOOOOOXX+++      ",
            "        ..ooooooOooOOoOOOOOOOXX+++      ",
            "        ..ooooooooOoOOOOOOOOOXX+++      ",
            "        ..ooooooOooOOoOOOOOOOXX+++      ",
            "        ..ooooooooOoOOOOOOOOOXX+++      ",
            "         ..oooooOooOOoOOOOOOXX+++       ",
            "         ..oooooooOoOOOOOOOOXX+++       ",
            "          ..ooooOooOOoOOOOOXX+++        ",
            "          ..ooooooOoOOOOOOOXX++++       ",
            "        ..o..oooOooOOoOOOOXX+XX+++      ",
            "       ...o..oooooOoOOOOOXX++XXX++      ",
            "      ....OO..ooOooOOoOOXX+++XXXX++     ",
            "     ...oo..+..oooOoOOOXX++XXooXXX++    ",
            "    ...ooo..++..OooOOoXX+++XXooOXXX+    ",
            "   ..oooOOXX+++....XXXX++++XXOOoOOXX+   ",
            "   ..oooOOXX+++ ...XXX+++++XXOOooOXX++  ",
            "   ..oooOXXX+++  ..XX+++  +XXOOooOXX++  ",
            "   .....XXX++++             XXXXXXX++   ",
            "    ....XX++++              XXXXXXX+    ",
            "     ...XX+++                XXXXX++    ",
            "                                        ",
            "                                        ",
            "                                        ",
            "                                        "];
        $(document).ready(function(){
            $('#xmp2canvas').xpm2canvas({xpm:pseudoXMP});
        });
  </script>
嘿咻 2024-11-23 09:42:54

IIRC,此类上下文中 canvas 元素的渲染上下文依赖于操作嵌入 img 标记的 src 属性。因此,大概只有当相关浏览器支持 XPM 文件时,它们才有可能工作。

检查这一点的最佳方法是对其进行测试。此问题的接受答案包含一些应该有所帮助的代码:

在画布上导入图像 html5

IIRC, the rendering context for a canvas element in such a context relies on manipulating the src attribute of an embedded img 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文