<!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>Document</title>
<style>
.Container {
position: relative;
box-sizing: border-box;
border: 1px solid red;
width: 0;
height: 0;
display: none;
overflow: hidden;
}
.Masks {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 99;
background: rgba(0, 0, 0, .5);
}
.Intercept {
cursor: move;
position: absolute;
top: 0;
left: 0;
z-index: 999;
}
.nodetop {
position: absolute;
top: -4px;
left: 50%;
transform: translateX(-50%);
width: 30px;
height: 8px;
background: #fff;
border-radius: 4px;
cursor: n-resize;
}
.nodebottom {
position: absolute;
bottom: -4px;
left: 50%;
transform: translateX(-50%);
width: 30px;
height: 8px;
background: #fff;
border-radius: 4px;
cursor: s-resize;
}
.nodeleft {
position: absolute;
top: 50%;
left: -4px;
transform: translateY(-50%);
width: 8px;
height: 30px;
background: #fff;
border-radius: 4px;
cursor: w-resize;
}
.noderight {
position: absolute;
top: 50%;
right: -4px;
transform: translateY(-50%);
width: 8px;
height: 30px;
background: #fff;
border-radius: 4px;
cursor: e-resize;
}
</style>
</head>
<body>
<input id="npt" type="file" accept="image/png,image/jpeg">
<img id="img" src="" alt="">
<script>
function Trim(obj, callback) {
this.canvas = document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.imgurl = '';
this.width = 0;
this.height = 0;
this.dv = document.createElement('div');
this.Masks = document.createElement('div');
this.Intercept = document.createElement('div');
this.nodetop = document.createElement('div');
this.nodebottom = document.createElement('div');
this.nodeleft = document.createElement('div');
this.noderight = document.createElement('div');
this.dragging = false;
this.position = [];
this.clone = [
{ x: 0, y: 0 },
{ xx: 0, yy: 0 }
]
this.nCanvas = document.createElement('canvas');
this.nCtx = this.nCanvas.getContext('2d');
this.callback = callback;
this._base64Url(obj.id);
}
Trim.prototype._base64Url = function (id) {
var npt = document.querySelector(id);
npt.onchange = () => {
var reader = new FileReader();
reader.readAsDataURL(npt.files[0]);
reader.onloadend = (e) => {
this.imgurl = e.target.result;
this._creatImg()
};
};
}
Trim.prototype._creatImg = function () {
var image = new Image();
image.src = this.imgurl;
image.addEventListener('load', () => {
this.width = image.width < 500 ? image.width : (image.width / 2);
this.height = image.width < 500 ? image.height : (image.height / 2);
this.canvas.height = this.height;
this.canvas.width = this.width;
this.ctx.drawImage(image, 0, 0, this.width, this.height);
this._creatDv();
})
}
Trim.prototype._creatDv = function () {
this.dv.appendChild(this.canvas);
document.body.appendChild(this.dv);
this.dv.className = 'Container';
this.dv.style.display = 'block';
this.dv.style.width = `${this.width}px`;
this.dv.style.height = `${this.height}px`;
this.Masks.className = 'Masks';
this.dv.appendChild(this.Masks);
this.dv.appendChild(this.Intercept);
this.Intercept.className = 'Intercept';
this.Intercept.style.width = `${this.width / 2}px`;
this.Intercept.style.height = `${this.height / 2}px`;
this.Intercept.style.top = `${this.height / 4}px`;
this.Intercept.style.left = `${this.width / 4}px`;
this.clone = [
{ x: this.Intercept.offsetLeft, y: this.Intercept.offsetTop },
{ xx: this.Intercept.offsetLeft + this.width / 2, yy: this.Intercept.offsetTop + this.height / 2 }
]
this._Screenshots();
this.Intercept.addEventListener('mousedown', (e) => {
this.dragging = true
this.position = [e.clientX, e.clientY]
})
this.Intercept.addEventListener('mousemove', (e) => {
if (this.dragging === false) {
return;
}
const x = e.clientX;
const y = e.clientY;
const deltax = x - this.position[0]
const deltay = y - this.position[1]
const left = parseInt(this.Intercept.offsetLeft)
const top = parseInt(this.Intercept.offsetTop)
if ((left + deltax + this.Intercept.offsetWidth) <= this.width && (left + deltax) >= 0) {
this.Intercept.style.left = left + deltax + 'px'
}
if ((top + deltay + this.Intercept.offsetHeight) <= this.height && (top + deltay) >= 0) {
this.Intercept.style.top = top + deltay + 'px'
}
this.clone = [
{ x: this.Intercept.offsetLeft, y: this.Intercept.offsetTop },
{ xx: this.Intercept.offsetLeft + this.width / 2, yy: this.Intercept.offsetTop + this.height / 2 }
]
this.position = [x, y]
this._Screenshots();
e.stopPropagation();
})
this.Intercept.addEventListener('mouseup', () => {
this.dragging = false
})
document.addEventListener('mousemove', () => {
this.dragging = false;
this.position = [0, 0]
})
}
Trim.prototype._Screenshots = function () {
var w = this.clone[1].xx - this.clone[0].x;
var h = this.clone[1].yy - this.clone[0].y;
var cutImage = this.ctx.getImageData(this.clone[0].x, this.clone[0].y, w + 1, h + 1);
this.nCanvas.width = w;
this.nCanvas.height = h;
this.nCtx.putImageData(cutImage, 0, 0);
this.Intercept.appendChild(this.nCanvas)
let imgurl = this.nCanvas.toDataURL('image/png')
this.callback(imgurl);
}
let a = new Trim({
id: '#npt'
}, function (data) {
console.log(data)
})
</script>
</body>
</html>