Spaces:
Runtime error
Runtime error
no
Browse files- frontend/src/lib/App.svelte +9 -95
- frontend/src/lib/Canvas.svelte +41 -10
- frontend/src/lib/Cursor.svelte +10 -11
- frontend/src/lib/Frame.svelte +6 -10
frontend/src/lib/App.svelte
CHANGED
@@ -4,7 +4,6 @@
|
|
4 |
import Canvas from '$lib/Canvas.svelte';
|
5 |
import Menu from '$lib/Menu.svelte';
|
6 |
import type { Room } from '@liveblocks/client';
|
7 |
-
import { onDestroy } from 'svelte';
|
8 |
import { COLORS, EMOJIS } from '$lib/constants';
|
9 |
import { currZoomTransform, myPresence, others } from '$lib/store';
|
10 |
|
@@ -14,61 +13,6 @@
|
|
14 |
*/
|
15 |
|
16 |
export let room: Room;
|
17 |
-
|
18 |
-
// // Get initial values for presence and others
|
19 |
-
// let myPresence = room.getPresence();
|
20 |
-
// let others = room.getOthers();
|
21 |
-
|
22 |
-
// // Subscribe to further changes
|
23 |
-
// const unsubscribeMyPresence = room.subscribe('my-presence', (presence) => {
|
24 |
-
// myPresence = presence;
|
25 |
-
// });
|
26 |
-
|
27 |
-
// const unsubscribeOthers = room.subscribe('others', (otherUsers) => {
|
28 |
-
// others = otherUsers;
|
29 |
-
// });
|
30 |
-
|
31 |
-
// // Unsubscribe when unmounting
|
32 |
-
// onDestroy(() => {
|
33 |
-
// unsubscribeMyPresence();
|
34 |
-
// unsubscribeOthers();
|
35 |
-
// });
|
36 |
-
|
37 |
-
// $: {
|
38 |
-
// console.log('myPresence', $myPresence);
|
39 |
-
// console.log('others', $others);
|
40 |
-
// }
|
41 |
-
$: {
|
42 |
-
console.log('Sefl', room.getSelf());
|
43 |
-
}
|
44 |
-
const r = 8;
|
45 |
-
function round(p, n) {
|
46 |
-
return p % n < n / 2 ? p - (p % n) : p + n - (p % n);
|
47 |
-
}
|
48 |
-
const grid = 8;
|
49 |
-
|
50 |
-
// Update cursor presence to current pointer location
|
51 |
-
function handlePointerMove(event: PointerEvent) {
|
52 |
-
event.preventDefault();
|
53 |
-
const x = Math.round(event.layerX / grid) * grid; //round(Math.max(r, Math.min(512 * 5 - r, event.clientX)), 100);
|
54 |
-
const y = Math.round(event.layerY / grid) * grid; //round(Math.max(r, Math.min(512 * 5 - r, event.clientY)), 100);
|
55 |
-
// const x = round(Math.max(r, Math.min(512 * 5 - r, event.clientX)), grid);
|
56 |
-
// const y = round(Math.max(r, Math.min(512 * 5 - r, event.clientY)), grid);
|
57 |
-
|
58 |
-
$myPresence = {
|
59 |
-
cursor: {
|
60 |
-
x,
|
61 |
-
y
|
62 |
-
}
|
63 |
-
};
|
64 |
-
}
|
65 |
-
|
66 |
-
// When the pointer leaves the page, set cursor presence to null
|
67 |
-
function handlePointerLeave() {
|
68 |
-
$myPresence = {
|
69 |
-
cursor: null
|
70 |
-
};
|
71 |
-
}
|
72 |
</script>
|
73 |
|
74 |
<!-- Show the current user's cursor location -->
|
@@ -77,27 +21,17 @@
|
|
77 |
? `${$myPresence.cursor.x} × ${$myPresence.cursor.y}`
|
78 |
: 'Move your cursor to broadcast its position to other people in the room.'}
|
79 |
</div>
|
80 |
-
<div
|
81 |
-
class="relative z-0 w-screen h-screen cursor-none"
|
82 |
-
on:pointerleave={handlePointerLeave}
|
83 |
-
on:pointermove={handlePointerMove}
|
84 |
-
>
|
85 |
<Canvas />
|
86 |
|
87 |
<main class="z-10 relative">
|
88 |
-
|
89 |
{#if $myPresence?.cursor}
|
90 |
-
|
91 |
-
color={COLORS[0]}
|
92 |
-
x={$myPresence.cursor.x}
|
93 |
-
y={$myPresence.cursor.y}
|
94 |
-
transform={$currZoomTransform}
|
95 |
-
/> -->
|
96 |
<Cursor
|
97 |
emoji={EMOJIS[0]}
|
98 |
color={COLORS[0]}
|
99 |
-
|
100 |
-
|
101 |
/>
|
102 |
{/if}
|
103 |
|
@@ -105,18 +39,17 @@
|
|
105 |
{#if others}
|
106 |
{#each [...$others] as { connectionId, presence } (connectionId)}
|
107 |
{#if presence?.cursor}
|
108 |
-
|
109 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
110 |
-
|
111 |
-
y={presence.cursor.y}
|
112 |
transform={$currZoomTransform}
|
113 |
-
/>
|
114 |
|
115 |
<Cursor
|
116 |
emoji={EMOJIS[1 + (connectionId % (EMOJIS.length - 1))]}
|
117 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
118 |
-
|
119 |
-
|
120 |
/>
|
121 |
{/if}
|
122 |
{/each}
|
@@ -128,23 +61,4 @@
|
|
128 |
</div>
|
129 |
|
130 |
<style lang="postcss" scoped>
|
131 |
-
main {
|
132 |
-
/* @apply fixed top-0 left-0 w-screen h-screen flex flex-col items-center justify-center touch-none bg-white; */
|
133 |
-
/* position: absolute;
|
134 |
-
top: 0;
|
135 |
-
left: 0;
|
136 |
-
width: 100vw;
|
137 |
-
height: 100vh;
|
138 |
-
display: flex;
|
139 |
-
place-content: center;
|
140 |
-
place-items: center;
|
141 |
-
touch-action: none;
|
142 |
-
background-color: white; */
|
143 |
-
}
|
144 |
-
|
145 |
-
.text {
|
146 |
-
max-width: 380px;
|
147 |
-
margin: 0 16px;
|
148 |
-
text-align: center;
|
149 |
-
}
|
150 |
</style>
|
|
|
4 |
import Canvas from '$lib/Canvas.svelte';
|
5 |
import Menu from '$lib/Menu.svelte';
|
6 |
import type { Room } from '@liveblocks/client';
|
|
|
7 |
import { COLORS, EMOJIS } from '$lib/constants';
|
8 |
import { currZoomTransform, myPresence, others } from '$lib/store';
|
9 |
|
|
|
13 |
*/
|
14 |
|
15 |
export let room: Room;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
</script>
|
17 |
|
18 |
<!-- Show the current user's cursor location -->
|
|
|
21 |
? `${$myPresence.cursor.x} × ${$myPresence.cursor.y}`
|
22 |
: 'Move your cursor to broadcast its position to other people in the room.'}
|
23 |
</div>
|
24 |
+
<div class="fixed left-0 z-0 w-screen h-screen cursor-none">
|
|
|
|
|
|
|
|
|
25 |
<Canvas />
|
26 |
|
27 |
<main class="z-10 relative">
|
|
|
28 |
{#if $myPresence?.cursor}
|
29 |
+
<Frame color={COLORS[0]} position={$myPresence.cursor} transform={$currZoomTransform} />
|
|
|
|
|
|
|
|
|
|
|
30 |
<Cursor
|
31 |
emoji={EMOJIS[0]}
|
32 |
color={COLORS[0]}
|
33 |
+
position={$myPresence.cursor}
|
34 |
+
transform={$currZoomTransform}
|
35 |
/>
|
36 |
{/if}
|
37 |
|
|
|
39 |
{#if others}
|
40 |
{#each [...$others] as { connectionId, presence } (connectionId)}
|
41 |
{#if presence?.cursor}
|
42 |
+
<Frame
|
43 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
44 |
+
position={presence.cursor}
|
|
|
45 |
transform={$currZoomTransform}
|
46 |
+
/>
|
47 |
|
48 |
<Cursor
|
49 |
emoji={EMOJIS[1 + (connectionId % (EMOJIS.length - 1))]}
|
50 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
51 |
+
position={presence.cursor}
|
52 |
+
transform={$currZoomTransform}
|
53 |
/>
|
54 |
{/if}
|
55 |
{/each}
|
|
|
61 |
</div>
|
62 |
|
63 |
<style lang="postcss" scoped>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
</style>
|
frontend/src/lib/Canvas.svelte
CHANGED
@@ -1,28 +1,36 @@
|
|
1 |
<script lang="ts">
|
2 |
import { zoom, type ZoomTransform, zoomIdentity } from 'd3-zoom';
|
3 |
import { select } from 'd3-selection';
|
|
|
4 |
import { onMount } from 'svelte';
|
5 |
-
import { currZoomTransform } from '$lib/store';
|
|
|
6 |
const height = 512 * 5;
|
7 |
const width = 512 * 5;
|
8 |
|
9 |
let canvasEl: HTMLCanvasElement;
|
10 |
let containerEl: HTMLDivElement;
|
11 |
let canvasCtx: CanvasRenderingContext2D;
|
|
|
|
|
12 |
|
13 |
const margin = { top: 100, right: 100, bottom: 100, left: 100 };
|
14 |
const extent = [
|
15 |
[-margin.left, -margin.top],
|
16 |
[width + margin.right, height + margin.bottom]
|
17 |
] as [[number, number], [number, number]];
|
|
|
18 |
onMount(() => {
|
|
|
|
|
|
|
19 |
const scale = width / containerEl.clientWidth;
|
20 |
const zoomHandler = zoom()
|
21 |
-
.scaleExtent([1
|
22 |
-
.translateExtent([
|
23 |
-
|
24 |
-
|
25 |
-
])
|
26 |
// .translateExtent(extent)
|
27 |
.clickDistance(2)
|
28 |
.on('zoom', zoomed);
|
@@ -46,11 +54,34 @@
|
|
46 |
const transform = ($currZoomTransform = e.transform);
|
47 |
canvasEl.style.transform = `translate(${transform.x}px, ${transform.y}px) scale(${transform.k})`;
|
48 |
}
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
}
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
</script>
|
56 |
|
|
|
1 |
<script lang="ts">
|
2 |
import { zoom, type ZoomTransform, zoomIdentity } from 'd3-zoom';
|
3 |
import { select } from 'd3-selection';
|
4 |
+
import { scaleLinear } from 'd3-scale';
|
5 |
import { onMount } from 'svelte';
|
6 |
+
import { currZoomTransform, myPresence, others } from '$lib/store';
|
7 |
+
|
8 |
const height = 512 * 5;
|
9 |
const width = 512 * 5;
|
10 |
|
11 |
let canvasEl: HTMLCanvasElement;
|
12 |
let containerEl: HTMLDivElement;
|
13 |
let canvasCtx: CanvasRenderingContext2D;
|
14 |
+
let xScale: (x: number) => number;
|
15 |
+
let yScale: (y: number) => number;
|
16 |
|
17 |
const margin = { top: 100, right: 100, bottom: 100, left: 100 };
|
18 |
const extent = [
|
19 |
[-margin.left, -margin.top],
|
20 |
[width + margin.right, height + margin.bottom]
|
21 |
] as [[number, number], [number, number]];
|
22 |
+
|
23 |
onMount(() => {
|
24 |
+
xScale = scaleLinear().domain([0, width]).range([0, width]);
|
25 |
+
yScale = scaleLinear().domain([0, height]).range([0, height]);
|
26 |
+
|
27 |
const scale = width / containerEl.clientWidth;
|
28 |
const zoomHandler = zoom()
|
29 |
+
.scaleExtent([1/scale,1])
|
30 |
+
// .translateExtent([
|
31 |
+
// [0, 0],
|
32 |
+
// [width, height]
|
33 |
+
// ])
|
34 |
// .translateExtent(extent)
|
35 |
.clickDistance(2)
|
36 |
.on('zoom', zoomed);
|
|
|
54 |
const transform = ($currZoomTransform = e.transform);
|
55 |
canvasEl.style.transform = `translate(${transform.x}px, ${transform.y}px) scale(${transform.k})`;
|
56 |
}
|
57 |
+
|
58 |
+
const r = 8;
|
59 |
+
function round(p, n) {
|
60 |
+
return p % n < n / 2 ? p - (p % n) : p + n - (p % n);
|
61 |
+
}
|
62 |
+
const grid = 8;
|
63 |
+
|
64 |
+
// Update cursor presence to current pointer location
|
65 |
+
function handlePointerMove(event: PointerEvent) {
|
66 |
+
event.preventDefault();
|
67 |
+
const x = Math.round(event.layerX / grid) * grid; //round(Math.max(r, Math.min(512 * 5 - r, event.clientX)), 100);
|
68 |
+
const y = Math.round(event.layerY / grid) * grid; //round(Math.max(r, Math.min(512 * 5 - r, event.clientY)), 100);
|
69 |
+
// const x = round(Math.max(r, Math.min(512 * 5 - r, event.clientX)), grid);
|
70 |
+
// const y = round(Math.max(r, Math.min(512 * 5 - r, event.clientY)), grid);
|
71 |
+
|
72 |
+
$myPresence = {
|
73 |
+
cursor: {
|
74 |
+
x,
|
75 |
+
y
|
76 |
+
}
|
77 |
+
};
|
78 |
}
|
79 |
+
|
80 |
+
// When the pointer leaves the page, set cursor presence to null
|
81 |
+
function handlePointerLeave() {
|
82 |
+
$myPresence = {
|
83 |
+
cursor: null
|
84 |
+
};
|
85 |
}
|
86 |
</script>
|
87 |
|
frontend/src/lib/Cursor.svelte
CHANGED
@@ -1,25 +1,24 @@
|
|
1 |
<script lang="ts">
|
2 |
import { spring } from 'svelte/motion';
|
3 |
|
|
|
|
|
|
|
4 |
export let color = '';
|
5 |
export let emoji = '';
|
6 |
-
export let
|
7 |
-
export let y = 0;
|
8 |
|
9 |
// Spring animation for cursor
|
10 |
-
const coords = spring(
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
damping: 0.35
|
15 |
-
}
|
16 |
-
);
|
17 |
// Update spring when x and y change
|
18 |
-
$: coords.set(
|
19 |
</script>
|
20 |
|
21 |
<div
|
22 |
-
class="absolute top-0 left-0 grid grid-cols-3"
|
23 |
style={`transform: translateX(${$coords.x}px) translateY(${$coords.y}px);`}
|
24 |
>
|
25 |
<svg
|
|
|
1 |
<script lang="ts">
|
2 |
import { spring } from 'svelte/motion';
|
3 |
|
4 |
+
import type { ZoomTransform } from 'd3-zoom';
|
5 |
+
|
6 |
+
export let transform: ZoomTransform;
|
7 |
export let color = '';
|
8 |
export let emoji = '';
|
9 |
+
export let position = { x: 0, y: 0 };
|
|
|
10 |
|
11 |
// Spring animation for cursor
|
12 |
+
const coords = spring(position, {
|
13 |
+
stiffness: 0.07,
|
14 |
+
damping: 0.35
|
15 |
+
});
|
|
|
|
|
|
|
16 |
// Update spring when x and y change
|
17 |
+
$: coords.set(position);
|
18 |
</script>
|
19 |
|
20 |
<div
|
21 |
+
class="absolute top-0 left-0 grid grid-cols-3 touch-none pointer-events-none"
|
22 |
style={`transform: translateX(${$coords.x}px) translateY(${$coords.y}px);`}
|
23 |
>
|
24 |
<svg
|
frontend/src/lib/Frame.svelte
CHANGED
@@ -6,19 +6,15 @@
|
|
6 |
|
7 |
export let transform: ZoomTransform;
|
8 |
export let color = '';
|
9 |
-
export let
|
10 |
-
export let y = 0;
|
11 |
|
12 |
// Spring animation for cursor
|
13 |
-
const coords = spring(
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
damping: 0.35
|
18 |
-
}
|
19 |
-
);
|
20 |
// Update spring when x and y change
|
21 |
-
$: coords.set(
|
22 |
</script>
|
23 |
|
24 |
<div
|
|
|
6 |
|
7 |
export let transform: ZoomTransform;
|
8 |
export let color = '';
|
9 |
+
export let position = { x: 0, y: 0 };
|
|
|
10 |
|
11 |
// Spring animation for cursor
|
12 |
+
const coords = spring(position, {
|
13 |
+
stiffness: 0.07,
|
14 |
+
damping: 0.35
|
15 |
+
});
|
|
|
|
|
|
|
16 |
// Update spring when x and y change
|
17 |
+
$: coords.set(position);
|
18 |
</script>
|
19 |
|
20 |
<div
|