登录  /  注册
首页 > web前端 > H5教程 > 正文

HTML5 canvas实现移动端上传头像拖拽裁剪效果_html5教程技巧

php中文网
发布: 2016-05-23 14:20:21
原创
2336人浏览过

本示例使用html5 canvas,简单的编写了上传头像的裁剪效果,移动端支持拖拽后裁剪, 虽然样式不好看,但是功能还算全:

下图为裁剪后的效果:

html部分:

XML/HTML Code复制内容到剪贴板
  1. nbsp;html>  
  2. html lang="en">  
  3. head>  
  4.     meta charset="UTF-8">  
  5.     title>上传头像title>  
  6.     meta name="renderer" content="webkit">  
  7.     meta name="viewport" content="width=device-width, initial-scale=1.0">  
  8. head>  
  9. body>  
  10. div id="imgCrop" style="width:200px;height:200px;border:1px solid #ccc;overflow:hidden;">  
  11.     img src="img/test.jpg" alt="">  
  12. div>  
  13. input type="file" accept="image/*" />  
  14. button id="save">保存button>  
  15. p>下面为剪切的图片:p>  
  16. div id="imgShow">div>  
  17. body>  
  18. html>  

JavaScript部分:

JavaScript Code复制内容到剪贴板
  1. var $imgCrop = $("#imgCrop");   
  2. var $img = $imgCrop.find("img");   
  3. var img = $img[0];   
  4. var width = parseInt($imgCrop.css("width"));   
  5. var height = parseInt($imgCrop.css("height"));   
  6. var startX,startY,scale = 1;   
  7. var x = 0,y = 0;   
  8. $("input").on("change",function(){   
  9.     var fr = new FileReader();   
  10.     var file = this.files[0]   
  11.     //console.log(file);   
  12.     if(!/image\/\w+/.test(file.type)){   
  13.         alert(file.name + "不是图片文件!");   
  14.         return;   
  15.     }   
  16.     console.log(file);   
  17.     $img.removeAttr("height width");   
  18.     fr.readAsDataURL(file);   
  19.   
  20.     fr.onload = function(){   
  21.         img.src = fr.result;   
  22.         var widthInit = img.width;   
  23.         if(img.width>img.height){   
  24.             img.height = height;   
  25.             x = (width - img.width)/2;   
  26.             y = 0;   
  27.         }else{   
  28.             img.width = width;   
  29.             x = 0;   
  30.             y = (height - img.height)/2;   
  31.         }   
  32.         scale = widthInit/img.width;   
  33.         move($img, x, y);   
  34.            
  35.     };   
  36.        
  37. });   
  38.   
  39. img.addEventListener("touchstart",function(e){     
  40.     startX = e.targetTouches[0].pageX;   
  41.     startY = e.targetTouches[0].pageY;   
  42.     
  43.     return;     
  44.   
  45. });    
  46. img.addEventListener("touchmove",function(e){     
  47.     e.preventDefault();     
  48.     e.stopPropagation();     
  49.   
  50.     var changeX = e.changedTouches[0].pageX - startX + x;   
  51.     var changeY = e.changedTouches[0].pageY - startY + y;   
  52.   
  53.     move($(this), changeX, changeY);   
  54.     return;     
  55.      
  56. });    
  57. img.addEventListener("touchend",function(e){      
  58.    var changeX = e.changedTouches[0].pageX - startX + x;   
  59.     var changeY = e.changedTouches[0].pageY - startY + y;   
  60.   
  61.     x = x + e.changedTouches[0].pageX - startX;   
  62.     y = y + e.changedTouches[0].pageY - startY;   
  63.   
  64.     move($(this), changeX, changeY);   
  65.     return;     
  66.      
  67. });     
  68. //确定目标图片的样式   
  69. function move(ele, x, y){   
  70.     ele.css({   
  71.         '-webkit-transform' : 'translate3d(' + x + 'px, ' + y + 'px, 0)',   
  72.         'transform' : 'translate3d(' + x + 'px, ' + y + 'px, 0)'  
  73.     });   
  74. }   
  75.   
  76. $("#save").on("click",function(){   
  77.     var url = imageData($img);   
  78.     console.log(url);   
  79.   
  80.     $("#imgShow").html("HTML5 canvas实现移动端上传头像拖拽裁剪效果_html5教程技巧+url+" />");;   
  81. });   
  82. //裁剪图片   
  83. function imageData($img) {   
  84.         var canvas = document.createElement('canvas');   
  85.         var ctx = canvas.getContext('2d');   
  86.         canvas.width = width ;   
  87.         canvas.height = height;   
  88.         ctx.drawImage(img, -x*scale, -y*scale, width*scale, height*scale, 0, 0, width, height);   
  89.         return canvas.toDataURL();   
  90.     }   
  91.   

以上就是本文的全部内容,希望对大家的学习有所帮助。

原文:http://www.cnblogs.com/yifengBlog/p/5265598.html

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号