-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] improved zoom performance (#865)
* [FEAT] improve zoom for better user experience - throttle for cumulativeZoom calculation - debounce for scaleAndTranslate to limit excessive browser painting Co-authored-by: Céline Souchet <[email protected]> Co-authored-by: Thomas Bouffard <[email protected]>
- Loading branch information
1 parent
2840f31
commit 61271b4
Showing
14 changed files
with
293 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright 2020 Bonitasoft S.A. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { ZoomConfiguration } from '../options'; | ||
|
||
export function ensureInRange(value: number, min: number, max: number, defaultValue: number): number { | ||
let inRangeValue = value == undefined ? defaultValue : value; | ||
inRangeValue = Math.min(Math.max(inRangeValue, min), max); | ||
return inRangeValue; | ||
} | ||
|
||
/** | ||
* Make sure the configuration parameters are defined and in range | ||
* @param config the {@link ZoomConfiguration} to make valid | ||
*/ | ||
export function ensureValidZoomConfiguration(config: ZoomConfiguration): ZoomConfiguration { | ||
const validatedConfig = config ?? {}; | ||
validatedConfig.debounceDelay = ensureInRange(validatedConfig.debounceDelay, 0, 100, 50); | ||
validatedConfig.throttleDelay = ensureInRange(validatedConfig.throttleDelay, 0, 100, 50); | ||
return validatedConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,14 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { FitOptions, FitType } from '../options'; | ||
import { FitOptions, FitType, ZoomConfiguration } from '../options'; | ||
import { mxgraph } from 'ts-mxgraph'; | ||
import { ensureValidZoomConfiguration } from '../helpers/validators'; | ||
import debounce from 'lodash.debounce'; | ||
import throttle from 'lodash.throttle'; | ||
|
||
// TODO unable to load mxClient from [email protected] | ||
declare const mxClient: typeof mxgraph.mxClient; | ||
|
||
export class BpmnMxGraph extends mxGraph { | ||
private cumulativeZoomFactor = 1; | ||
|
@@ -74,23 +81,54 @@ export class BpmnMxGraph extends mxGraph { | |
return Math.max(input || 0, 0); | ||
} | ||
|
||
// solution inspired by https://github.com/algenty/grafana-flowcharting/blob/0.9.0/src/graph_class.ts#L1254 | ||
public performZoom(up: boolean, evt: MouseEvent): void { | ||
const rect = this.container.getBoundingClientRect(); | ||
const x = evt.clientX - rect.left; | ||
const y = evt.clientY - rect.top; | ||
this.zoomTo(null, null, up, x, y); | ||
} | ||
|
||
zoomTo(scale: number, center?: boolean, up?: boolean, offsetX?: number, offsetY?: number): void { | ||
zoomTo(scale: number, center?: boolean, up?: boolean, offsetX?: number, offsetY?: number, performScaling?: boolean): void { | ||
if (scale === null) { | ||
const [newScale, dx, dy] = this.getScaleAndTranslationDeltas(up, offsetX, offsetY); | ||
this.view.scaleAndTranslate(newScale, this.view.translate.x + dx, this.view.translate.y + dy); | ||
if (performScaling) { | ||
this.view.scaleAndTranslate(newScale, this.view.translate.x + dx, this.view.translate.y + dy); | ||
} | ||
} else { | ||
super.zoomTo(scale, center); | ||
} | ||
} | ||
|
||
createMouseWheelZoomExperience(config: ZoomConfiguration): void { | ||
config = ensureValidZoomConfiguration(config); | ||
mxEvent.addMouseWheelListener(debounce(this.getZoomHandler(true), config.debounceDelay), this.container); | ||
mxEvent.addMouseWheelListener(throttle(this.getZoomHandler(false), config.throttleDelay), this.container); | ||
} | ||
|
||
// solution inspired by https://github.com/algenty/grafana-flowcharting/blob/0.9.0/src/graph_class.ts#L1254 | ||
private performZoom(up: boolean, evt: MouseEvent, performScaling: boolean): void { | ||
const [x, y] = this.getRelativeEventCoordinates(evt); | ||
this.zoomTo(null, null, up, x, y, performScaling); | ||
if (performScaling) { | ||
mxEvent.consume(evt); | ||
} | ||
} | ||
|
||
private getZoomHandler(calculateFactorOnly: boolean) { | ||
return (event: Event, up: boolean) => { | ||
// TODO review type: this hack is due to the introduction of mxgraph-type-definitions | ||
const evt = (event as unknown) as MouseEvent; | ||
if (mxEvent.isConsumed((evt as unknown) as mxMouseEvent)) { | ||
return; | ||
} | ||
// only the ctrl key or the meta key on mac | ||
const isZoomWheelEvent = (evt.ctrlKey || (mxClient.IS_MAC && evt.metaKey)) && !evt.altKey && !evt.shiftKey; | ||
if (isZoomWheelEvent) { | ||
this.performZoom(up, evt, calculateFactorOnly); | ||
} | ||
}; | ||
} | ||
|
||
private getRelativeEventCoordinates(evt: MouseEvent): [number, number] { | ||
const rect = this.container.getBoundingClientRect(); | ||
const x = evt.clientX - rect.left; | ||
const y = evt.clientY - rect.top; | ||
return [x, y]; | ||
} | ||
|
||
private getScaleAndTranslationDeltas(up: boolean, offsetX: number, offsetY: number): [number, number, number] { | ||
let dx = offsetX * 2; | ||
let dy = offsetY * 2; | ||
|
@@ -113,7 +151,8 @@ export class BpmnMxGraph extends mxGraph { | |
} | ||
|
||
private calculateFactorAndScale(up: boolean): [number, number] { | ||
this.cumulativeZoomFactor *= up ? 1.25 : 0.8; | ||
// as with new zoom scaling is invoked 2x the factor's square root is taken | ||
this.cumulativeZoomFactor *= up ? Math.sqrt(1.25) : Math.sqrt(0.8); | ||
let factor = this.cumulativeZoomFactor / this.view.scale; | ||
const scale = Math.round(this.view.scale * factor * 100) / 100; | ||
factor = scale / this.view.scale; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,7 @@ import ShapeConfigurator from './config/ShapeConfigurator'; | |
import MarkerConfigurator from './config/MarkerConfigurator'; | ||
import MxClientConfigurator from './config/MxClientConfigurator'; | ||
import { GlobalOptions } from '../options'; | ||
import { mxgraph } from 'ts-mxgraph'; | ||
import { BpmnMxGraph } from './BpmnMxGraph'; | ||
// TODO unable to load mxClient from [email protected] | ||
declare const mxClient: typeof mxgraph.mxClient; | ||
|
||
/** | ||
* Configure the BpmnMxGraph graph that can be used by the lib | ||
|
@@ -72,12 +69,13 @@ export default class MxGraphConfigurator { | |
this.graph.panningHandler.addListener(mxEvent.PAN_START, this.getPanningHandler('grab')); | ||
this.graph.panningHandler.addListener(mxEvent.PAN_END, this.getPanningHandler('default')); | ||
this.graph.setPanning(true); | ||
|
||
// Zoom configuration | ||
this.graph.createMouseWheelZoomExperience(options.zoomConfiguration); | ||
} else { | ||
this.graph.setPanning(false); | ||
this.graph.panningHandler.setPinchEnabled(false); // ensure gesture support is disabled (zoom only for now!) | ||
} | ||
|
||
this.configureMouseEvent(mouseNavigationSupport); | ||
} | ||
|
||
private getPanningHandler(cursor: 'grab' | 'default'): OmitThisParameter<(this: BpmnMxGraph) => void> { | ||
|
@@ -89,24 +87,4 @@ export default class MxGraphConfigurator { | |
this.isEnabled() && (this.container.style.cursor = cursor); | ||
}; | ||
} | ||
|
||
private configureMouseEvent(activated = false): void { | ||
if (!activated) { | ||
return; | ||
} | ||
|
||
mxEvent.addMouseWheelListener((event: Event, up: boolean) => { | ||
// TODO review type: this hack is due to the introduction of mxgraph-type-definitions | ||
const evt = (event as unknown) as MouseEvent; | ||
if (mxEvent.isConsumed((evt as unknown) as mxMouseEvent)) { | ||
return; | ||
} | ||
// only the ctrl key or the meta key on mac | ||
const isZoomWheelEvent = (evt.ctrlKey || (mxClient.IS_MAC && evt.metaKey)) && !evt.altKey && !evt.shiftKey; | ||
if (isZoomWheelEvent) { | ||
this.graph.performZoom(up, evt); | ||
mxEvent.consume(evt); | ||
} | ||
}, this.container); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.