日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

将HTML5 Canvas的内容保存为图片

發布時間:2025/6/15 HTML 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将HTML5 Canvas的内容保存为图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要思想是借助Canvas自己的API - toDataURL()來實現,整個實現

HTML + JavaScript的代碼很簡單。

[html]?view plaincopy
  • <html>??
  • <meta?http-equiv="X-UA-Compatible"?content="chrome=1">??
  • <head>??
  • <script>??
  • ????????window.onload?=?function()?{??
  • ????????????draw();??
  • ????????????var?saveButton?=?document.getElementById("saveImageBtn");??
  • ????????????bindButtonEvent(saveButton,?"click",?saveImageInfo);??
  • ????????????var?dlButton?=?document.getElementById("downloadImageBtn");??
  • ????????????bindButtonEvent(dlButton,?"click",?saveAsLocalImage);??
  • ????????};??
  • ????????????function?draw(){??
  • ????????????????var?canvas?=?document.getElementById("thecanvas");??
  • ????????????????var?ctx?=?canvas.getContext("2d");??
  • ????????????????ctx.fillStyle?=?"rgba(125,?46,?138,?0.5)";??
  • ????????????????ctx.fillRect(25,25,100,100);???
  • ????????????????ctx.fillStyle?=?"rgba(?0,?146,?38,?0.5)";??
  • ????????????????ctx.fillRect(58,?74,?125,?100);??
  • ????????????????ctx.fillStyle?=?"rgba(?0,?0,?0,?1)";?//?black?color??
  • ????????????????ctx.fillText("Gloomyfish?-?Demo",?50,?50);??
  • ????????????}??
  • ??????????????
  • ????????????function?bindButtonEvent(element,?type,?handler)??
  • ????????????{??
  • ???????????????????if(element.addEventListener)?{??
  • ??????????????????????element.addEventListener(type,?handler,?false);??
  • ???????????????????}?else?{??
  • ??????????????????????element.attachEvent('on'+type,?handler);??
  • ???????????????????}??
  • ????????????}??
  • ??????????????
  • ????????????function?saveImageInfo?()???
  • ????????????{??
  • ????????????????var?mycanvas?=?document.getElementById("thecanvas");??
  • ????????????????var?image????=?mycanvas.toDataURL("image/png");??
  • ????????????????var?w=window.open('about:blank','image?from?canvas');??
  • ????????????????w.document.write("<img?src='"+image+"'?alt='from?canvas'/>");??
  • ????????????}??
  • ??
  • ????????????function?saveAsLocalImage?()?{??
  • ????????????????var?myCanvas?=?document.getElementById("thecanvas");??
  • ????????????????//?here?is?the?most?important?part?because?if?you?dont?replace?you?will?get?a?DOM?18?exception.??
  • ????????????????//?var?image?=?myCanvas.toDataURL("image/png").replace("image/png",?"image/octet-stream;Content-Disposition:?attachment;filename=foobar.png");??
  • ????????????????var?image?=?myCanvas.toDataURL("image/png").replace("image/png",?"image/octet-stream");???
  • ????????????????window.location.href=image;?//?it?will?save?locally??
  • ????????????}??
  • ????????</script>??
  • </head>??
  • <body?bgcolor="#E6E6FA">??
  • ????<div>??
  • ????????<canvas?width=200?height=200?id="thecanvas"></canvas>??
  • ????????<button?id="saveImageBtn">Save?Image</button>??
  • ????????<button?id="downloadImageBtn">Download?Image</button>??
  • ????</div>??
  • </body>??
  • </html>??
  • 運行效果如下:



    總結

    以上是生活随笔為你收集整理的将HTML5 Canvas的内容保存为图片的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。