Home  >  Article  >  Web Front-end  >  How to save the image generated by canvas in js

How to save the image generated by canvas in js

亚连
亚连Original
2018-06-14 10:46:023761browse

Below I will share with you a js method to generate a picture from canvas and save it, or directly save a picture. It has a good reference value and I hope it will be helpful to everyone.

Save the canvas array

function downLoadImage(canvas,name) {
  var a = document.createElement("a");
  a.href = canvas.toDataURL();
  a.download = name;
  a.click();
}

canvas: The dom object passed into the canvas

name: The name of the saved picture

How to save the picture directly

function downLoadImage(img,name) {
  var a = document.createElement("a");
  a.href = img.src;
  a.download = name;
  a.click();
}

img :DOM object of the picture

name:The name when saving as a picture

The above is what I compiled for everyone , I hope it will be helpful to everyone in the future.

Related articles:

Using JS to calculate the problem of buying 100 chickens

Implementing DOM attribute binding in Angular4

How to implement JSON tree using Vue2.x

Tutorial on the installation and use of CLI in Angular4

The above is the detailed content of How to save the image generated by canvas in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn