HTML5 canvas实现移动端上传头像拖拽裁剪效果_html5教程技巧
canvas
html5
裁切
本示例使用HTML5 canvas,简单的编写了上传头像的裁剪效果,移动端支持拖拽后裁剪, 虽然样式不好看,但是功能还算全:
下图为裁剪后的效果:
html部分:
XML/HTML Code复制内容到剪贴板
- nbsp;html>
- html lang="en">
- head>
- meta charset="UTF-8">
- title>上传头像title>
- meta name="renderer" content="webkit">
- meta name="viewport" content="width=device-width, initial-scale=1.0">
- head>
- body>
- div id="imgCrop" style="width:200px;height:200px;border:1px solid #ccc;overflow:hidden;">
- img src="img/test.jpg" alt="">
- div>
- input type="file" accept="image/*" />
- button id="save">保存button>
- p>下面为剪切的图片:p>
- div id="imgShow">div>
- body>
- html>
JavaScript部分:
JavaScript Code复制内容到剪贴板
- var $imgCrop = $("#imgCrop");
- var $img = $imgCrop.find("img");
- var img = $img[0];
- var width = parseInt($imgCrop.css("width"));
- var height = parseInt($imgCrop.css("height"));
- var startX,startY,scale = 1;
- var x = 0,y = 0;
- $("input").on("change",function(){
- var fr = new FileReader();
- var file = this.files[0]
- //console.log(file);
- if(!/image\/\w+/.test(file.type)){
- alert(file.name + "不是图片文件!");
- return;
- }
- console.log(file);
- $img.removeAttr("height width");
- fr.readAsDataURL(file);
- fr.onload = function(){
- img.src = fr.result;
- var widthInit = img.width;
- if(img.width>img.height){
- img.height = height;
- x = (width - img.width)/2;
- y = 0;
- }else{
- img.width = width;
- x = 0;
- y = (height - img.height)/2;
- }
- scale = widthInit/img.width;
- move($img, x, y);
- };
- });
- img.addEventListener("touchstart",function(e){
- startX = e.targetTouches[0].pageX;
- startY = e.targetTouches[0].pageY;
- return;
- });
- img.addEventListener("touchmove",function(e){
- e.preventDefault();
- e.stopPropagation();
- var changeX = e.changedTouches[0].pageX - startX + x;
- var changeY = e.changedTouches[0].pageY - startY + y;
- move($(this), changeX, changeY);
- return;
- });
- img.addEventListener("touchend",function(e){
- var changeX = e.changedTouches[0].pageX - startX + x;
- var changeY = e.changedTouches[0].pageY - startY + y;
- x = x + e.changedTouches[0].pageX - startX;
- y = y + e.changedTouches[0].pageY - startY;
- move($(this), changeX, changeY);
- return;
- });
- //确定目标图片的样式
- function move(ele, x, y){
- ele.css({
- '-webkit-transform' : 'translate3d(' + x + 'px, ' + y + 'px, 0)',
- 'transform' : 'translate3d(' + x + 'px, ' + y + 'px, 0)'
- });
- }
- $("#save").on("click",function(){
- var url = imageData($img);
- console.log(url);
-
$("#imgShow").html("
+url+" />");;
- });
- //裁剪图片
- function imageData($img) {
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- canvas.width = width ;
- canvas.height = height;
- ctx.drawImage(img, -x*scale, -y*scale, width*scale, height*scale, 0, 0, width, height);
- return canvas.toDataURL();
- }
以上就是本文的全部内容,希望对大家的学习有所帮助。
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前
By DDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前
By DDD
Inzoi:如何申請學校和大學
3 週前
By DDD
如何修復KB5055518無法在Windows 10中安裝?
2 週前
By DDD
Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla
4 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)