How to save an HTML5 Canvas as an image on a server?

后端 未结 8 2222
走了就别回头了
走了就别回头了 2020-11-21 23:50

I\'m working on a generative art project where I would like to allow users to save the resulting images from an algorithm. The general idea is:

  • Create an image
8条回答
  •  天涯浪人
    2020-11-22 00:23

    I played with this two weeks ago, it's very simple. The only problem is that all the tutorials just talk about saving the image locally. This is how I did it:

    1) I set up a form so I can use a POST method.

    2) When the user is done drawing, he can click the "Save" button.

    3) When the button is clicked I take the image data and put it into a hidden field. After that I submit the form.

    document.getElementById('my_hidden').value = canvas.toDataURL('image/png');
    document.forms["form1"].submit();
    

    4) When the form is submited I have this small php script:

    
    

提交回复
热议问题