源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <script src="https://static.jyshare.com/libs/assets/canvas2image/canvas2image.js"></script> <style> .doc { width: 604px; margin: 0 auto; } canvas { display: block; border: 2px solid #888; } </style> <body> <div class="doc"> <canvas width="300" height="200" id="cvs"></canvas> <div> <p> <button id="save">保存</button> 或者 <button id="convert">转换为</button> : <select id="sel"> <option value="png">png</option> <option value="jpeg">jpeg</option> <option value="bmp">bmp</option> </select><br/> 宽度 : <input type="number" value="300" id="imgW" /><br/> 高度 : <input type="number" value="200" id="imgH" /> </p> </div> <div id="imgs"> </div> </div> <script> var canvas, ctx, bMouseIsDown = false, iLastX, iLastY, $save, $imgs, $convert, $imgW, $imgH, $sel; function init () { canvas = document.getElementById('cvs'); ctx = canvas.getContext('2d'); $save = document.getElementById('save'); $convert = document.getElementById('convert'); $sel = document.getElementById('sel'); $imgs = document.getElementById('imgs'); $imgW = document.getElementById('imgW'); $imgH = document.getElementById('imgH'); bind(); draw(); } function bind () { canvas.onmousedown = function(e) { bMouseIsDown = true; iLastX = e.clientX - canvas.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft); iLastY = e.clientY - canvas.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop); } canvas.onmouseup = function() { bMouseIsDown = false; iLastX = -1; iLastY = -1; } canvas.onmousemove = function(e) { if (bMouseIsDown) { var iX = e.clientX - canvas.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft); var iY = e.clientY - canvas.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop); ctx.moveTo(iLastX, iLastY); ctx.lineTo(iX, iY); ctx.stroke(); iLastX = iX; iLastY = iY; } }; $save.onclick = function (e) { var type = $sel.value, w = $imgW.value, h = $imgH.value; Canvas2Image.saveAsImage(canvas, w, h, type); } $convert.onclick = function (e) { var type = $sel.value, w = $imgW.value, h = $imgH.value; $imgs.appendChild(Canvas2Image.convertToImage(canvas, w, h, type)) } } function draw () { ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, 600, 400); ctx.fillStyle = 'red'; ctx.fillRect(100, 100, 50, 50); } onload = init; </script> </body> </html>
运行结果: