david-pfx / PuzzleScriptNext

A direct successor to PuzzleScript, building on the work of PuzzleScript Plus, Pattern:Script and others.
http://www.puzzlescriptnext.polyomino.com
MIT License
15 stars 6 forks source link

Support loading images via URL #129

Open IrregularHex opened 2 days ago

IrregularHex commented 2 days ago

Check ([ ] -> [x]) all that apply, then describe the issue below:

I wanted to add support for using full resolution images / spritesheets. A simple way to get this working was to support loading and drawing from a URL like this:

load_images my_sprites=https://[...]/sprites.png

========
OBJECTS
========
Player canvas:
{"drawImage":["my_sprites",   0,   0, 64, 64, 0, 0, 1, 1]}

I based the load_images command on how custom_font currently works, and I special-cased the canvas drawImage command to use the images you loaded earlier (currently any drawImage command fails with an error because the image argument is passed as a string).

The main downside of this approach is that you have to host the images online somewhere. If you try to load them from a local file, Chrome will give you a CORS error (anecdotally, Safari seems to be less picky). One solution is to upload the images to a GitHub repo, or you also could try running a local server which sets the right CORS headers for images (Python's simple HTTP server does not set these headers, so it doesn't work).

So the workflow is a bit annoying, but it does work :)

An different approach might be to have PuzzleScript pop up a file dialog where you can import your image, and it would be saved to localstorage. Then when you export the game, it would be written back out to an image file next to the html file. This would be a lot harder to implement, though.

david-pfx commented 20 hours ago

That's cute! Definitely worth doing, if not too hard. Worth a chat on the PSNext thread, and see if anyone else has ideas...