Skip to content

Commit

Permalink
refactor(behavior): optimize zoom canvas experience (#5584)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored Mar 22, 2024
1 parent b57cdfb commit 7024c5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('behavior zoom canvas', () => {
it('zoom in', async () => {
graph.emit(CommonEvent.WHEEL, { deltaY: -10 });

expect(graph.getZoom()).toBe(2);
expect(graph.getZoom()).toBe(1.1);

await expect(graph).toMatchSnapshot(__filename);
});
Expand All @@ -29,11 +29,11 @@ describe('behavior zoom canvas', () => {

graph.emit(CommonEvent.WHEEL, { deltaY: 5 });

expect(graph.getZoom()).toBe(currentZoom - 0.5);
expect(graph.getZoom()).toBe(currentZoom * 0.95);

graph.emit(CommonEvent.WHEEL, { deltaY: 5 });

expect(graph.getZoom()).toBe(currentZoom - 1);
expect(graph.getZoom()).toBeCloseTo(currentZoom * 0.95 ** 2);
});

const shortcutZoomCanvasOptions: ZoomCanvasOptions = {
Expand All @@ -59,7 +59,7 @@ describe('behavior zoom canvas', () => {
graph.emit(CommonEvent.KEY_DOWN, { key: 'Control' });
graph.emit(CommonEvent.KEY_DOWN, { key: '=' });

expect(graph.getZoom()).toBe(currentZoom + 0.1);
expect(graph.getZoom()).toBe(currentZoom * 1.1);

graph.emit(CommonEvent.KEY_UP, { key: 'Control' });
graph.emit(CommonEvent.KEY_UP, { key: '=' });
Expand All @@ -68,7 +68,7 @@ describe('behavior zoom canvas', () => {
graph.emit(CommonEvent.KEY_DOWN, { key: 'Control' });
graph.emit(CommonEvent.KEY_DOWN, { key: '0' });

expect(graph.getZoom()).toBe(currentZoom);
expect(graph.getZoom()).toBe(1);

graph.emit(CommonEvent.KEY_UP, { key: 'Control' });
graph.emit(CommonEvent.KEY_UP, { key: '0' });
Expand All @@ -77,7 +77,7 @@ describe('behavior zoom canvas', () => {
graph.emit(CommonEvent.KEY_DOWN, { key: 'Control' });
graph.emit(CommonEvent.KEY_DOWN, { key: '-' });

expect(graph.getZoom()).toBe(currentZoom - 0.1);
expect(graph.getZoom()).toBe(0.9);

graph.emit(CommonEvent.KEY_UP, { key: 'Control' });
graph.emit(CommonEvent.KEY_UP, { key: '-' });
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('behavior zoom canvas', () => {
graph.emit(CommonEvent.KEY_UP, { key: 'Control' });
graph.emit(CommonEvent.KEY_UP, { key: '=' });

expect(graph.getZoom()).toBe(currentZoom + 0.1);
expect(graph.getZoom()).toBe(currentZoom * 1.1);
});

it('preconditionKey', () => {
Expand All @@ -147,7 +147,7 @@ describe('behavior zoom canvas', () => {

graph.emit(CommonEvent.KEY_DOWN, { key: 'Control' });
graph.emit(CommonEvent.WHEEL, { deltaY: -10 });
expect(graph.getZoom()).toBe(currentZoom + 1);
expect(graph.getZoom()).toBe(currentZoom * 1.1);
});

it('canvas event', () => {
Expand Down
19 changes: 12 additions & 7 deletions packages/g6/src/behaviors/zoom-canvas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PointLike } from '@antv/g';
import { isArray, isFunction, isObject } from '@antv/util';
import { clamp, isArray, isFunction, isObject } from '@antv/util';
import { CanvasEvent } from '../constants';
import type { RuntimeContext } from '../runtime/types';
import type { BehaviorEvent, Point, ViewportAnimationEffectTiming } from '../types';
Expand Down Expand Up @@ -87,14 +87,14 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
this.preventDefault(CanvasEvent.WHEEL);
this.shortcut.bind([...trigger, CanvasEvent.WHEEL], (event) => {
const { deltaX, deltaY } = event;
this.zoom(-(deltaY ?? deltaX), event);
this.zoom(-(deltaY ?? deltaX), event, false);
});
}

if (isObject(trigger)) {
const { zoomIn = [], zoomOut = [], reset = [] } = trigger as CombinationKey;
this.shortcut.bind(zoomIn, (event) => this.zoom(1, event));
this.shortcut.bind(zoomOut, (event) => this.zoom(-1, event));
this.shortcut.bind(zoomIn, (event) => this.zoom(10, event, this.options.animation));
this.shortcut.bind(zoomOut, (event) => this.zoom(-10, event, this.options.animation));
this.shortcut.bind(reset, this.onReset);
}
}
Expand All @@ -105,8 +105,13 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
* <en/> Zoom canvas
* @param value - <zh/> 缩放值, > 0 放大, < 0 缩小 | <en/> Zoom value, > 0 zoom in, < 0 zoom out
* @param event - <zh/> 事件对象 | <en/> Event object
* @param animation - <zh/> 缩放动画配置 | <en/> Zoom animation configuration
*/
private zoom = async (value: number, event: BehaviorEvent<WheelEvent> | BehaviorEvent<KeyboardEvent>) => {
private zoom = async (
value: number,
event: BehaviorEvent<WheelEvent> | BehaviorEvent<KeyboardEvent>,
animation: ZoomCanvasOptions['animation'],
) => {
if (!this.validate(event)) return;
const { graph } = this.context;

Expand All @@ -116,9 +121,9 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
}

const { sensitivity, onfinish } = this.options;
const diff = (value * sensitivity) / 10;
const ratio = 1 + (clamp(value, -50, 50) * sensitivity) / 100;
const zoom = graph.getZoom();
await graph.zoomTo(zoom + diff, this.options.animation, origin);
await graph.zoomTo(zoom * ratio, animation, origin);

onfinish?.();
};
Expand Down

0 comments on commit 7024c5e

Please sign in to comment.