Home >Web Front-end >H5 Tutorial >How to put pictures in HTML5 Canvas and save them as pictures
This article mainly introduces the method of placing pictures in HTML5 Canvas and saving them as pictures. Especially saving the picture content as pictures is a very practical function. Friends who need it can refer to it
Use JavaScript to copy the image into the canvas
To put the image into the canvas, we use the drawImage method of the canvas element:
// Converts image to canvas; returns new canvas element function convertImageToCanvas(image) { var canvas = document.createElement("canvas"); canvas.width = image.width; canvas.height = image.height; canvas.getContext("2d").drawImage(image, 0, 0);</p> <p> return canvas; }
0 here, 0 parameter coordinate point on the canvas, the image will be copied to this place.
Use JavaScript to save the canvas into an image format
If your work on the canvas has been completed, you can use the following simple method to convert the canvas data into an image format :
// Converts canvas to an image function convertCanvasToImage(canvas) { var image = new Image(); image.src = canvas.toDataURL("image/png"); return image; }
This code can magically convert canvas into PNG format!
These techniques for converting between images and canvases may be much simpler than you think.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
HTML5 and JS to implement local image cropping and uploading functions
Image compression through HTML5 mobile development Upload function
The above is the detailed content of How to put pictures in HTML5 Canvas and save them as pictures. For more information, please follow other related articles on the PHP Chinese website!