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