-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragging.ts
32 lines (27 loc) · 990 Bytes
/
dragging.ts
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
import throttle from 'lodash.throttle';
import Minimal from './minimal';
import {getCords, insertShape, shiftBy} from '../lib';
class Dragging extends Minimal {
constructor(id, initDoc) {
super(id, initDoc);
}
protected bindEvents() {
super.bindEvents();
const handleMouseMove = throttle(e => {
this.handleMouseMove(getCords(e, this.canvas));
return false;
}, 16);
this.canvas.addEventListener('mousemove', handleMouseMove);
}
// this method does more than one job
protected handleMouseMove(cords) {
if (this.dragging === null) {//Potential source of bugs: explicit state check
return null;
}
const dx = cords.x - this.dragging.startPos.x;
const dy = cords.y - this.dragging.startPos.y;
this.doc = insertShape(this.doc, shiftBy(this.dragging.shape, dx, dy));// doc is set in different place
return null;
}
}
export default Dragging;