]> git.r.bdr.sh - rbdr/canvas/blame - src/routes/+page.svelte
Update project, improve sensor/render logic
[rbdr/canvas] / src / routes / +page.svelte
CommitLineData
c336b229 1<script>
2df937df 2 import Sensor from '$lib/components/sensor.svelte';
3717f7fb
RBR
3 import Background from '$lib/components/background.svelte';
4 import Palette from '$lib/components/palette.svelte';
5 import Hud from '$lib/components/hud.svelte';
e4e4b520 6 import Minimap from '$lib/components/minimap.svelte';
d7cea969 7 import Widget from '$lib/components/widget.svelte';
2df937df 8 import { modulo } from '$lib/math';
c30e6881 9 import { widgets } from '$lib/stores/widgets';
d7cea969 10 import { canvas } from '$lib/stores/canvas';
3717f7fb 11
2df937df 12 import { coordinateLength, maxSize } from '$lib/config';
c336b229 13
f106543f 14 $: shouldShowPalette = false;
c336b229 15
c336b229
RBR
16 let dragging = false;
17
18 const moveCanvas = function moveCanvas(event) {
19
20 if (dragging) {
3717f7fb
RBR
21 let deltaX = event.x - dragging.x;
22 let deltaY = event.y - dragging.y;
d7cea969
RBR
23 let x = modulo($canvas.x - deltaX, maxSize);
24 let y = modulo($canvas.y - deltaY, maxSize);
c336b229
RBR
25 dragging.x = event.x;
26 dragging.y = event.y;
d7cea969 27 canvas.set({ x, y })
c336b229
RBR
28 }
29 }
30
31 const startDragging = function startDragging(event) {
32 dragging = {
33 x: event.x,
34 y: event.y
35 };
36 }
37
38 const stopDragging = function startDragging() {
39 dragging = null;
40 }
41
f106543f
RBR
42 const showPalette = function showPalette(event) {
43 event.preventDefault();
44 dragging = null;
45 document.documentElement.style.setProperty('--palette-x', event.clientX + 'px');
46 document.documentElement.style.setProperty('--palette-y', event.clientY + 'px');
47 shouldShowPalette = true;
48 }
49
50 const hidePalette = function hidePalette() {
51 event.preventDefault();
52 shouldShowPalette = false;
53 }
c336b229
RBR
54</script>
55
f106543f
RBR
56<div
57 class="canvas"
3717f7fb 58 on:click={hidePalette}
f106543f
RBR
59 on:contextmenu={showPalette}
60 on:mousedown={startDragging}
61 on:mouseup={stopDragging}
c30e6881 62 on:mousemove={moveCanvas}
3717f7fb 63>
d7cea969
RBR
64 <Sensor />
65 <Background />
66 <Hud />
e4e4b520 67 <Minimap />
c336b229 68
3717f7fb
RBR
69 {#if shouldShowPalette }
70 <Palette/>
71 {/if}
c30e6881 72
d7cea969
RBR
73 {#if $widgets}
74 {#each $widgets as widget}
75 <Widget widget={widget}/>
76 {/each}
77 {/if}
c336b229
RBR
78</div>
79
80<style>
81 .canvas {
82 user-select: none;
c336b229
RBR
83 padding: 0;
84 margin: 0;
85 width: 100vw;
86 height: 100vh;
87 }
c336b229 88</style>