Skip to content
Advertisement

I’ll upload a WebGL rendered Canvas with toDataURL and imgurAPI, but it will be an image that will be a black image

I would appreciate any and all wisdom you can give me. Thank you in advance.

Assumptions

I wrote a Javascript game using a game engine called Phaser.js.

Issue.

  • [ ] When accessing the URL in the response, an image that is only black is displayed.

Response content from imgurAPI

{"id":"dwdGkep","title":null,"description":null,"datetime":1672621440,"type":"image/png","animated":false,"width":800,"height":800,"size":12914,"views":0,"bandwidth":0,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":0,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"445HisfrUgBgogT","name":"","link":"https://i.imgur.com/dwdGkep.png"},"success":true,"status":200}

Since “success”:true, “status”:200, the upload process itself seems to be OK.

Relevant source code

Image sending process

JavaScript

The calling part of the above process (excerpt) Like setting up an event in the create function.

JavaScript

Here is the entire code create function

JavaScript

What I tried

I tried the following for the same event, but it didn’t work.

JavaScript

Current Processing

  • [ ] Clicking an element in the Canvas on the Result screen will perform the following series of processes.
  • [ ] Convert the WebGL canvas to an image using toDataURL().
  • [ ] Convert the image data to Blob format.
  • [ ] Upload the data in Blob format with XMLHttpRequest using imgur’s API
  • [ ] Obtain the URL in the response`

Advertisement

Answer

The easy solution is to use phaser’s builtin function snapshot (link to the documentation), the resulting image should display in the correct fashion (no special configuration needed).

This function creates an snapshot-image of the current canvas and passes the taken image as a HTMLImageElement to a given callback function.

You would just have to change your code to look something like this:

JavaScript

(I tested it, on one of my local projects, and it works)

If this solution doesn’t work for you, you could open an issue on the phaser github or mention it on the discourse page, and fix might be made for the next version.

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