How to save an image from canvas

前端 未结 4 574
借酒劲吻你
借酒劲吻你 2020-12-17 01:07

Recently I\'ve been attempting to create a photo booth by relying on Webrtc and have nearly completed all of the code except I\'ve not been able to figure out a way to save

4条回答
  •  抹茶落季
    2020-12-17 01:48

    Regarding the first task, you can export the canvas content to an image with toDataUrl method supported by the canvas object.

    var canvas = document.getElementById("canvas");
    if (canvas.getContext) {
        var ctx = canvas.getContext("2d");                // Get the context for the canvas.
        var myImage = canvas.toDataURL("image/png");      // Get the data as an image.
    }
    
    var image = document.getElementById("screenshot");  // Get the image object.
    image.src = myImage; 
    

提交回复
热议问题