效果图以下: css
引入js和对应的css。真的有须要的小伙伴只要把我对应文件夹的dragger.js和dragger.css拷进本身的项目里就能够用啦,不限制前端框架啊,vue、react仍是html文件里均可以使用哦html
import Drag from '../../static/dragger.js'
import './assets/css/dragger.css'
复制代码
以后,实例化前端
new Drag({
id: 'box-dragger',
showAngle: true,
isScale: false,
showBorder: false
})
new Drag({
id: 'box-dragger2',
canZoom: false,
canRotate: false
})
new Drag({
id: 'img-box',
showAngle: true,
showPosition: true
})
new Drag({
id: 'test'
})
复制代码
这两种方式都实现过一次,第一种比较简单,可是第一种,很差控制选中某个元素才让操做点展现。vue
考虑如何让用户快速上手使用,可参考的点:react
字段 | 说明 | 是否必填 | 默认值 |
---|---|---|---|
id | 目标元素id | 是 | 无 |
container | 父容器id | 否 | body |
canRotate | 是否能够旋转 | 否 | true |
canZoom | 是否能够缩放 | 否 | true |
canPull | 是否能够拉升 | 否 | true |
canMove | 是否能够平移 | 否 | true |
showAngle | 展现角度 | 否 | false |
showPosition | 展现位置 | 否 | false |
isScale | 是否等比例缩放 | 否 | true |
showBorder | 是否展现pannel的border | 否 | false |
具体看图: git
初始化参数github
初始化目标dom对象的位置:记录其:bash
初始化pannel结构
当前的父容器中只有一个pannel结构,每次实例化对象时,会判断一下若是当前这个父容器里已经存在id为pannel的结构,就将其子节点清空,按照当前实例化对象传进来的属性从新渲染pannel子结构。若是没有id为pannel的结构,就建立。前端框架
初始化事件app
initEvent () {
document.addEventListener('mousemove', e => {
e.preventDefault && e.preventDefault()
this.moveChange(e, this.targetObj)
})
document.addEventListener('mouseup', e => {
this.moveLeave(this.targetObj)
})
if (this.canMove) {
// 外层给this.pannelDom添加mousedown事件,是在全部实例化结束后,panneldom被展现在最后一个实例化对象上,鼠标按下它时,触发moveInit事件
this.pannelDom.onmousedown = e => {
e.stopPropagation()
this.moveInit(9, e, this.targetObj)
}
this.targetObj.onmousedown = e => {
e.stopPropagation()
this.moveInit(9, e, this.targetObj)
this.initPannel()
// 在点击其余未被选中元素时,pannel定位到该元素上,重写pannelDom事件,由于此时的this.pannelDom已经根据新的目标元素被重写
this.pannelDom.onmousedown= e => {
this.moveInit(9, e, this.targetObj)
}
}
}
}
复制代码
dom操做
this.mouseInit = {
x: Math.floor(e.clientX),
y: Math.floor(e.clientY)
}
this.preRadian = Math.atan2(this.mouseInit.y - this.centerPos.y, this.mouseInit.x - this.centerPos.x)
复制代码
this.rotateCurrent = {
x: Math.floor(e.clientX),
y: Math.floor(e.clientY)
}
this.curRadian = Math.atan2(this.rotateCurrent.y - this.centerPos.y, this.rotateCurrent.x - this.centerPos.x)
复制代码
this.tranformRadian = this.curRadian - this.preRadian
复制代码
this.angle = this.getRotate(target) + Math.round(this.tranformRadian * 180 / Math.PI)
this.preRadian = this.curRadian //鼠标移动的每一下都计算这个角度,因此每一下移动前的弧度值都上一次移动后的弧度值
复制代码
let disAngle = this.angle - this.initAngle
this.rightBottomPoint = this.getRotatedPoint(this.initRightBottomPoint, this.centerPos, disAngle)
this.rightTopPoint = this.getRotatedPoint(this.initRightTopPoint, this.centerPos, disAngle)
this.leftTopPoint = this.getRotatedPoint(this.initLeftTopPoint, this.centerPos, disAngle)
this.leftBottomPoint = this.getRotatedPoint(this.initLeftBottomPoint, this.centerPos, disAngle)
this.leftMiddlePoint = this.getRotatedPoint(this.initLeftMiddlePoint, this.centerPos, disAngle)
this.rightMiddlePoint = this.getRotatedPoint(this.initRightMiddlePoint, this.centerPos, disAngle)
this.topMiddlePoint = this.getRotatedPoint(this.initTopMiddlePoint, this.centerPos, disAngle)
this.bottomMiddlePoint = this.getRotatedPoint(this.initBottomMiddlePoint, this.centerPos, disAngle)
复制代码
复制代码
优化,mousemove事件添加节流函数
function throttle(fn, interval) {
let canRun = true;
return function () {
if (!canRun) return;
canRun = false;
setTimeout(() => {
fn.apply(this, arguments);
canRun = true;
}, interval);
};
}
let that = this
document.addEventListener('mousemove', throttle(function (e) {
e.preventDefault && e.preventDefault()
that.moveChange(e, that.targetObj)
}, 10))
复制代码