-
Notifications
You must be signed in to change notification settings - Fork 1
/
Visualizer.js
133 lines (108 loc) · 4.35 KB
/
Visualizer.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {AmbientLight, PerspectiveCamera, Scene, Vector3, WebGLRenderer,} from "three"
import {FlyControls} from "three/addons/controls/FlyControls.js"
import {TrackballControls} from "three/addons/controls/TrackballControls.js"
import "./packages/unpkg.com/[email protected]/dist/three-spritetext.min.js"
export default class Visualizer extends HTMLCanvasElement {
static ELEMENT_NAME = "x-visualizer"
static VISUALIZER_READY = "visualizer-ready"
#graph
#camera
#renderer
#scene
#trackball
#fly
#shift = false
#labels
connectedCallback() {
this.#buildScene()
window.addEventListener("resize", this.#onResize.bind(this))
window.addEventListener("keydown", this.#onKeydown.bind(this))
this.dispatchEvent(new CustomEvent(Visualizer.VISUALIZER_READY))
}
#buildScene() {
this.#scene = new Scene()
this.#scene.add(new AmbientLight(0x96DCBE))
this.#camera = new PerspectiveCamera()
this.#camera.aspect = window.innerWidth / window.innerHeight
this.#camera.updateProjectionMatrix()
this.#camera.position.z = 50
this.#renderer = new WebGLRenderer({canvas: this})
this.#renderer.setSize(window.innerWidth, window.innerHeight)
this.#trackball = new TrackballControls(this.#camera, this)
this.#fly = new FlyControls(this.#camera, this)
this.#graph = new ThreeForceGraph()
.nodeRelSize(5)
.nodeResolution(10)
.nodeThreeObjectExtend(true)
.nodeThreeObject((d) => new SpriteText(d.label, 5, "lightgrey"))
.linkThreeObject((d) => d.sprite = new SpriteText(d.label, 5, "lightgrey"))
.linkWidth(2)
.linkThreeObjectExtend(true)
.linkPositionUpdate((obj, {start, end}) => Object.assign(
obj.position,
...["x", "y", "z"].map(dimension => (
{
[dimension]: start[dimension] + (end[dimension] - start[dimension]) / 2
}))))
.linkDirectionalArrowLength(8)
.linkDirectionalArrowRelPos(1)
.linkDirectionalParticles(2)
.linkDirectionalParticleSpeed(0.001)
this.#scene.add(this.#graph)
this.#camera.lookAt(this.#graph.position)
this.#animateGraph.call(this)
}
set value(d) {
this.#graph.graphData(d)
}
#animateGraph() {
requestAnimationFrame(this.#animateGraph.bind(this))
this.#graph.tickFrame()
if (this.#shift) {
this.#fly.update(1)
} else {
this.#trackball.update()
}
this.#graph.graphData().links.forEach(this.#rotateLinkSprites, this)
this.#renderer.render(this.#scene, this.#camera)
}
#rotateLinkSprites(link) {
if ("id" in link.source) {
const source = link.source
const target = link.target
const start = new Vector3(source.x, source.y, source.z).project(this.#camera)
const end = new Vector3(target.x, target.y, target.z).project(this.#camera)
const delta = end.sub(start)
link.sprite.material.rotation = Math.atan2(delta.y, delta.x * this.#camera.aspect)
}
}
#onResize() {
this.#camera.aspect = window.innerWidth / window.innerHeight
this.#camera.updateProjectionMatrix()
this.#renderer.setSize(window.innerWidth, window.innerHeight)
}
// TODO: Sync cameras on switch
#onKeydown(e) {
if (e.shiftKey) {
this.#shift = !this.#shift
}
if (e.key === ",") {
this.#graph.d3Force("link").distance(this.#graph.d3Force("link").distance()() - 5)
this.#graph.d3ReheatSimulation()
}
if (e.key === ".") {
this.#graph.d3Force("link").distance(this.#graph.d3Force("link").distance()() + 5)
this.#graph.d3ReheatSimulation()
}
if (e.key === "l") {
this.#labels = !this.#labels
}
if (this.#labels) {
this.#graph.linkThreeObject(d => d.sprite = new SpriteText)
this.#graph.nodeThreeObject(() => null)
} else {
this.#graph.linkThreeObject(d => d.sprite = new SpriteText(d.label, 5, "lightgrey"))
this.#graph.nodeThreeObject(d => new SpriteText(d.label, 5, "lightgrey"))
}
}
}