|
|
|
@ -1,4 +1,4 @@ |
|
|
|
import { IObject } from "@/types/interface"; |
|
|
|
import { IObject } from '@/types/interface'; |
|
|
|
|
|
|
|
class DragStretchService { |
|
|
|
target: HTMLElement; |
|
|
|
@ -20,7 +20,7 @@ class DragStretchService { |
|
|
|
this.sourceX = 0; |
|
|
|
this.sourceY = 0; |
|
|
|
this.distanceY = opt?.distanceY || 0; |
|
|
|
this.scale = opt?.scale || "1"; |
|
|
|
this.scale = opt?.scale || '1'; |
|
|
|
this.transform = this.getTransform(); |
|
|
|
this.init(); |
|
|
|
} |
|
|
|
@ -29,7 +29,7 @@ class DragStretchService { |
|
|
|
return { |
|
|
|
distanceX: this.distanceX, |
|
|
|
distanceY: this.distanceY, |
|
|
|
scale: this.scale |
|
|
|
scale: this.scale, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
@ -39,10 +39,10 @@ class DragStretchService { |
|
|
|
: this.target.currentStyle[property]; |
|
|
|
} |
|
|
|
getTransform() { |
|
|
|
let transform = "", |
|
|
|
let transform = '', |
|
|
|
i = 0; |
|
|
|
const divStyle = document.createElement("div").style, |
|
|
|
transformArr = ["transform", "webkitTransform", "MozTransform", "msTransform", "OTransform"], |
|
|
|
const divStyle = document.createElement('div').style, |
|
|
|
transformArr = ['transform', 'webkitTransform', 'MozTransform', 'msTransform', 'OTransform'], |
|
|
|
len = transformArr.length; |
|
|
|
|
|
|
|
for (; i < len; i++) { |
|
|
|
@ -57,18 +57,18 @@ class DragStretchService { |
|
|
|
let pos = { x: 0, y: 0 }; |
|
|
|
|
|
|
|
const transformValue = this.getStyle(this.transform); |
|
|
|
if (transformValue == "none") { |
|
|
|
this.target.style[this.transform] = "translate(0, 0) scale(1)"; |
|
|
|
if (transformValue == 'none') { |
|
|
|
this.target.style[this.transform] = 'translate(0, 0) scale(1)'; |
|
|
|
} |
|
|
|
const matrixReg = /^matrix\((?:[-\d.]+,\s*){4}([-\d.]+),\s*([-\d.]+)\)$/; |
|
|
|
const scaleReg = /\d+(?:\.\d+)?(?=,)/; |
|
|
|
const temp = transformValue.match(matrixReg); |
|
|
|
const scale = transformValue.match(scaleReg); |
|
|
|
console.log(scale, "scale"); |
|
|
|
console.log(scale, 'scale'); |
|
|
|
pos = { |
|
|
|
x: parseInt(temp[1].trim()), |
|
|
|
y: parseInt(temp[2].trim()), |
|
|
|
scale: parseFloat(scale[0]) |
|
|
|
scale: parseFloat(scale[0]), |
|
|
|
}; |
|
|
|
|
|
|
|
return pos; |
|
|
|
@ -83,8 +83,8 @@ class DragStretchService { |
|
|
|
this.sourceX = pos.x; |
|
|
|
this.sourceY = pos.y; |
|
|
|
this.scale = pos.scale; |
|
|
|
document.addEventListener("mousemove", move, false); |
|
|
|
document.addEventListener("mouseup", end, false); |
|
|
|
document.addEventListener('mousemove', move, false); |
|
|
|
document.addEventListener('mouseup', end, false); |
|
|
|
}; |
|
|
|
|
|
|
|
const move = (event: MouseEvent) => { |
|
|
|
@ -98,12 +98,12 @@ class DragStretchService { |
|
|
|
}; |
|
|
|
|
|
|
|
const end = () => { |
|
|
|
document.removeEventListener("mousemove", move); |
|
|
|
document.removeEventListener("mouseup", end); |
|
|
|
document.removeEventListener('mousemove', move); |
|
|
|
document.removeEventListener('mouseup', end); |
|
|
|
// do other things
|
|
|
|
}; |
|
|
|
|
|
|
|
this.target.addEventListener("mousedown", start, false); |
|
|
|
this.target.addEventListener('mousedown', start, false); |
|
|
|
} |
|
|
|
// 初始化
|
|
|
|
init() { |
|
|
|
@ -121,7 +121,7 @@ class DragStretchService { |
|
|
|
const { clientWidth, clientHeight } = parentNode as HTMLElement; |
|
|
|
return { |
|
|
|
clientWidth, |
|
|
|
clientHeight |
|
|
|
clientHeight, |
|
|
|
}; |
|
|
|
} |
|
|
|
return {}; |
|
|
|
@ -192,7 +192,7 @@ class DragStretchService { |
|
|
|
// }
|
|
|
|
|
|
|
|
stretch() { |
|
|
|
this.target.addEventListener("mousewheel", (e: Event) => { |
|
|
|
this.target.addEventListener('mousewheel', (e: Event) => { |
|
|
|
const ev: IObject = e; |
|
|
|
const up = ev.wheelDelta ? ev.wheelDelta >= 0 : ev.detail <= 0; // 定义一个标志,当滚轮向下滚时,执行一些操作
|
|
|
|
|
|
|
|
@ -206,7 +206,7 @@ class DragStretchService { |
|
|
|
this.setTransform(); |
|
|
|
} else { |
|
|
|
if (+this.scale <= 0.1) { |
|
|
|
this.scale = "0.1"; |
|
|
|
this.scale = '0.1'; |
|
|
|
} else { |
|
|
|
this.scale = (parseFloat(this.scale) - this.options.ratio).toFixed(2); |
|
|
|
} |
|
|
|
@ -222,7 +222,7 @@ class DragStretchService { |
|
|
|
} |
|
|
|
|
|
|
|
setTransform() { |
|
|
|
console.log(this.scale, "this.scale"); |
|
|
|
console.log(this.scale, 'this.scale'); |
|
|
|
//this.target.style.transform = `translate(${this.distanceX}px, ${this.distanceY}px)`;
|
|
|
|
this.target.style.transform = `translate(${this.distanceX}px, ${this.distanceY}px) scale(${this.scale})`; |
|
|
|
//this.target.style.transformOrigin = "0 0";
|
|
|
|
@ -231,7 +231,7 @@ class DragStretchService { |
|
|
|
reset() { |
|
|
|
this.distanceX = 0; |
|
|
|
this.distanceY = 0; |
|
|
|
this.scale = "1"; |
|
|
|
this.scale = this.options.scale || '1'; |
|
|
|
this.setTransform(); |
|
|
|
} |
|
|
|
} |
|
|
|
|