Skip to content

Commit 81e2d34

Browse files
jledentubenmccann
authored andcommitted
Add a animationDuration option to configure the duration of the drag animation (#282)
Add a animationDuration option for drag
1 parent d3fdd1e commit 81e2d34

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ plugins: {
6565
// Enable drag-to-zoom behavior
6666
drag: true,
6767

68-
// Drag-to-zoom rectangle style can be customized
68+
// Drag-to-zoom effect can be customized
6969
// drag: {
7070
// borderColor: 'rgba(225,225,225,0.3)'
7171
// borderWidth: 5,
72-
// backgroundColor: 'rgb(225,225,225)'
72+
// backgroundColor: 'rgb(225,225,225)',
73+
// animationDuration: 0
7374
// },
7475

7576
// Zooming directions. Remove the appropriate direction to disable

samples/zoom-time.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
<script>
2727
var timeFormat = 'MM/DD/YYYY HH:mm';
2828
var now = window.moment();
29+
var dragOptions = {
30+
animationDuration: 1000
31+
};
2932

3033
function randomScalingFactor() {
3134
return Math.round(Math.random() * 100 * (Math.random() > 0.5 ? -1 : 1));
@@ -110,7 +113,7 @@
110113
zoom: {
111114
zoom: {
112115
enabled: true,
113-
drag: true,
116+
drag: dragOptions,
114117
mode: 'x',
115118
speed: 0.05
116119
}
@@ -134,7 +137,8 @@
134137
window.toggleDragMode = function() {
135138
var chart = window.myLine;
136139
var zoomOptions = chart.options.plugins.zoom.zoom;
137-
zoomOptions.drag = !zoomOptions.drag;
140+
zoomOptions.drag = zoomOptions.drag ? false : dragOptions;
141+
138142
chart.update();
139143
document.getElementById('drag-switch').innerText = zoomOptions.drag ? 'Disable drag mode' : 'Enable drag mode';
140144
};

src/plugin.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ function zoomScale(scale, zoom, center, zoomOptions) {
191191
* @param {number} percentZoomY The zoom percentage in the y direction
192192
* @param {{x: number, y: number}} focalPoint The x and y coordinates of zoom focal point. The point which doesn't change while zooming. E.g. the location of the mouse cursor when "drag: false"
193193
* @param {string} whichAxes `xy`, 'x', or 'y'
194+
* @param {number} animationDuration Duration of the animation of the redraw in milliseconds
194195
*/
195-
function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes) {
196+
function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes, animationDuration) {
196197
var ca = chart.chartArea;
197198
if (!focalPoint) {
198199
focalPoint = {
@@ -229,7 +230,14 @@ function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes) {
229230
}
230231
});
231232

232-
chart.update(0);
233+
if (animationDuration) {
234+
chart.update({
235+
duration: animationDuration,
236+
easing: 'easeOutQuad',
237+
});
238+
} else {
239+
chart.update(0);
240+
}
233241

234242
if (typeof zoomOptions.onZoom === 'function') {
235243
zoomOptions.onZoom({chart: chart});
@@ -489,7 +497,7 @@ var zoomPlugin = {
489497
doZoom(chartInstance, zoomX, zoomY, {
490498
x: (startX - chartArea.left) / (1 - dragDistanceX / chartDistanceX) + chartArea.left,
491499
y: (startY - chartArea.top) / (1 - dragDistanceY / chartDistanceY) + chartArea.top
492-
});
500+
}, undefined, zoomOptions.drag.animationDuration);
493501

494502
if (typeof zoomOptions.onZoomComplete === 'function') {
495503
zoomOptions.onZoomComplete({chart: chartInstance});

0 commit comments

Comments
 (0)