-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathDraggable.js
More file actions
134 lines (79 loc) · 3.15 KB
/
Copy pathDraggable.js
File metadata and controls
134 lines (79 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {Vector2} from 'three';
window.addEventListener( 'touchmove', () => {} );
document.addEventListener( 'touchmove', event => { event.preventDefault(); }, { passive: false } );
class Draggable {
constructor( element, options ) {
this.position = {
current: new Vector2(),
start: new Vector2(),
delta: new Vector2(),
old: new Vector2(),
drag: new Vector2(),
};
this.options = Object.assign( {
calcDelta: false,
}, options || {} );
this.element = element;
this.touch = null;
this.drag = {
start: ( event ) => {
if ( event.type == 'mousedown' && event.which != 1 ) return;
if ( event.type == 'touchstart' && event.touches.length > 1 ) return;
this.getPositionCurrent( event );
if ( this.options.calcDelta ) {
this.position.start = this.position.current.clone();
this.position.delta.set( 0, 0 );
this.position.drag.set( 0, 0 );
}
this.touch = ( event.type == 'touchstart' );
this.onDragStart( this.position );
window.addEventListener( ( this.touch ) ? 'touchmove' : 'mousemove', this.drag.move, {capture: false, passive: true} );
window.addEventListener( ( this.touch ) ? 'touchend' : 'mouseup', this.drag.end, {capture: false, passive: true} );
},
move: ( event ) => {
if ( this.options.calcDelta ) {
this.position.old = this.position.current.clone();
}
this.getPositionCurrent( event );
if ( this.options.calcDelta ) {
this.position.delta = this.position.current.clone().sub( this.position.old );
this.position.drag = this.position.current.clone().sub( this.position.start );
}
this.onDragMove( this.position );
},
end: ( event ) => {
this.getPositionCurrent( event );
this.onDragEnd( this.position );
window.removeEventListener( ( this.touch ) ? 'touchmove' : 'mousemove', this.drag.move, false );
window.removeEventListener( ( this.touch ) ? 'touchend' : 'mouseup', this.drag.end, false );
},
};
this.onDragStart = () => {};
this.onDragMove = () => {};
this.onDragEnd = () => {};
this.enable();
return this;
}
enable() {
this.element.addEventListener( 'touchstart', this.drag.start, {capture: false, passive: true} );
this.element.addEventListener( 'mousedown', this.drag.start, {capture: false, passive: true} );
return this;
}
disable() {
this.element.removeEventListener( 'touchstart', this.drag.start, {capture: false, passive: true} );
this.element.removeEventListener( 'mousedown', this.drag.start, {capture: false, passive: true} );
return this;
}
getPositionCurrent( event ) {
const dragEvent = event.touches
? ( event.touches[ 0 ] || event.changedTouches[ 0 ] )
: event;
this.position.current.set( dragEvent.pageX, dragEvent.pageY );
}
convertPosition( position ) {
position.x = ( position.x / this.element.offsetWidth ) * 2 - 1;
position.y = - ( ( position.y / this.element.offsetHeight ) * 2 - 1 );
return position;
}
}
export { Draggable };