UNPKG

@antv/x6

Version:

JavaScript diagramming library that uses SVG and HTML for rendering.

149 lines 6.49 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import JQuery from 'jquery'; import { ModifierKey } from '../types'; import { Dom, NumberExt, Platform } from '../util'; import { Disposable } from '../common'; export class MouseWheel extends Disposable { constructor(options) { super(); this.options = options; this.cumulatedFactor = 1; const scroller = this.graph.scroller.widget; this.container = scroller ? scroller.container : this.graph.container; this.target = this.options.global ? document : this.container; this.eventName = Platform.isEventSupported('wheel') ? 'wheel' : 'mousewheel'; this.handler = this.onMouseWheel.bind(this); if (this.options.enabled) { this.enable(true); } } get graph() { return this.options.graph; } get disabled() { return this.options.enabled !== true; } enable(force) { if (this.disabled || force) { this.options.enabled = true; this.graph.options.mousewheel.enabled = true; if (Platform.SUPPORT_PASSIVE) { this.target.addEventListener(this.eventName, this.handler, { passive: false, }); } else { JQuery(this.target).on('mousewheel', this.handler); } } } disable() { if (!this.disabled) { this.options.enabled = false; this.graph.options.mousewheel.enabled = false; if (Platform.SUPPORT_PASSIVE) { this.target.removeEventListener(this.eventName, this.handler); } else { JQuery(this.target).off('mousewheel'); } } } onMouseWheel(evt) { const e = (evt.originalEvent || evt); const guard = this.options.guard; if ((guard == null || guard.call(this.graph, e)) && ModifierKey.isMatch(e, this.options.modifiers)) { evt.preventDefault(); evt.stopPropagation(); if (this.frameId) { Dom.cancelAnimationFrame(this.frameId); this.frameId = null; } const factor = this.options.factor || 1.2; if (this.currentScale == null) { this.startPos = { x: evt.clientX, y: evt.clientY }; this.currentScale = this.graph.scroller.widget ? this.graph.scroller.widget.zoom() : this.graph.transform.getScale().sx; } const delta = evt.deltaY; if (delta < 0) { // zoomin // ------ // Switches to 1% zoom steps below 15% if (this.currentScale * this.cumulatedFactor < 0.15) { this.cumulatedFactor = (this.currentScale + 0.01) / this.currentScale; } else { // Uses to 5% zoom steps for better grid rendering in // webkit and to avoid rounding errors for zoom steps this.cumulatedFactor *= factor; } } else { // zoomout // ------- // Switches to 1% zoom steps below 15% if (this.currentScale * this.cumulatedFactor <= 0.15) { this.cumulatedFactor = (this.currentScale - 0.01) / this.currentScale; } else { // Uses to 5% zoom steps for better grid rendering in // webkit and to avoid rounding errors for zoom steps this.cumulatedFactor /= factor; } } this.cumulatedFactor = Math.round(this.currentScale * this.cumulatedFactor * 20) / 20 / this.currentScale; this.cumulatedFactor = Math.max(0.01, Math.min(this.currentScale * this.cumulatedFactor, 160) / this.currentScale); this.frameId = Dom.requestAnimationFrame(() => { const scroller = this.graph.scroller.widget; const currentScale = this.currentScale; let targetScale = this.graph.transform.clampScale(currentScale * this.cumulatedFactor); const minScale = this.options.minScale || Number.MIN_SAFE_INTEGER; const maxScale = this.options.maxScale || Number.MAX_SAFE_INTEGER; targetScale = NumberExt.clamp(targetScale, minScale, maxScale); if (targetScale !== currentScale) { if (scroller) { if (this.options.zoomAtMousePosition) { const origin = this.graph.coord.clientToLocalPoint(this.startPos); scroller.zoom(targetScale, { absolute: true, center: origin.clone(), }); } else { scroller.zoom(targetScale, { absolute: true }); } } else { if (this.options.zoomAtMousePosition) { const origin = this.graph.coord.clientToLocalPoint(this.startPos); this.graph.translate(origin.x * (1 - targetScale), origin.y * (1 - targetScale)); } this.graph.scale(targetScale, targetScale); } } this.frameId = null; this.currentScale = null; this.cumulatedFactor = 1; }); } } dispose() { this.disable(); } } __decorate([ Disposable.dispose() ], MouseWheel.prototype, "dispose", null); //# sourceMappingURL=mousewheel.js.map