I am trying to capture a screenshot of this game but when I try it simply return a blank image.
var data = document.getElementsByTagName("canvas")[0].toDataURL('image/png');
var out = document.createElement('img');
out.id = "sc";
out.src = data;
var link = document.createElement("a");
link.download = "sc.png";
link.href = out.src;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
The image downloads perfectly fine but it is simply blank.
I have tried a number of other methods including just printing the base64 to the console with console.log(document.querySelector("#openfl-content > canvas").toDataURL("image/png").split(',')[1]); but when I put it into a decoder its blank once again.
EDIT: The above code works perfectly fine on my MacBook but does not work on my Windows PC regardless of the browser. I have no idea why.
Your original code wasn't giving you the output you expected, for a couple of reasons.
Reason 1
You are not appending the
outchild to the DOM - there is nodocument.body.appendChild(out).Or you can avoid creating an
imgelement, and just usea, with itshrefset to the base64 image.Reason 2 (and a working solution)
However, the previous will work under certain conditions:
The following code addresses both of these issues. I'm also using your initial coding style -
varfor variables (instead of,constorlet), Javascript specific selectors (instead of a genericquerySelector(<selector>))Let's optimize
While this is working, it's actually not a very good way of doing things - we're repeating some bits of our code. This would be one way of optimizing: