-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvisualizer_live.js
370 lines (309 loc) · 12.8 KB
/
visualizer_live.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Enables point filtering
//PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
// Create a PixiJS application
const app = new PIXI.Application({
resizeTo: window,
eventMode: "static",
eventFeatures: {
wheel: true,
mouse: true,
}
});
let socket = null;
let lastFrameTime = Date.now();
let curStats = {envs: 0, viewers: 0};
let backgroundSharp = null;
let backgroundSmooth = null;
// animate each batch of updates for 12 seconds
const animationDuration = 12000;
const container = new PIXI.Container();
// scale and center container initially
const renderWidth = window.innerWidth; // or the width of your specific rendering area
const renderHeight = window.innerHeight; // or the height of your specific rendering area
const desiredCenterX = renderWidth / 2;
const desiredCenterY = renderHeight / 2;
container.x = desiredCenterX;
container.y = desiredCenterY;
container.pivot.x = container.width / 2;
container.pivot.y = container.height / 2;
container.scale.set(0.1, 0.1);
app.stage.addChild(container);
// add the view that Pixi created for you to the DOM
document.body.appendChild(app.view);
const zoomSpeed = 0.0015;
function smoothstep(min, max, value) {
const x = Math.max(0, Math.min(1, (value-min)/(max-min)));
return x*x*(3 - 2*x);
}
let userFilter = new RegExp("");
let activeSprites = [];
function setUserFilter(value) {
userFilter = new RegExp(value);
activeSprites.forEach(obj => {
container.removeChild(obj.subContainer); // Remove sprite from the scene
obj.subContainer.destroy({ children: true }); // Optional: frees up memory used by the sprite
});
activeSprites = []
}
app.view.addEventListener('wheel', (e) => {
e.preventDefault();
const scaleFactor = 1.0 - (e.deltaY * zoomSpeed);
// Get the mouse position relative to the canvas
const rect = app.view.getBoundingClientRect();
const x = (e.clientX - rect.left) * (app.renderer.width / rect.width);
const y = (e.clientY - rect.top) * (app.renderer.height / rect.height);
// Calculate the point to scale around
const point = new PIXI.Point(x, y);
const localPoint = container.toLocal(point);
// Scale the container
container.scale.x *= scaleFactor;
container.scale.y *= scaleFactor;
if (backgroundSmooth && backgroundSharp) {
const val = container.scale.x;
const start = 2;
const end = 4.5;
const smooth = smoothstep(start, end, val);
backgroundSharp.alpha = Math.pow(smooth, 0.3);
backgroundSmooth.alpha = Math.pow(1.0 - smooth, 0.3);
}
// Calculate the new position of the point
const newPoint = container.toGlobal(localPoint);
container.x -= (newPoint.x - point.x);
container.y -= (newPoint.y - point.y);
});
let dragging = false;
let dragStart = { x: 0, y: 0 };
let dragOffset = { x: 0, y: 0 };
container.on('mousedown', (event) => {
dragging = true;
// Get the position of the mouse relative to the container's position
dragStart = event.data.getLocalPosition(container.parent);
// Calculate the offset
dragOffset.x = container.x - dragStart.x;
dragOffset.y = container.y - dragStart.y;
})
.on('mouseup', () => {
dragging = false;
})
.on('mouseupoutside', () => {
dragging = false;
})
.on('mousemove', (event) => {
if (dragging) {
// Get the new position of the mouse relative to the container's parent
const newPosition = event.data.getLocalPosition(container.parent);
// Apply the offset to get the new container position
container.x = newPosition.x + dragOffset.x;
container.y = newPosition.y + dragOffset.y;
}
});
let lastTouchDistance = null;
function getTouchDistance(touch1, touch2) {
const dx = touch1.pageX - touch2.pageX;
const dy = touch1.pageY - touch2.pageY;
return Math.sqrt(dx * dx + dy * dy);
}
function getMidpoint(touch1, touch2) {
return {
x: (touch1.pageX + touch2.pageX) / 2,
y: (touch1.pageY + touch2.pageY) / 2,
};
}
app.view.addEventListener('touchmove', (e) => {
e.preventDefault();
if (e.touches.length == 2) {
// distance between the two touches
const touchDistance = getTouchDistance(e.touches[0], e.touches[1]);
// midpoint of the two touches in screen coordinates
const screenMidpoint = getMidpoint(e.touches[0], e.touches[1]);
if (lastTouchDistance !== null) {
const scaleFactor = touchDistance / lastTouchDistance;
const newScale = container.scale.x * scaleFactor;
// Convert to the container's local coordinate space
const rect = app.view.getBoundingClientRect();
const localMidpoint = container.toLocal(new PIXI.Point(screenMidpoint.x - rect.left, screenMidpoint.y - rect.top));
container.scale.x = newScale;
container.scale.y = newScale;
const newLocalMidpoint = container.toGlobal(localMidpoint);
container.x += screenMidpoint.x - rect.left - newLocalMidpoint.x;
container.y += screenMidpoint.y - rect.top - newLocalMidpoint.y;
}
lastTouchDistance = touchDistance;
}
}, { passive: false });
app.view.addEventListener('touchend', () => {
lastTouchDistance = null;
});
// panning
app.view.addEventListener('touchstart', (e) => {
if (e.touches.length == 1) { // one finger
dragging = true;
const touch = e.touches[0];
dragStart.x = touch.pageX;
dragStart.y = touch.pageY;
dragOffset.x = container.x - dragStart.x;
dragOffset.y = container.y - dragStart.y;
}
}, { passive: false });
app.view.addEventListener('touchmove', (e) => {
if (dragging && e.touches.length == 1) {
const touch = e.touches[0];
const newPosition = { x: touch.pageX, y: touch.pageY };
container.x = newPosition.x + dragOffset.x;
container.y = newPosition.y + dragOffset.y;
}
}, { passive: false });
app.view.addEventListener('touchend', () => {
dragging = false;
});
let coordConversionFunc = (coords) => [0,0];
fetch('assets/map_data.json')
.then(response => response.json())
.then(data => {
MAP_DATA = data.regions.reduce((acc, e) => {
acc[e.id] = e;
return acc;
}, {});
coordConversionFunc = (coords) => {
if (MAP_DATA[coords[2]] !== undefined) {
const mapX = MAP_DATA[coords[2]].coordinates[0];
const mapY = MAP_DATA[coords[2]].coordinates[1];//-vec2(217.5,221.5)
return [coords[0] + mapX - 217.5, coords[1] + mapY - 221.5];
} else {
console.warn(`No map coordiate location for id: ${coords[2]}`);
return [0,0];
}
};
})
.catch(error => console.error('Error loading map data:', error));
function getSpriteByCoords(x, y, baseTex) {
const sx = 9 + 17 * x;
const sy = 34 + 17 * y;
const width = 16;
const height = 16;
return new PIXI.Texture(baseTex, new PIXI.Rectangle(sx, sy, width, height));
}
// "kanto_big_done1.png",
// "characters_transparent.png",
// "characters_front.png"
PIXI.Assets.load([
"assets/kanto_big_done1.png",
"assets/characters_transparent.png",
"assets/characters_front.png"
]).then(() => {
let baseTextureSmooth = new PIXI.BaseTexture("assets/kanto_big_done1.png", {
mipmap: PIXI.MIPMAP_MODES.ON, scaleMode: PIXI.SCALE_MODES.LINEAR,
});
let textureSmooth = new PIXI.Texture(baseTextureSmooth);
backgroundSmooth = new PIXI.Sprite(textureSmooth);
backgroundSmooth.anchor.set(0.5);
let baseTextureSharp = new PIXI.BaseTexture("assets/kanto_big_done1.png", {
scaleMode: PIXI.SCALE_MODES.NEAREST,
});
let textureSharp = new PIXI.Texture(baseTextureSharp);
backgroundSharp = new PIXI.Sprite(textureSharp);
backgroundSharp.anchor.set(0.5);
backgroundSharp.alpha = 0.0;
container.addChild(backgroundSmooth);
container.addChild(backgroundSharp);
// Function to initialize WebSocket connection
function initializeWebSocket(url) {
const ws = new WebSocket(url);
ws.onmessage = function(event) {
const data = JSON.parse(event.data); // Assuming the data is JSON-encoded
if ("stats" in data) {
curStats = data["stats"];
document.getElementById('envsCount').innerText = `${curStats.envs} Environments Streaming`;
document.getElementById('viewersCount').innerText = `${curStats.viewers} Viewers Connected`;
} else {
const path = data["coords"];
const meta = data["metadata"];
console.log(meta);
if (Date.now() - lastFrameTime < 2 * animationDuration) {
startAnimationForPath(path, meta);
}
}
};
return ws;
}
const refreshWS = () => {
console.log("Refreshing WebSocket connection.");
if (socket !== null) {
socket.close(); // Close the current connection
}
socket = initializeWebSocket("wss://transdimensional.xyz/receive");
};
refreshWS();
// Refresh WebSocket connection every 2 minutes (120000 milliseconds)
setInterval(refreshWS, 120000);
let baseTextureChar = new PIXI.BaseTexture("assets/characters_transparent.png", {
scaleMode: PIXI.SCALE_MODES.NEAREST,
});
const charOffset = 1; // 1 index here gets sprite direction index
let textureChars = [];
for (let i = 0; i < 50; i++) {
textureChars.push(getSpriteByCoords(charOffset, i, baseTextureChar))
}
function startAnimationForPath(path, meta) {
// Check if meta is defined and has a 'user' key
if (meta && meta.user !== undefined && typeof(meta.user) === "string") {
// Create a text label
const envID = meta.env_id !== undefined ? `-${meta.env_id}` : "";
const extraInfo = meta.extra !== undefined ? ` ${meta.extra}` : "";
const color = (meta.color && CSS.supports('color', meta.color)) ? meta.color : "0x000000";
const labelText = meta.user + envID + extraInfo;
if (userFilter.exec(labelText) !== null) {
let spriteIdx = 0;
if (meta.sprite_id !== undefined) {
let parsed = parseInt(meta.sprite_id, 10);
if (!isNaN(parsed) && parsed > 0 && parsed < 50) {
spriteIdx = parsed;
}
}
const sprite = new PIXI.Sprite(textureChars[spriteIdx]);
//sprite.x = charOffset * 40;
sprite.anchor.set(0.5);
//sprite.scale.set(0.5); // Adjust scale as needed
const subContainer = new PIXI.Container();
subContainer.addChild(sprite);
const label = new PIXI.Text(
labelText,
{
fontFamily: 'Arial',
fontSize: 14,
fill: color,
align: 'center',
});
label.x = sprite.x + sprite.width * 0.5; // Position the label next to the sprite
label.y -= sprite.height; // Adjust the label position as needed
subContainer.addChild(label);
container.addChild(subContainer);
activeSprites.push({ subContainer, path, startTime: null });
}
}
}
function animate(time) {
activeSprites.forEach(obj => {
if (!obj.startTime) obj.startTime = time;
const timeDelta = time - obj.startTime;
const progress = Math.min(timeDelta / animationDuration, 1);
// Calculate the current position
const currentIndex = Math.floor(progress * (obj.path.length - 1));
const nextIndex = Math.min(currentIndex + 1, obj.path.length - 1);
const pointProgress = (progress * (obj.path.length - 1)) - currentIndex;
const currentPoint = coordConversionFunc(obj.path[currentIndex]);
const nextPoint = coordConversionFunc(obj.path[nextIndex]);
obj.subContainer.x = 16*(currentPoint[0] + (nextPoint[0] - currentPoint[0]) * pointProgress);
obj.subContainer.y = 16*(currentPoint[1] + (nextPoint[1] - currentPoint[1]) * pointProgress);
if (progress >= 1) {
container.removeChild(obj.subContainer); // Remove sprite from the scene
obj.subContainer.destroy({ children: true }); // Optional: frees up memory used by the sprite
}
});
// Remove sprites that have completed their animation
activeSprites = activeSprites.filter(obj => (time - obj.startTime) < animationDuration);
lastFrameTime = Date.now();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
});