H5简单实现头像裁剪上传

1.如何实现图片实现本地预览

2.如何实现裁剪部分图片

3.如何实现裁剪框拖动

4.如何实现裁剪框缩放

5.如何实现部分土图片转成文件对象




<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>头像裁剪上传</title>

<style>

* { margin: 0; padding: 0; box-sizing: border-box; list-style-type: none; user-select: none; }

img { pointer-events: none; }

ul { width: 600px; display: flex; align-items: flex-start; margin: 20px auto; }

.test { width: 400px; height: 300px; border: 1px solid #ddd; display: flex; align-items: center; position: relative; background-color: #f2f2f2; }

.test img { width: 100%; height: auto; }

.frame { width: 60px; height: 60px; border: 1px solid blue; position: absolute; z-index: 2; top: 50%; left: 50%; cursor: all-scroll; display: none;  transform: translate(-50%, -50%); }

.avtar { margin-left: 20px; width: 96px; height: 96px; border: 1px solid #ddd; border-radius: 50%; overflow: hidden; }

.frame div { border-width: 3px; border-color: blue; position: absolute; width: 15px; height: 15px; z-index: 4; }

.l_t { cursor: nw-resize; left: -2px; top: -2px; border-top-style: solid; border-left-style: solid; }

.l_b { cursor: sw-resize; left: -2px; bottom: -2px; border-bottom-style: solid; border-left-style: solid; }

.r_t { cursor: sw-resize; right: -2px; top: -2px; border-top-style: solid; border-right-style: solid; }

.r_b { cursor: nw-resize; right: -2px; bottom: -2px; border-bottom-style: solid; border-right-style: solid; }

</style>

</head>

<body>

<input type="file" />

<ul>

 <li class="test">

   <img class="preview" />

   <div class="frame">

      <div class="l_t"></div>

      <div class="l_b"></div>

      <div class="r_t"></div>

      <div class="r_b"></div>

   </div>

 </li>

  <li class="avtar">

     <canvas></canvas>

 </li>

</ul>

<div>

   <img src="" class="show" />

   <button>生成裁剪图片</button>

</div>

<script>

/**

* 实现头像裁剪上传

* 1.如何实现图片实现本地预览

* 2.如何实现部分图片上传

* * **/

// 输入框

const inp = document.querySelector("input");

// 本地图片

const test = document.querySelector(".test");

const preview = document.querySelector(".preview");

// 裁剪框

const frame = document.querySelector(".frame");

// 是否按下裁剪框 true:是

let isFrame = false;

// 裁剪后展示框

const avtar = document.querySelector(".avtar");

// 裁剪画布

const cvs = document.querySelector("canvas");

cvs.width = avtar.offsetWidth;

cvs.height = avtar.offsetHeight;

const ctx = cvs.getContext("2d");

// 监听选择图片

inp.onchange = function (ev) {

    const file = ev.target.files[0];

    ev.target.value = ""; 

   frame.style.display = "block";

    // 将 file 转为 blob 路径地址

    let blobURL = URL.createObjectURL(file);

     // 本地预览选择的图片

     preview.src = blobURL;

};

// 裁剪对象 

let imgInfo = {

// 裁剪图片的 x 坐标,从图片的哪个横坐标开始裁剪

x: frame.offsetLeft,

// 裁剪图片的 y 坐标,从图片的哪个纵坐标开始裁剪

y: frame.offsetTop,

// 裁剪的宽度

cutW: frame.offsetWidth,

// 裁剪的高度

cutH: frame.offsetHeight,

// 剪裁之后展示的宽度,缩小或放大

w: avtar.offsetWidth,

// 剪裁之后展示的高度,缩小或放大

h: avtar.offsetHeight,

};

frame.onmousedown = function (ev) {    

    ev.preventDefault();

     ev.stopPropagation();

     isFrame = true;

     // 浏览器可视区域左侧和上侧为起始点

     const { x, y } = ev;

     // 当前左边距(已test左侧为起始点)

      const l = frame.offsetLeft;

     // 当前上边距(已test上侧为起始点)

      const t = frame.offsetTop;

// 当前裁剪框宽度

const w = frame.offsetWidth;

// 当前裁剪框高度

const h = frame.offsetHeight;

// 最大移动宽度

const maxL = test.offsetWidth - frame.offsetWidth / 2;

const minL = frame.offsetWidth / 2;

const hh = (test.offsetHeight - preview.offsetHeight) / 2;

// 最大移动高度

const maxT = test.offsetHeight - hh - frame.offsetHeight / 2;

const minT = hh + frame.offsetHeight / 2;

document.onmousemove = function (e) {

if (!isFrame) return;

// 浏览器可视区域左侧和上侧为起始点

const { x: dx, y: dy } = e;

let left = dx - x + l;

let top = dy - y + t;

frame.style.left = `${ left <= minL ? minL : left >= maxL ? maxL : left }px`;

frame.style.top = `${ top <= minT ? minT : top >= maxT ? maxT : top }px`;

draw();

};

document.onmouseup = function (e) {

isFrame = false;

document.onmousemove = null;

document.onmouseup = null;

};

};

// 裁剪图片生成裁剪文件对象

function draw() {

// 最终裁剪x坐标

const hh = (test.offsetHeight - preview.offsetHeight) / 2;

const absx = frame.offsetLeft - frame.offsetWidth / 2;

const absy = frame.offsetTop - frame.offsetHeight / 2 - hh;

imgInfo.x = (absx / preview.offsetWidth) * preview.naturalWidth;

// 最终裁剪y坐标

imgInfo.y = (absy / preview.offsetHeight) * preview.naturalHeight;

// 最终裁剪宽度

imgInfo.cutW = (frame.offsetWidth / preview.offsetWidth) * preview.naturalWidth;

// 最终裁剪高度

imgInfo.cutH = (frame.offsetHeight / preview.offsetHeight) * preview.naturalHeight;

ctx.drawImage( preview, imgInfo.x, imgInfo.y, imgInfo.cutW, imgInfo.cutH, 0, 0, imgInfo.w, imgInfo.h );

}

const btn = document.querySelector("button");

btn.onclick = function () {

const show = document.querySelector(".show");

cvs.toBlob((blod) => {

// 裁剪之后的文件对象,可供上传

const file = new File([blod], "test.jpg", { type: "image/jepg" });

console.log(file);

show.src = URL.createObjectURL(file);

});

};

const l_t = document.querySelector(".l_t");

const l_b = document.querySelector(".l_b");

const r_t = document.querySelector(".r_t");

const r_b = document.querySelector(".r_b");

// 是否进行缩放

let iSalce = false;

// 左上角缩放

l_t.onmousedown = Scale("lt"); // 左下角缩放

l_b.onmousedown = Scale("lb"); // 右上角缩放

r_t.onmousedown = Scale("rt"); // 右下角缩放

r_b.onmousedown = Scale("rb");

// 方向缩放函数 direction:方向

function Scale(direction) {

return function (e) {

iSalce = true;

isFrame = false;

e.preventDefault();

e.stopPropagation();

const { x } = e;

const left = frame.offsetLeft;

const top = frame.offsetTop;

const w = frame.offsetWidth;

const h = frame.offsetHeight;

const hh = (test.offsetHeight - preview.offsetHeight) / 2;

// 最大缩放长度

let maxv = 0;

// 最小缩放长度

let minv = 40 - h;

// 左上

if (direction == "lt") {

const maxH = frame.offsetTop - frame.offsetHeight / 2 - hh;

const maxW = frame.offsetLeft - frame.offsetWidth / 2;

maxv = Math.min(maxH, maxW);

}

// 左下

if (direction == "lb") {

const maxH = test.offsetHeight - frame.offsetTop - frame.offsetHeight / 2 - hh;

const maxW = frame.offsetLeft - frame.offsetWidth / 2;

maxv = Math.min(maxH, maxW);

}

// 右上

if (direction == "rt") {

const maxH = frame.offsetTop - frame.offsetHeight / 2 - hh;

const maxW = test.offsetWidth - frame.offsetLeft - frame.offsetWidth / 2;

maxv = Math.min(maxH, maxW);

}

// 右下

if (direction == "rb") {

const maxH = test.offsetHeight - frame.offsetTop - frame.offsetHeight / 2 - hh;

const maxW = test.offsetWidth - frame.offsetLeft - frame.offsetWidth / 2;

maxv = Math.min(maxH, maxW);

}

document.onmousemove = function (ev) {

if (!iSalce) return;

let absx = direction == "rt" || direction == "rb" ? ev.x - x : x - ev.x;

absx = absx >= maxv ? maxv : absx >= minv ? absx : minv;

frame.style.width = `${w + absx}px`;

frame.style.height = `${h + absx}px`;

// 左上

if (direction == "lt") {

frame.style.left = `${left - absx / 2}px`;

frame.style.top = `${top - absx / 2}px`;

}

// 右上

if (direction == "rt") {

frame.style.left = `${left + absx / 2}px`;

frame.style.top = `${top - absx / 2}px`;

}

// 左下

if (direction == "lb") {

frame.style.left = `${left - absx / 2}px`;

frame.style.top = `${top + absx / 2}px`;

}

// 右下

if (direction == "rb") {

frame.style.left = `${left + absx / 2}px`;

frame.style.top = `${top + absx / 2}px`;

}

draw();

};

document.onmouseup = function () {

isFrame = false;

document.onmousemove = null;

document.onmouseup = null;

};

};

}

</script>

</body>

</html>



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容