Libon

用 Canvas 生成图片并下载

#HTML#canvas
使用 Canvas 的 toBlob 方法将画布内容生成图片并下载

ToC

const canvas = document.createElement('canvas')

// do something...

canvas.toBlob(function(blob) {
  var a = document.createElement("a");
  document.body.appendChild(a);
  a.download = "img" + ".jpg";
  a.href = window.URL.createObjectURL(blob);

  a.click();
  document.body.removeChild("a");
  window.URL.revokeObjectURL(blob)
})

CD ..