forked from sgratzl/cytoscape.js-layers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-fixed-label.ts
36 lines (35 loc) · 1.01 KB
/
node-fixed-label.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* eslint-disable @typescript-eslint/no-namespace */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
namespace NodeFixedHTMLLabels {
declare const cytoscape: typeof import('cytoscape');
declare const CytoscapeLayers: typeof import('../build');
const cy = cytoscape({
container: document.getElementById('app'),
layout: {
name: 'grid',
},
elements: [
...Array(100)
.fill(0)
.map((_, i) => ({ data: { id: `i${i}`, label: `L${i}` } })),
],
});
const layers = CytoscapeLayers.layers(cy);
// render centered labels on each node
layers.renderPerNode(
layers.append('html'),
(elem) => {
elem.style.transform = `${elem.style.transform}scale(${1 / cy.zoom()})`;
},
{
init: (elem, node) => {
elem.classList.add('node-label');
elem.textContent = node.data('label') || node.id();
},
transform: 'translate(-50%,-50%)',
position: 'center',
uniqueElements: true,
checkBounds: true,
}
);
}