3

I have a canvas #mycanvas which contains an image. I want to create a blob out of that image, preferably as a jpeg. Here is how i create the blob

document.getElementById('mycanvas').toDataURL("image/jpeg").replace(/^data:image\/(png|jpeg);base64,/, "")

How do i recreate the image from this blob, and show it in #mycanvas again?

Anton Gildebrand
  • 3,641
  • 12
  • 50
  • 86
  • 1
    Check out the answer here http://stackoverflow.com/questions/4773966/drawing-an-image-from-a-data-url-to-a-canvas – Sergiu Paraschiv Aug 24 '13 at 14:08
  • 2
    May I suggest that the word "blob" is probably a little misleading ? As shown on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) the right way to convert a `` content into a `Blob` is to use the `.toBlob()` method, not at all `.toDataURL()`. In fact your question should have probably been _HTML5 canvas, save jpeg __data URL__ and restore to canvas from __data URL__._ What do you think ? – danidemi Aug 03 '16 at 16:51
  • 1
    A data URL is a base64 representation of the image not a Blob. As danidemi pointed out. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob and https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL – Björn Feb 22 '17 at 14:09

3 Answers3

4

Here's how i solved my problem

function blob2canvas(canvas,blob){
    var img = new Img;
    var ctx = canvas.getContext('2d');
    img.onload = function () {
        ctx.drawImage(img,0,0);
    }
    img.src = blob;
}

The blob was received when calling canvas.toDataURL("image/jpeg")

Anton Gildebrand
  • 3,641
  • 12
  • 50
  • 86
2

Anton's answer no longer works as it is. You need this syntax now.

function blob2canvas(canvas,blob){
    var img = new window.Image();
    img.addEventListener("load", function () {
        canvas.getContext("2d").drawImage(img, 0, 0);
    });
    img.setAttribute("src", blob);
}
0

Restore to Canvas from Blob sample taken from: https://googlechrome.github.io/samples/image-capture/grab-frame-take-photo.html

// ms is a MediaStreamPointer
let imageCapture = new ImageCapture(ms.getVideoTracks()[0]);

                imageCapture.takePhoto()
                    .then(blob => createImageBitmap(blob))
                    .then(imageBitmap => {
                        const canvas = document.getElementById('canvas')
                        drawCanvas(canvas, imageBitmap);
                    })

function drawCanvas(canvas, img) {
    canvas.width = getComputedStyle(canvas).width.split('px')[0];
    canvas.height = getComputedStyle(canvas).height.split('px')[0];
    let ratio = Math.min(canvas.width / img.width, canvas.height / img.height);
    let x = (canvas.width - img.width * ratio) / 2;
    let y = (canvas.height - img.height * ratio) / 2;
    canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
    canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height,
        x, y, img.width * ratio, img.height * ratio);
}
patrick
  • 16,091
  • 29
  • 100
  • 164