Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat add drag step #680

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion dist/zrender.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@
}
if (draggingTarget) {
this._draggingTarget = draggingTarget;
this.targetX = draggingTarget.x;
this.downX = e.offsetX;
draggingTarget.dragging = true;
this._x = e.offsetX;
this._y = e.offsetY;
Expand All @@ -827,7 +829,20 @@
var dy = y - this._y;
this._x = x;
this._y = y;
draggingTarget.drift(dx, dy, e);
var draggableXStep = draggingTarget.draggableXStep, draggableXStepValve = draggingTarget.draggableXStepValve;
if (draggableXStep && draggableXStep !== 0) {
var diffX = x - this.downX;
var step = draggableXStep;
var value = draggableXStepValve;
var rem = diffX % step;
var finaldX = diffX - rem + (rem < step - value ? 0 : step);
draggingTarget.attr({
x: this.targetX + finaldX
});
}
else {
draggingTarget.drift(dx, dy, e);
}
this.handler.dispatchToElement(new Param(draggingTarget, e), 'drag', e.event);
var dropTarget = this.handler.findHover(x, y, draggingTarget).target;
var lastDropTarget = this._dropTarget;
Expand All @@ -853,6 +868,8 @@
}
this._draggingTarget = null;
this._dropTarget = null;
this.downX = null;
this.targetX = null;
};
return Draggable;
}());
Expand Down Expand Up @@ -14648,6 +14665,10 @@
}
function bindStyle(svgEl, style, el) {
var opacity = style.opacity == null ? 1 : style.opacity;
if (el instanceof ZRImage) {
svgEl.style.opacity = opacity + '';
return;
}
if (pathHasFill(style)) {
var fill = style.fill;
fill = fill === 'transparent' ? NONE : fill;
Expand Down Expand Up @@ -14849,6 +14870,7 @@
attr(svgEl, 'height', dh + '');
attr(svgEl, 'x', x + '');
attr(svgEl, 'y', y + '');
bindStyle(svgEl, style, el);
setTransform(svgEl, el.transform);
}
};
Expand Down
2 changes: 1 addition & 1 deletion dist/zrender.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/zrender.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ export interface ElementProps extends Partial<ElementEventHandlerProps> {
ignore?: boolean
isGroup?: boolean
draggable?: boolean | 'horizontal' | 'vertical'
// drag step
draggableXStep?: number
// Drag offset
draggableXStepValve?:number

silent?: boolean

Expand Down Expand Up @@ -296,7 +300,6 @@ interface Element<Props extends ElementProps = ElementProps> extends Transformab
}

class Element<Props extends ElementProps = ElementProps> {

id: number = guid()
/**
* Element type
Expand Down Expand Up @@ -328,6 +331,15 @@ class Element<Props extends ElementProps = ElementProps> {
*/
draggable: boolean | 'horizontal' | 'vertical'

/**
* drag step
*/
draggableXStep?: number
/**
* Drag offset
*/
draggableXStepValve?:number

/**
* Whether is it dragging.
*/
Expand Down
25 changes: 23 additions & 2 deletions src/mixin/Draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export default class Draggable {
_x: number
_y: number

// current target X
targetX:number

// current mousedown X
downX:number

constructor(handler: Handler) {
this.handler = handler;

Expand All @@ -50,6 +56,8 @@ export default class Draggable {
}
if (draggingTarget) {
this._draggingTarget = draggingTarget;
this.targetX = draggingTarget.x
this.downX = e.offsetX
draggingTarget.dragging = true;
this._x = e.offsetX;
this._y = e.offsetY;
Expand All @@ -71,8 +79,19 @@ export default class Draggable {
const dy = y - this._y;
this._x = x;
this._y = y;

draggingTarget.drift(dx, dy, e);
const { draggableXStep,draggableXStepValve } = draggingTarget
if ( draggableXStep && draggableXStep!==0 ){
let diffX = x - this.downX
const step = draggableXStep
const value = draggableXStepValve
const rem = diffX % step
const finaldX = diffX - rem + (rem < step - value ?0:step)
draggingTarget.attr({
x:this.targetX+finaldX
})
}else{
draggingTarget.drift(dx, dy, e);
}
this.handler.dispatchToElement(
new Param(draggingTarget, e), 'drag', e.event
);
Expand Down Expand Up @@ -113,6 +132,8 @@ export default class Draggable {

this._draggingTarget = null;
this._dropTarget = null;
this.downX = null
this.targetX = null
}

}
31 changes: 31 additions & 0 deletions test/drag_step.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>drag_step</title>
<script src="../dist/zrender.js"></script>

</head>
<body>
<div id="main" style="width:1000px;height:500px;"></div>
<script type="text/javascript">
const layer = zrender.init(document.querySelector('#main'))
var bar = new zrender.Rect({
draggable:'horizontal',
draggableXStep:30,
draggableXStepValve:15,
shape:{
x:0,
y:0,
width:60,
height:30
},
style:{
fill:'red'
}
})
layer.add(bar)
</script>
</body>
</html>