How to convert the canvas image "data: image / jpeg; base64, .." to a regular image file - javascript

I have

image = canvas.toDataURL("image/png", 1);

but it returns

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA.......

but I need a normal image, for example src="myimage.png"

How can I get it from the canvas? I google a lot, I can’t find what I need ...

+4
source share
1 answer

The string you get can be used as the source attribute for the image, you just need to create a new element Imagethat you can assign to it:

var image = new Image();
image.src = canvas.toDataURL("image/png", 1);
// append image to DOM

EDIT. , , . POST toDataURL . IO// , , base64, . .

: . , <img>, src:

<img src="<< base-64 encoded data string>>" />
+5

Source: https://habr.com/ru/post/1568620/


All Articles