save image to a png file using processing.js

孤人 提交于 2019-12-04 15:57:53

问题


I have an small drawing application made in processing.js. I would like to save the image made using the application to a server using a php file (no problem on this). I know the canvas element has a method to get that content, canvas.toDataURL() but I dont know how to do the same with processing.js

Any hint?

Cheers!


回答1:


perhaps

var image = document.getElementsByTagName("canvas")[0].toDataURL();



回答2:


So when you use saveFrame() in processing.js. The image opens in a new window and i found it hard to intercept the data. The soultion is to get the image from the canvas in javascript.

// this grabs the canvas and turns it into a base64 image    
var image = document.getElementsByTagName("canvas")[0].toDataURL();

// Log the data to the canvas to check it working
console.log(image);

// then you can place the image on your web page    
document.getElementById("theImage").src=image;

Then the html is easy. Just add

<img id="theImage"></img> 

If you want to upload the image to a server you can acsess the data in js with.

image

Note that you can also use style display:none; and have the canvas render image for you without the canvas displaying. It also supports transparent pngs and the canvas by default is transparent



来源:https://stackoverflow.com/questions/5078015/save-image-to-a-png-file-using-processing-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!