| `maintainAspectRatio` | `boolean` | `true` | Maintain the original canvas aspect ratio `(width / height)` when resizing.
| `aspectRatio` | `number` | `2` | Canvas aspect ratio (i.e. `width / height`, a value of 1 representing a square canvas). Note that this option is ignored if the height is explicitly defined either as attribute or via the style.
| `onResize` | `function` | `null` | Called when a resize occurs. Gets passed two arguments: the chart instance and the new size.
+| `resizeDelay` | `number` | `0` | Delay the resize update by give amount of milliseconds. This can ease the resize process by debouncing update of the elements.
## Important Note
import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
// @ts-ignore
import {version} from '../../package.json';
+import {debounce} from '../helpers/helpers.extras';
/**
* @typedef { import("../platform/platform.base").ChartEvent } ChartEvent
this.attached = false;
this._animationsDisabled = undefined;
this.$context = undefined;
+ this._doResize = debounce(() => this.update('resize'), options.resizeDelay || 0);
// Add the chart instance to the global namespace
instances[me.id] = me;
callCallback(options.onResize, [newSize], me);
if (me.attached) {
- me.update('resize');
+ if (me._doResize()) {
+ // The resize update is delayed, only draw without updating.
+ me.render();
+ }
}
}
};
}
+/**
+ * Debounces calling `fn` for `delay` ms
+ * @param {function} fn - Function to call. No arguments are passed.
+ * @param {number} delay - Delay in ms. 0 = immediate invocation.
+ * @returns {function}
+ */
+export function debounce(fn, delay) {
+ let timeout;
+ return function() {
+ if (delay) {
+ clearTimeout(timeout);
+ timeout = setTimeout(fn, delay);
+ } else {
+ fn();
+ }
+ return delay;
+ };
+}
+
/**
* Converts 'start' to 'left', 'end' to 'right' and others to 'center'