diff --git a/timeline-chart/src/layer/time-graph-axis.ts b/timeline-chart/src/layer/time-graph-axis.ts index 7489964..e5a2a3b 100644 --- a/timeline-chart/src/layer/time-graph-axis.ts +++ b/timeline-chart/src/layer/time-graph-axis.ts @@ -41,29 +41,14 @@ export class TimeGraphAxis extends TimeGraphLayer { const mw = _.throttle((ev: WheelEvent) => { if (this.controlKeyDown) { // ZOOM AROUND MOUSE POINTER - let newViewRangeLength = this.unitController.viewRangeLength; - let xOffset = 0; - let moveX = false; - if (Math.abs(ev.deltaX) > Math.abs(ev.deltaY)) { - xOffset = -(ev.deltaX / this.stateController.zoomFactor); - moveX = true; - } else { - const zoomPosition = (ev.offsetX / this.stateController.zoomFactor); - const deltaLength = (ev.deltaY / this.stateController.zoomFactor); - newViewRangeLength += deltaLength; - xOffset = ((zoomPosition / this.unitController.viewRangeLength) * deltaLength); - } - let start = this.unitController.viewRange.start - xOffset; - if (start < 0) { - start = 0; - } - let end = start + newViewRangeLength; - if (end > this.unitController.absoluteRange) { - end = this.unitController.absoluteRange; - if (moveX) { - start = end - newViewRangeLength; - } - } + const zoomPosition = (ev.offsetX / this.stateController.zoomFactor); + const zoomIn = ev.deltaY < 0; + const newViewRangeLength = Math.max(1, Math.min(this.unitController.absoluteRange, + this.unitController.viewRangeLength * (zoomIn ? 0.8 : 1.25))); + const center = this.unitController.viewRange.start + zoomPosition; + const start = Math.max(0, Math.min(this.unitController.absoluteRange - newViewRangeLength, + center - zoomPosition * newViewRangeLength / this.unitController.viewRangeLength)); + const end = start + newViewRangeLength; this.unitController.viewRange = { start, end @@ -83,6 +68,7 @@ export class TimeGraphAxis extends TimeGraphLayer { } this.unitController.viewRange = { start, end } } + ev.preventDefault(); return false; }); this.onCanvasEvent('mousewheel', mw); diff --git a/timeline-chart/src/layer/time-graph-chart.ts b/timeline-chart/src/layer/time-graph-chart.ts index f37a2f6..3e55819 100644 --- a/timeline-chart/src/layer/time-graph-chart.ts +++ b/timeline-chart/src/layer/time-graph-chart.ts @@ -59,7 +59,6 @@ export class TimeGraphChart extends TimeGraphChartLayer { protected afterAddToContainer() { let mousePositionX = 1; - let mousePositionY = 1; const horizontalDelta = 3; let triggerKeyEvent = false; @@ -88,11 +87,13 @@ export class TimeGraphChart extends TimeGraphChartLayer { this.rowController.verticalOffset = verticalOffset; } - const adjustZoom = (zoomPosition: number, deltaLength: number) => { - const newViewRangeLength = this.unitController.viewRangeLength + deltaLength; - const xOffset = ((zoomPosition / this.unitController.viewRangeLength) * deltaLength); - const start = Math.max(0, this.unitController.viewRange.start - xOffset); - const end = Math.min(start + newViewRangeLength, this.unitController.absoluteRange); + const adjustZoom = (zoomPosition: number, zoomIn: boolean) => { + const newViewRangeLength = Math.max(1, Math.min(this.unitController.absoluteRange, + this.unitController.viewRangeLength * (zoomIn ? 0.8 : 1.25))); + const center = this.unitController.viewRange.start + zoomPosition; + const start = Math.max(0, Math.min(this.unitController.absoluteRange - newViewRangeLength, + center - zoomPosition * newViewRangeLength / this.unitController.viewRangeLength)); + const end = start + newViewRangeLength; if (Math.trunc(start) !== Math.trunc(end)) { this.unitController.viewRange = { start, @@ -103,7 +104,6 @@ export class TimeGraphChart extends TimeGraphChartLayer { const mouseMoveHandler = (event: MouseEvent) => { mousePositionX = event.offsetX; - mousePositionY = event.offsetY; }; const keyDownHandler = (event: KeyboardEvent) => { @@ -112,13 +112,11 @@ export class TimeGraphChart extends TimeGraphChartLayer { if (triggerKeyEvent) { if (keyBoardNavs['zoomin'].indexOf(keyPressed) >= 0) { const zoomPosition = (mousePositionX / this.stateController.zoomFactor); - const deltaLength = -(mousePositionY / this.stateController.zoomFactor); - adjustZoom(zoomPosition, deltaLength); + adjustZoom(zoomPosition, true); } else if (keyBoardNavs['zoomout'].indexOf(keyPressed) >= 0) { const zoomPosition = (mousePositionX / this.stateController.zoomFactor); - const deltaLength = (mousePositionY / this.stateController.zoomFactor); - adjustZoom(zoomPosition, deltaLength); + adjustZoom(zoomPosition, false); } else if (keyBoardNavs['panleft'].indexOf(keyPressed) >= 0) { moveHorizontally(-horizontalDelta); @@ -141,8 +139,8 @@ export class TimeGraphChart extends TimeGraphChartLayer { const mouseWheelHandler = (ev: WheelEvent) => { if (ev.ctrlKey) { const zoomPosition = (ev.offsetX / this.stateController.zoomFactor); - const deltaLength = (ev.deltaY / this.stateController.zoomFactor); - adjustZoom(zoomPosition, deltaLength); + const zoomIn = ev.deltaY < 0; + adjustZoom(zoomPosition, zoomIn); } else if (ev.shiftKey) { moveHorizontally(ev.deltaY);