fix: fix unexpected canvas access after switch renderer

This commit is contained in:
Aaron 2024-08-01 17:12:35 +08:00
parent 12611b1b70
commit 432c700a5c
3 changed files with 57 additions and 12 deletions

View File

@ -1,11 +1,14 @@
import { Renderer as CanvasRenderer } from '@antv/g-canvas';
import type { NodeData } from '@antv/g6';
import { ExtensionCategory, Graph, register } from '@antv/g6';
import { Light, Sphere, renderer } from '../../src';
import { Light, Line3D, ObserveCanvas3D, Sphere, ZoomCanvas3D, renderer } from '../../src';
export const switchRenderer: TestCase = async (context) => {
register(ExtensionCategory.PLUGIN, '3d-light', Light);
register(ExtensionCategory.NODE, 'sphere', Sphere);
register(ExtensionCategory.EDGE, 'line3d', Line3D);
register(ExtensionCategory.BEHAVIOR, 'observe-canvas-3d', ObserveCanvas3D);
register(ExtensionCategory.BEHAVIOR, 'zoom-canvas-3d', ZoomCanvas3D);
const nodes: NodeData[] = [{ id: '1' }, { id: '2' }];
@ -26,10 +29,35 @@ export const switchRenderer: TestCase = async (context) => {
if (name === '2d') {
graph.setOptions({
renderer: () => new CanvasRenderer(),
behaviors: [
'zoom-canvas',
'drag-canvas',
'drag-element',
{
type: 'hover-activate',
degree: 1,
state: 'highlight',
},
],
node: {
type: 'circle',
state: {
highlight: {
fill: '#D580FF',
},
},
},
edge: {
style: {
labelBackgroundFill: '#FFF',
labelBackground: true,
},
},
layout: {
type: 'force',
preventOverlap: true,
animation: false,
},
plugins: [],
});
} else {
graph.setOptions({
@ -40,14 +68,32 @@ export const switchRenderer: TestCase = async (context) => {
materialType: 'phong',
},
},
edge: {
type: 'line3d',
},
plugins: [
{
type: 'camera-setting',
projectionMode: 'orthographic',
near: 1,
far: 10000,
fov: 45,
aspect: 1,
},
{
type: '3d-light',
directional: {
direction: [0, 0, 1],
},
},
{
type: 'background',
backgroundImage:
'url(https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*M_OaRrzIZOEAAAAAAAAAAAAADmJ7AQ/original)',
backgroundPosition: 'center',
},
],
behaviors: ['observe-canvas-3d', 'zoom-canvas-3d'],
});
}

View File

@ -174,6 +174,7 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
public destroy(): void {
this.shortcut.destroy();
this.unbindEvents();
this.context.canvas.setCursor(this.defaultCursor);
super.destroy();
}

View File

@ -1,4 +1,4 @@
import { clamp, isArray, isFunction, isObject } from '@antv/util';
import { clamp, isFunction } from '@antv/util';
import { CommonEvent } from '../constants';
import type { RuntimeContext } from '../runtime/types';
import type { IKeyboardEvent, IWheelEvent, Point, PointObject, ViewportAnimationEffectTiming } from '../types';
@ -98,15 +98,15 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
const { trigger } = this.options;
this.shortcut.unbindAll();
if (isArray(trigger)) {
this.preventDefault(CommonEvent.WHEEL);
if (Array.isArray(trigger)) {
this.context.canvas.getContainer()?.addEventListener(CommonEvent.WHEEL, this.preventDefault);
this.shortcut.bind([...trigger, CommonEvent.WHEEL], (event) => {
const { deltaX, deltaY } = event;
this.zoom(-(deltaY ?? deltaX), event, false);
});
}
if (isObject(trigger)) {
if (typeof trigger === 'object') {
const {
zoomIn = [],
zoomOut = [],
@ -170,12 +170,9 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
return !!enable;
}
private preventDefault(eventName: string) {
const listener = (e: Event) => e.preventDefault();
const container = this.context.canvas.getContainer();
if (!container) return;
container.addEventListener(eventName, listener);
}
private preventDefault = (event: Event) => {
event.preventDefault();
};
/**
* <zh/>
@ -184,6 +181,7 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
*/
public destroy() {
this.shortcut.destroy();
this.context.canvas.getContainer()?.removeEventListener(CommonEvent.WHEEL, this.preventDefault);
super.destroy();
}
}