Skip to content
Advertisement

Download pixel art drawing as .png in PhaserJS

I need to download the created pixel drawing from this Phaser example as a .png image via FilesSaver.js but the canvas returns null.

Error:

Uncaught TypeError: Cannot read properties of null (reading ‘toBlob’)

This is the save function:

JavaScript

drawingArea: (PhaserJS 2)

JavaScript

How to get the data of the drawing to create a .png out of it?

Advertisement

Answer

Well I don’t think the canvas has the ID canvas. That’s why, I asume that is the reason for the null Error. In any case I took the original example code, as a basis for this working solution.

Disclaimer: This Code will only create a image from the “drawn-image”, not the whole UI.

Main idea, On Save:

  1. create a new canvas
  2. draw the target area into the new canvas
  3. create the image, with filesave.js

Info: I’m getting information/values from the the globally defined variables canvasGrid and canvas, if your code, doesn’t contain them, this code will not work.

I hope this helps.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement