使用 todataurl() 方法获取画布的图像数据url。它将绘图(画布)转换为64位编码的png url。
示例您可以尝试运行以下代码将画布保存为图像 −
<!doctype html><html> <head> </head> <body> <canvasid = "newcanvas" width = "450" height = "300"></canvas> <script> var canvas = document.getelementbyid('newcanvas'); var ctx = canvas.getcontext('2d'); // draw any shape ctx.beginpath(); ctx.moveto(120, 50); ctx.beziercurveto(130,100, 130, 250, 330, 150); ctx.beziercurveto(350,180, 320, 180, 240, 150); ctx.beziercurveto(320,150, 420, 120, 390, 100); ctx.beziercurveto(130,40, 370, 30, 240, 50); ctx.beziercurveto(220,7, 350, 20, 150, 50); ctx.beziercurveto(250,5, 150, 20, 170, 80); ctx.closepath(); ctx.linewidth = 3; ctx.fillstyle ='#f1f1f1'; ctx.fill(); ctx.stroke(); var dataurl =canvas.todataurl(); </script> </body></html>
以上就是如何使用canvas.todataurl()将html canvas保存为图像?的详细内容。