FocusElement 聚焦元素
上一篇
FixElementSize 缩放画布时固定元素大小
下一篇
HoverActivate 悬停激活
Loading...
FocusElement 是 G6 中用于实现元素聚焦功能的内置交互,支持通过点击元素将其聚焦到视图中心。这个交互可以帮助用户快速定位和关注特定的图元素。
createGraph({data: {nodes: [{ id: 'node-1', style: { x: 200, y: 100 } },{ id: 'node-2', style: { x: 360, y: 100 } },{ id: 'node-3', style: { x: 280, y: 220 } },],edges: [{ source: 'node-1', target: 'node-2' },{ source: 'node-1', target: 'node-3' },{ source: 'node-2', target: 'node-3' },],},node: { style: { fill: '#7e3feb' } },edge: { style: { stroke: '#8b9baf' } },behaviors: [{type: 'focus-element',key: 'focus-element',},],plugins: [{ type: 'grid-line', size: 30 }],animation: true,},{ width: 600, height: 300 },(gui, graph) => {const options = {key: 'focus-element',type: 'focus-element',animation: true,enable: true,};const optionFolder = gui.addFolder('FocusElement Options');optionFolder.add(options, 'type').disable(true);optionFolder.add(options, 'animation');optionFolder.add(options, 'enable');optionFolder.onChange(({ property, value }) => {graph.updateBehavior({key: 'focus-element',[property]: value,});graph.render();});},);
在图配置中添加这一交互:
1. 快速配置(静态)
使用字符串形式直接声明:
const graph = new Graph({// 其他配置...behaviors: ['focus-element'],});
2. 对象配置(推荐)
使用对象形式进行配置,支持自定义参数:
const graph = new Graph({// 其他配置...behaviors: [{type: 'focus-element',animation: {duration: 500,easing: 'ease-in',},},],});
配置项 | 说明 | 类型 | 默认值 | 必选 |
---|---|---|---|---|
type | 交互类型名称 | string | focus-element | ✓ |
animation | 聚焦动画效果设置 | ViewportAnimationEffectTiming | { duration: 500, easing: 'ease-in' } | |
enable | 是否启用聚焦功能 | boolean | ((event: IElementEvent) => boolean) | true |
type ViewportAnimationEffectTiming =| boolean // true 启用默认动画,false 禁用动画| {easing?: string; // 动画缓动函数:'ease-in-out'、'ease-in'、'ease-out'、'linear'duration?: number; // 动画持续时间(毫秒)};
const graph = new Graph({container: 'container',width: 800,height: 600,behaviors: ['focus-element'],});
const graph = new Graph({// 其他配置...behaviors: [{type: 'focus-element',animation: {duration: 800,easing: 'ease-in-out',},},],});
const graph = new Graph({// 其他配置...behaviors: [{type: 'focus-element',enable: (event) => {// 只对节点启用聚焦,边不聚焦return event.target.type === 'node';},},],});
destroy(): void;