-
Notifications
You must be signed in to change notification settings - Fork 270
/
index-canvas.html
41 lines (34 loc) · 1.51 KB
/
index-canvas.html
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
37
38
39
40
41
<head>
<style> body { margin: 0; } </style>
<script src="//unpkg.com/react/umd/react.production.min.js"></script>
<script src="//unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="//unpkg.com/@babel/standalone"></script>
<script src="//unpkg.com/react-force-graph-2d"></script>
<!--<script src="../../src/packages/react-force-graph-2d/dist/react-force-graph-2d.js"></script>-->
<script src="../datasets/random-data.js"></script>
</head>
<body>
<div id="graph"></div>
<script type="text/jsx">
function nodePaint({ id, x, y }, color, ctx) {
ctx.fillStyle = color;
[
() => { ctx.fillRect(x - 6, y - 4, 12, 8); }, // rectangle
() => { ctx.beginPath(); ctx.moveTo(x, y - 5); ctx.lineTo(x - 5, y + 5); ctx.lineTo(x + 5, y + 5); ctx.fill(); }, // triangle
() => { ctx.beginPath(); ctx.arc(x, y, 5, 0, 2 * Math.PI, false); ctx.fill(); }, // circle
() => { ctx.font = '10px Sans-Serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('Text', x, y); } // text
][id%4]();
}
// gen a number persistent color from around the palette
const getColor = n => '#' + ((n * 1234567) % Math.pow(2, 24)).toString(16).padStart(6, '0');
ReactDOM.render(
<ForceGraph2D
graphData={genRandomTree(20)}
nodeLabel="id"
nodeCanvasObject={(node, ctx) => nodePaint(node, getColor(node.id), ctx)}
nodePointerAreaPaint={nodePaint}
/>,
document.getElementById('graph')
);
</script>
</body>