canvas实现手机端用来上传用户头像的代码
废话不多说了直接给大家介绍canvas实现手机端用来上传用户头像的代码,具体代码如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> label{ height:40px; width:100px; border:1px solid #666; display:block; text-align:center; line-height:40px; border-radius:10px; background:lightgreen; opacity: 1; } input{ display:none; } span{ display:block; height:100%; width:100%; } #canvas { border:1px solid #666; } </style> <script src="js/jquery-1.11.3.js"></script> </head> <body> <label> <input type="file" id="file" /> <span>上传文件</span> </label> <canvas width="300" height="300" id="canvas"></canvas> </body> <script> $("#file").change(function (){ var file = new FileReader();//读取文件2进制 file.onload = function(e){ var base64 = e.target.result; var img = new Image();//创建一个图片对象 img.onload = function (){ var canvas = $("#canvas").get(0); var ctx = canvas.getContext("2d"); //使用drawImage显示图片 ctx.drawImage(img,0,0,canvas.width,canvas.height); } //把base64添加到图片上 img.src = base64; }; file.readAsDataURL(this.files[0]); }); </script> </html>
相关推荐
northwindx 2020-05-31
xdyangxiaoromg 2020-03-27
大地飞鸿 2020-11-12
星星有所不知 2020-10-12
jinxiutong 2020-07-26
MIKUScallion 2020-07-05
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25
MIKUScallion 2020-05-25
jinxiutong 2020-05-10
xdyangxiaoromg 2020-05-10
大地飞鸿 2020-05-06