forked from jacomyal/sigma.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.image.ts
More file actions
336 lines (280 loc) · 10.4 KB
/
Copy pathnode.image.ts
File metadata and controls
336 lines (280 loc) · 10.4 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Sigma.js WebGL Renderer Node Program
* =====================================
*
* Program rendering nodes using GL_POINTS, but that draws an image on top of
* the classic colored disc.
* @module
*/
import { Coordinates, Dimensions, NodeDisplayData } from "../../../types";
import { floatColor } from "../../../utils";
import vertexShaderSource from "../shaders/node.image.vert.glsl";
import fragmentShaderSource from "../shaders/node.image.frag.glsl";
import { AbstractNodeProgram } from "./common/node";
import { RenderParams } from "./common/program";
import Sigma from "../../../sigma";
const POINTS = 1,
ATTRIBUTES = 8,
// maximum size of single texture in atlas
MAX_TEXTURE_SIZE = 192,
// maximum width of atlas texture (limited by browser)
// low setting of 3072 works on phones & tablets
MAX_CANVAS_WIDTH = 3072;
type ImageLoading = { status: "loading" };
type ImageError = { status: "error" };
type ImagePending = { status: "pending"; image: HTMLImageElement };
type ImageReady = { status: "ready" } & Coordinates & Dimensions;
type ImageType = ImageLoading | ImageError | ImagePending | ImageReady;
// This class only exists for the return typing of `getNodeImageProgram`:
class AbstractNodeImageProgram extends AbstractNodeProgram {
/* eslint-disable @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars */
constructor(gl: WebGLRenderingContext, renderer: Sigma) {
super(gl, vertexShaderSource, fragmentShaderSource, POINTS, ATTRIBUTES);
}
bind(): void {}
process(data: NodeDisplayData & { image?: string }, hidden: boolean, offset: number): void {}
render(params: RenderParams): void {}
rebindTexture() {}
/* eslint-enable @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars */
}
/**
* To share the texture between the program instances of the graph and the
* hovered nodes (to prevent some flickering, mostly), this program must be
* "built" for each sigma instance:
*/
export default function getNodeImageProgram(): typeof AbstractNodeImageProgram {
/**
* These attributes are shared between all instances of this exact class,
* returned by this call to getNodeProgramImage:
*/
const rebindTextureFns: (() => void)[] = [];
const images: Record<string, ImageType> = {};
let textureImage: ImageData;
let hasReceivedImages = false;
let pendingImagesFrameID: number | undefined = undefined;
// next write position in texture
let writePositionX = 0;
let writePositionY = 0;
// height of current row
let writeRowHeight = 0;
interface PendingImage {
image: HTMLImageElement;
id: string;
size: number;
}
/**
* Helper to load an image:
*/
function loadImage(imageSource: string): void {
if (images[imageSource]) return;
const image = new Image();
image.addEventListener("load", () => {
images[imageSource] = {
status: "pending",
image,
};
if (typeof pendingImagesFrameID !== "number") {
pendingImagesFrameID = requestAnimationFrame(() => finalizePendingImages());
}
});
image.addEventListener("error", () => {
images[imageSource] = { status: "error" };
});
images[imageSource] = { status: "loading" };
// Load image:
image.setAttribute("crossOrigin", "");
image.src = imageSource;
}
/**
* Helper that takes all pending images and adds them into the texture:
*/
function finalizePendingImages(): void {
pendingImagesFrameID = undefined;
const pendingImages: PendingImage[] = [];
// List all pending images:
for (const id in images) {
const state = images[id];
if (state.status === "pending") {
pendingImages.push({
id,
image: state.image,
size: Math.min(state.image.width, state.image.height) || 1,
});
}
}
// Add images to texture:
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d", {willReadFrequently: true}) as CanvasRenderingContext2D;
// limit canvas size to avoid browser and platform limits
let totalWidth = hasReceivedImages ? textureImage.width : 0;
let totalHeight = hasReceivedImages ? textureImage.height : 0;
// initialize image drawing offsets with current write position
let xOffset = writePositionX;
let yOffset = writePositionY;
/**
* Draws a (full or partial) row of images into the atlas texture
* @param pendingImages
*/
const drawRow = (pendingImages: PendingImage[]) => {
// update canvas size before drawing
if (canvas.width !== totalWidth || canvas.height !== totalHeight) {
canvas.width = Math.min(MAX_CANVAS_WIDTH, totalWidth);
canvas.height = totalHeight;
// draw previous texture into resized canvas
if (hasReceivedImages) {
ctx.putImageData(textureImage, 0, 0);
}
}
pendingImages.forEach(({ id, image, size }) => {
const imageSizeInTexture = Math.min(MAX_TEXTURE_SIZE, size);
// Crop image, to only keep the biggest square, centered:
let dx = 0,
dy = 0;
if ((image.width || 0) > (image.height || 0)) {
dx = (image.width - image.height) / 2;
} else {
dy = (image.height - image.width) / 2;
}
ctx.drawImage(image, dx, dy, size, size, xOffset, yOffset, imageSizeInTexture, imageSizeInTexture);
// Update image state:
images[id] = {
status: "ready",
x: xOffset,
y: yOffset,
width: imageSizeInTexture,
height: imageSizeInTexture,
};
xOffset += imageSizeInTexture;
});
hasReceivedImages = true;
textureImage = ctx.getImageData(0, 0, canvas.width, canvas.height);
};
let rowImages: PendingImage[] = [];
pendingImages.forEach((image) => {
const { size } = image;
const imageSizeInTexture = Math.min(size, MAX_TEXTURE_SIZE);
if (writePositionX + imageSizeInTexture > MAX_CANVAS_WIDTH) {
// existing row is full: flush row and continue on next line
if (rowImages.length > 0) {
totalWidth = Math.max(writePositionX, totalWidth);
totalHeight = Math.max(writePositionY + writeRowHeight, totalHeight);
drawRow(rowImages);
rowImages = [];
writeRowHeight = 0;
}
writePositionX = 0;
writePositionY = totalHeight;
xOffset = 0;
yOffset = totalHeight;
}
// add image to row
rowImages.push(image);
// advance write position and update maximum row height
writePositionX += imageSizeInTexture;
writeRowHeight = Math.max(writeRowHeight, imageSizeInTexture);
});
// flush pending images in row - keep write position (and drawing cursor)
totalWidth = Math.max(writePositionX, totalWidth);
totalHeight = Math.max(writePositionY + writeRowHeight, totalHeight);
drawRow(rowImages);
rowImages = [];
rebindTextureFns.forEach((fn) => fn());
}
return class NodeImageProgram extends AbstractNodeProgram {
texture: WebGLTexture;
textureLocation: GLint;
atlasLocation: WebGLUniformLocation;
latestRenderParams?: RenderParams;
constructor(gl: WebGLRenderingContext, renderer: Sigma) {
super(gl, vertexShaderSource, fragmentShaderSource, POINTS, ATTRIBUTES);
rebindTextureFns.push(() => {
if (this && this.rebindTexture) this.rebindTexture();
if (renderer && renderer.refresh) renderer.refresh();
});
textureImage = new ImageData(1, 1);
// Attribute Location
this.textureLocation = gl.getAttribLocation(this.program, "a_texture");
// Uniform Location
const atlasLocation = gl.getUniformLocation(this.program, "u_atlas");
if (atlasLocation === null) throw new Error("NodeProgramImage: error while getting atlasLocation");
this.atlasLocation = atlasLocation;
// Initialize WebGL texture:
this.texture = gl.createTexture() as WebGLTexture;
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 0]));
this.bind();
}
bind(): void {
super.bind();
const gl = this.gl;
gl.enableVertexAttribArray(this.textureLocation);
gl.vertexAttribPointer(
this.textureLocation,
4,
gl.FLOAT,
false,
this.attributes * Float32Array.BYTES_PER_ELEMENT,
16,
);
}
process(data: NodeDisplayData & { image?: string }, hidden: boolean, offset: number): void {
const array = this.array;
let i = offset * POINTS * ATTRIBUTES;
const imageSource = data.image;
const imageState = imageSource && images[imageSource];
if (typeof imageSource === "string" && !imageState) loadImage(imageSource);
if (hidden) {
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
// Texture:
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
return;
}
array[i++] = data.x;
array[i++] = data.y;
array[i++] = data.size;
array[i++] = floatColor(data.color);
// Reference texture:
if (imageState && imageState.status === "ready") {
const { width, height } = textureImage;
array[i++] = imageState.x / width;
array[i++] = imageState.y / height;
array[i++] = imageState.width / width;
array[i++] = imageState.height / height;
} else {
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
}
}
render(params: RenderParams): void {
if (this.hasNothingToRender()) return;
this.latestRenderParams = params;
const gl = this.gl;
const program = this.program;
gl.useProgram(program);
gl.uniform1f(this.ratioLocation, 1 / Math.sqrt(params.ratio));
gl.uniform1f(this.scaleLocation, params.scalingRatio);
gl.uniformMatrix3fv(this.matrixLocation, false, params.matrix);
gl.uniform1i(this.atlasLocation, 0);
gl.drawArrays(gl.POINTS, 0, this.array.length / ATTRIBUTES);
}
rebindTexture() {
const gl = this.gl;
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureImage);
gl.generateMipmap(gl.TEXTURE_2D);
if (this.latestRenderParams) {
this.bind();
this.bufferData();
this.render(this.latestRenderParams);
}
}
};
}