实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>canvas多张图片上传</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> </head> <style type="text/css"> #ip{font-size: 28px;color: red} #img{display: none;} </style> <body> <canvas id="canvas" width="300" height="300" style="border:1px solid #ccc;display: block; margin: 0 auto;"> </canvas> <img src="" id="img"> <input type="file" id="file" name=""> </body> <script type="text/javascript"> $('#file').change(function(e){ var file=e.target.files[0]; var url=window.URL.createObjectURL(file); var img=document.getElementById('img'); img.src=url; img.onload=function(){ var canvas=document.getElementById('canvas'); var sy=document.getElementById('sy'); var ctx=canvas.getContext('2d'); var tempw=img.width; var temph=img.height; img.width=500; img.height=500/tempw*temph; canvas.width=img.width; canvas.height=img.height; ctx.drawImage(img,0,0,canvas.width, canvas.height); // ctx.fillStyle="rgba(252,255,255,0.7)"; // ctx.fillRect(0,0,150,75); ctx.font="30px 微软雅黑"; ctx.fillText("PengLi",10,50); //ctx.drawImage(sy,0,0); var dataUrl=canvas.toDataURL("image/jpeg",0.9); console.log(dataUrl); } }) </script> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例