forked from chartjs/chartjs-plugin-zoom
/
hammer.js
160 lines (142 loc) · 4.41 KB
/
hammer.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import {callback as call} from 'chart.js/helpers';
import Hammer from 'hammerjs';
import {pan, zoom} from './core';
import {getState} from './state';
import {directionEnabled, getEnabledScalesByPoint, getModifierKey, keyNotPressed, keyPressed} from './utils';
function createEnabler(chart, state) {
return function(recognizer, event) {
const {pan: panOptions, zoom: zoomOptions = {}} = state.options;
if (!panOptions || !panOptions.enabled) {
return false;
}
const srcEvent = event && event.srcEvent;
if (!srcEvent) { // Sometimes Hammer queries this with a null event.
return true;
}
if (!state.panning && event.pointerType === 'mouse' && (
keyNotPressed(getModifierKey(panOptions), srcEvent) || keyPressed(getModifierKey(zoomOptions.drag), srcEvent))
) {
call(panOptions.onPanRejected, [{chart, event}]);
return false;
}
return true;
};
}
function pinchAxes(p0, p1) {
// fingers position difference
const pinchX = Math.abs(p0.clientX - p1.clientX);
const pinchY = Math.abs(p0.clientY - p1.clientY);
// diagonal fingers will change both (xy) axes
const p = pinchX / pinchY;
let x, y;
if (p > 0.3 && p < 1.7) {
x = y = true;
} else if (pinchX > pinchY) {
x = true;
} else {
y = true;
}
return {x, y};
}
function handlePinch(chart, state, e) {
if (state.scale) {
const {center, pointers} = e;
// Hammer reports the total scaling. We need the incremental amount
const zoomPercent = 1 / state.scale * e.scale;
const rect = e.target.getBoundingClientRect();
const pinch = pinchAxes(pointers[0], pointers[1]);
const mode = state.options.zoom.mode;
const amount = {
x: pinch.x && directionEnabled(mode, 'x', chart) ? zoomPercent : 1,
y: pinch.y && directionEnabled(mode, 'y', chart) ? zoomPercent : 1,
focalPoint: {
x: center.x - rect.left,
y: center.y - rect.top
}
};
zoom(chart, amount);
// Keep track of overall scale
state.scale = e.scale;
}
}
function startPinch(chart, state) {
if (state.options.zoom.pinch.enabled) {
state.scale = 1;
}
}
function endPinch(chart, state, e) {
if (state.scale) {
handlePinch(chart, state, e);
state.scale = null; // reset
call(state.options.zoom.onZoomComplete, [{chart}]);
}
}
function handlePan(chart, state, e) {
const delta = state.delta;
if (delta) {
state.panning = true;
pan(chart, {x: e.deltaX - delta.x, y: e.deltaY - delta.y}, state.panScales);
state.delta = {x: e.deltaX, y: e.deltaY};
}
}
function startPan(chart, state, event) {
const {enabled, onPanStart, onPanRejected} = state.options.pan;
if (!enabled) {
return;
}
const rect = event.target.getBoundingClientRect();
const point = {
x: event.center.x - rect.left,
y: event.center.y - rect.top
};
if (call(onPanStart, [{chart, event, point}]) === false) {
return call(onPanRejected, [{chart, event}]);
}
state.panScales = getEnabledScalesByPoint(state.options.pan, point, chart);
state.delta = {x: 0, y: 0};
clearTimeout(state.panEndTimeout);
handlePan(chart, state, event);
}
function endPan(chart, state) {
state.delta = null;
if (state.panning) {
state.panEndTimeout = setTimeout(() => (state.panning = false), 500);
call(state.options.pan.onPanComplete, [{chart}]);
}
}
const hammers = new WeakMap();
export function startHammer(chart, options) {
const state = getState(chart);
const canvas = chart.canvas;
const {pan: panOptions, zoom: zoomOptions} = options;
const mc = new Hammer.Manager(canvas);
if (zoomOptions && zoomOptions.pinch.enabled) {
mc.add(new Hammer.Pinch());
mc.on('pinchstart', () => startPinch(chart, state));
mc.on('pinch', (e) => handlePinch(chart, state, e));
mc.on('pinchend', (e) => endPinch(chart, state, e));
}
if (panOptions && panOptions.enabled) {
mc.add(new Hammer.Pan({
threshold: panOptions.threshold,
enable: createEnabler(chart, state)
}));
mc.on('panstart', (e) => startPan(chart, state, e));
mc.on('panmove', (e) => handlePan(chart, state, e));
mc.on('panend', () => endPan(chart, state));
}
hammers.set(chart, mc);
}
export function stopHammer(chart) {
const mc = hammers.get(chart);
if (mc) {
mc.remove('pinchstart');
mc.remove('pinch');
mc.remove('pinchend');
mc.remove('panstart');
mc.remove('pan');
mc.remove('panend');
mc.destroy();
hammers.delete(chart);
}
}