全屏展示 Fullscreen
概述
全屏展示插件允许用户将图可视化内容扩展到整个屏幕,提供更广阔的视图和更好的沉浸式体验。
使用场景
全屏展示插件主要适用于以下场景:
- 提供更广阔的视图,便于查看复杂图数据
- 增强沉浸式体验,专注于图可视化内容
- 在演示或报告中展示图数据
基本用法
以下是一个简单的 Fullscreen 插件初始化示例:
const graph = new Graph({
plugins: [
{
type: 'fullscreen',
autoFit: true,
trigger: {
request: 'F', // 使用快捷键 F 进入全屏
exit: 'Esc', // 使用快捷键 Esc 退出全屏
},
onEnter: () => {
console.log('进入全屏模式');
},
onExit: () => {
console.log('退出全屏模式');
},
},
],
});
配置项
| 属性 | 描述 | 类型 | 默认值 | 必选 |
|---|---|---|---|---|
| type | 插件类型 | string | fullscreen |
✓ |
| key | 插件的唯一标识,可用于获取插件实例或更新插件选项 | string | - | |
| autoFit | 是否自适应画布尺寸,全屏后画布尺寸会自动适应屏幕尺寸 | boolean | true | |
| trigger | 触发全屏的方式,示例 | { request?: string; exit?: string; } | - | |
| onEnter | 进入全屏后的回调 | () => void | - | |
| onExit | 退出全屏后的回调 | () => void | - |
trigger
trigger 属性用于控制触发全屏的方式。它支持两种配置方式:
快捷键配置
使用键盘快捷键来触发全屏和退出全屏。
const graph = new Graph({
plugins: [
{
type: 'fullscreen',
trigger: {
request: 'F', // 使用快捷键 F 进入全屏
exit: 'Esc', // 使用快捷键 Esc 退出全屏
},
},
],
});
自定义触发
通过调用 request 和 exit 方法来控制全屏。
const graph = new Graph({
plugins: [
{
type: 'fullscreen',
key: 'my-fullscreen',
},
],
});
// 进入全屏
graph.getPluginInstance('my-fullscreen').request();
// 退出全屏
graph.getPluginInstance('my-fullscreen').exit();
autoFit
是否自适应画布尺寸,全屏后画布尺寸会自动适应屏幕尺寸。
- 设置为 true 时,画布会自动调整大小以适应整个屏幕。
- 设置为 false 时,画布大小保持不变。
const graph = new Graph({
plugins: [
{
type: 'fullscreen',
autoFit: true,
},
],
});
API
Fullscreen.request()
这个方法可以让你通过代码进入全屏模式。调用插件实例上的这个方法,就能把图形内容扩展到整个屏幕。
const graph = new Graph({
plugins: [
{
type: 'fullscreen',
key: 'my-fullscreen',
},
],
});
// 进入全屏
graph.getPluginInstance('my-fullscreen').request();
Fullscreen.exit()
这个方法可以让你通过代码退出全屏模式。调用插件实例上的这个方法,就能把图形内容恢复到原来的大小。
const graph = new Graph({
plugins: [
{
type: 'fullscreen',
key: 'my-fullscreen',
},
],
});
// 退出全屏
graph.getPluginInstance('my-fullscreen').exit();
实际案例
import { Graph } from '@antv/g6';
const graph = new Graph({
data: { nodes: Array.from({ length: 20 }).map((_, i) => ({ id: `node${i}` })) },
autoFit: 'center',
background: '#fff',
plugins: [
{
type: 'fullscreen',
key: 'fullscreen',
},
function () {
const graph = this;
return {
type: 'toolbar',
key: 'toolbar',
position: 'top-left',
onClick: (item) => {
const fullscreenPlugin = graph.getPluginInstance('fullscreen');
if (item === 'request-fullscreen') {
fullscreenPlugin.request();
}
if (item === 'exit-fullscreen') {
fullscreenPlugin.exit();
}
},
getItems: () => {
return [
{ id: 'request-fullscreen', value: 'request-fullscreen' },
{ id: 'exit-fullscreen', value: 'exit-fullscreen' },
];
},
};
},
],
});
graph.render();
import { Graph } from '@antv/g6';
import GUI from 'lil-gui';
export function addPanel(renderPanel: (gui: GUI) => void) {
const gui = new GUI({ container: document.body });
gui.title('Control');
Object.assign(gui.domElement.style, { position: 'absolute', top: '0', right: '0', zIndex: '10' });
renderPanel(gui);
addEventListener('pagehide', () => gui.destroy(), { once: true });
}
export async function createGraph(
options: ConstructorParameters<typeof Graph>[0],
size: { width?: number; height?: number } = {},
renderPanel?: (gui: GUI, graph: Graph) => void,
) {
const container = document.createElement('div');
Object.assign(container.style, {
width: '100%',
maxWidth: `${size.width || 600}px`,
height: `${size.height || 400}px`,
});
document.getElementById('container')!.append(container);
const graph = new Graph({ ...size, ...options, width: container.clientWidth, container, autoResize: true });
addEventListener('pagehide', () => graph.destroy(), { once: true });
await graph.render();
if (renderPanel) addPanel((gui) => renderPanel(gui, graph));
return container;
}