g6/packages/site/examples/feature/features/demo/webgl3d.js

127 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-08-30 10:59:32 +08:00
import { Graph, Extensions, extend } from '@antv/g6';
const ExtGraph = extend(Graph, {
nodes: {
'sphere-node': Extensions.SphereNode,
'cube-node': Extensions.CubeNode,
2023-08-30 10:59:32 +08:00
},
behaviors: {
'orbit-canvas-3d': Extensions.OrbitCanvas3D,
'zoom-canvas-3d': Extensions.ZoomCanvas3D,
},
});
const container = document.getElementById('container');
const width = container.scrollWidth;
const height = container.scrollHeight || 500;
2023-08-30 14:21:51 +08:00
const getDegrees = (data) => {
const degrees = {};
data.edges.forEach((edge) => {
const { source, target } = edge;
degrees[source] = degrees[source] || 0;
degrees[target] = degrees[target] || 0;
degrees[source]++;
degrees[target]++;
});
return degrees;
};
fetch('https://assets.antv.antgroup.com/g6/eva-3d-data.json')
2023-08-30 14:21:51 +08:00
.then((res) => res.json())
.then((data) => {
const degrees = getDegrees(data);
const graph = new ExtGraph({
container: 'container',
width,
height,
renderer: 'webgl-3d',
modes: {
default: ['drag-canvas', 'drag-node', 'zoom-canvas'],
2023-08-30 10:59:32 +08:00
},
2023-08-30 14:21:51 +08:00
modes: {
default: [
{
type: 'orbit-canvas-3d',
trigger: 'drag',
},
'zoom-canvas-3d',
],
2023-08-30 10:59:32 +08:00
},
2023-08-30 14:21:51 +08:00
theme: {
type: 'spec',
base: 'dark',
specification: {
node: {
dataTypeField: 'cluster',
},
2023-08-30 10:59:32 +08:00
},
},
2023-08-30 14:21:51 +08:00
edge: (innerModel) => {
return {
...innerModel,
data: {
...innerModel.data,
keyShape: {
2023-08-31 18:16:13 +08:00
lineWidth: 0.4,
opacity: 0.4,
2023-08-30 14:21:51 +08:00
stroke: '#fff',
},
type: 'line-edge',
},
};
2023-08-30 10:59:32 +08:00
},
2023-08-30 14:21:51 +08:00
node: (innerModel) => {
const isCube = ['c148', 'c498', 'c41', 'c254'].includes(innerModel.data.cluster);
2023-08-30 14:21:51 +08:00
return {
...innerModel,
data: {
...innerModel.data,
type: isCube ? 'cube-node' : 'sphere-node',
keyShape: isCube
? {
width: 400,
height: 400,
depth: 400,
}
: {
r: 12 + degrees[innerModel.id] / 2,
},
2023-08-30 14:21:51 +08:00
labelShape:
degrees[innerModel.id] > 20
? {
text: innerModel.data.label,
fontSize: 100,
lod: -1,
fill: 'rgba(255,255,255,0.85)',
wordWrap: false,
2023-08-30 14:21:51 +08:00
isBillboard: true,
}
: undefined,
},
};
},
data,
});
let frame;
graph.on('afterlayout', () => {
const camera = graph.canvas.getCamera();
let counter = 0;
const tick = () => {
if (counter < 80) {
camera.dolly(4);
}
camera.rotate(0.4, 0);
counter++;
frame = requestAnimationFrame(tick);
if (counter > 160 && frame) {
cancelAnimationFrame(frame);
}
};
tick();
});
graph.on('pointerdown', (e) => {
console.log('frame', frame);
if (frame) cancelAnimationFrame(frame);
});
window.graph = graph;
2023-08-30 10:59:32 +08:00
});