Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix unexpected canvas access after switch renderer #6110

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions packages/g6-extension-3d/__tests__/demos/switch-renderer.ts
Original file line number Diff line number Diff line change
@@ -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' }];

Expand All @@ -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({
Expand All @@ -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'],
});
}

Expand Down
33 changes: 0 additions & 33 deletions packages/g6/__tests__/demos/canvas-switch-renderer.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/g6/__tests__/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export { behaviorLassoSelect } from './behavior-lasso-select';
export { behaviorOptimizeViewportTransform } from './behavior-optimize-viewport-transform';
export { behaviorScrollCanvas } from './behavior-scroll-canvas';
export { behaviorZoomCanvas } from './behavior-zoom-canvas';
export { canvasSwitchRenderer } from './canvas-switch-renderer';
export { caseIndentedTree } from './case-indented-tree';
export { caseOrgChart } from './case-org-chart';
export { elementCombo } from './combo';
Expand Down
1 change: 1 addition & 0 deletions packages/g6/src/behaviors/drag-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 8 additions & 10 deletions packages/g6/src/behaviors/zoom-canvas.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 = [],
Expand Down Expand Up @@ -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/> 销毁缩放画布
Expand All @@ -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();
}
}
Loading