UNPKG

@antv/g6

Version:

A Graph Visualization Framework in JavaScript

126 lines 5.07 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { clamp, isFunction } from '@antv/util'; import { CommonEvent } from '../constants'; import { parsePoint } from '../utils/point'; import { Shortcut } from '../utils/shortcut'; import { BaseBehavior } from './base-behavior'; /** * <zh/> 缩放画布交互 * * <en/> Zoom canvas behavior */ export class ZoomCanvas extends BaseBehavior { constructor(context, options) { super(context, Object.assign({}, ZoomCanvas.defaultOptions, options)); /** * <zh/> 缩放画布 * * <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 */ this.zoom = (value, event, animation) => __awaiter(this, void 0, void 0, function* () { if (!this.validate(event)) return; const { graph } = this.context; let origin = this.options.origin; if (!origin && 'viewport' in event) { origin = parsePoint(event.viewport); } const { sensitivity, onFinish } = this.options; const ratio = 1 + (clamp(value, -50, 50) * sensitivity) / 100; const zoom = graph.getZoom(); yield graph.zoomTo(zoom * ratio, animation, origin); onFinish === null || onFinish === void 0 ? void 0 : onFinish(); }); this.onReset = () => __awaiter(this, void 0, void 0, function* () { yield this.context.graph.zoomTo(1, this.options.animation); }); this.preventDefault = (event) => { if (this.options.preventDefault) event.preventDefault(); }; this.shortcut = new Shortcut(context.graph); this.bindEvents(); } /** * <zh/> 更新配置 * * <en/> Update options * @param options - <zh/> 配置项 | <en/> Options * @internal */ update(options) { super.update(options); this.bindEvents(); } bindEvents() { const { trigger } = this.options; this.shortcut.unbindAll(); if (Array.isArray(trigger)) { if (trigger.includes(CommonEvent.PINCH)) { this.shortcut.bind([CommonEvent.PINCH], (event) => { this.zoom(event.scale, event, false); }); } else { const container = this.context.canvas.getContainer(); container === null || container === void 0 ? void 0 : container.addEventListener(CommonEvent.WHEEL, this.preventDefault); this.shortcut.bind([...trigger, CommonEvent.WHEEL], (event) => { const { deltaX, deltaY } = event; this.zoom(-(deltaY !== null && deltaY !== void 0 ? deltaY : deltaX), event, false); }); } } if (typeof trigger === 'object') { const { zoomIn = [], zoomOut = [], reset = [], } = trigger; 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); } } /** * <zh/> 验证是否可以缩放 * * <en/> Verify whether it can be zoomed * @param event - <zh/> 事件对象 | <en/> Event object * @returns <zh/> 是否可以缩放 | <en/> Whether it can be zoomed * @internal */ validate(event) { if (this.destroyed) return false; const { enable } = this.options; if (isFunction(enable)) return enable(event); return !!enable; } /** * <zh/> 销毁缩放画布 * * <en/> Destroy zoom canvas */ destroy() { var _a; this.shortcut.destroy(); (_a = this.context.canvas.getContainer()) === null || _a === void 0 ? void 0 : _a.removeEventListener(CommonEvent.WHEEL, this.preventDefault); super.destroy(); } } ZoomCanvas.defaultOptions = { animation: { duration: 200 }, enable: true, sensitivity: 1, trigger: [], preventDefault: true, }; //# sourceMappingURL=zoom-canvas.js.map