]> git.r.bdr.sh - rbdr/canvas/blame - src/routes/index.svelte
Move things to components
[rbdr/canvas] / src / routes / index.svelte
CommitLineData
c336b229
RBR
1<script>
2
3 import { browser } from '$app/env';
3717f7fb
RBR
4 import Background from '$lib/components/background.svelte';
5 import Palette from '$lib/components/palette.svelte';
6 import Hud from '$lib/components/hud.svelte';
7
8 const kCoordinateLength = 6;
9 const kMaxSize = Math.pow(16, kCoordinateLength);
c336b229 10
c336b229
RBR
11 $: x = 0;
12 $: y = 0;
f106543f 13 $: shouldShowPalette = false;
c336b229 14
c336b229
RBR
15 let dragging = false;
16
3717f7fb
RBR
17 const modulo = function modulo (number, base) {
18 return ((number % base) + base) % base
19 };
20
c336b229
RBR
21 const moveCanvas = function moveCanvas(event) {
22
23 if (dragging) {
3717f7fb
RBR
24 let deltaX = event.x - dragging.x;
25 let deltaY = event.y - dragging.y;
26 x = modulo(x - deltaX, kMaxSize);
27 y = modulo(y - deltaY, kMaxSize);
c336b229
RBR
28 dragging.x = event.x;
29 dragging.y = event.y;
30 }
31 }
32
33 const startDragging = function startDragging(event) {
34 dragging = {
35 x: event.x,
36 y: event.y
37 };
38 }
39
40 const stopDragging = function startDragging() {
41 dragging = null;
42 }
43
f106543f
RBR
44 const showPalette = function showPalette(event) {
45 event.preventDefault();
46 dragging = null;
47 document.documentElement.style.setProperty('--palette-x', event.clientX + 'px');
48 document.documentElement.style.setProperty('--palette-y', event.clientY + 'px');
49 shouldShowPalette = true;
50 }
51
52 const hidePalette = function hidePalette() {
53 event.preventDefault();
54 shouldShowPalette = false;
55 }
c336b229
RBR
56</script>
57
f106543f
RBR
58<div
59 class="canvas"
3717f7fb 60 on:click={hidePalette}
f106543f
RBR
61 on:contextmenu={showPalette}
62 on:mousedown={startDragging}
63 on:mouseup={stopDragging}
64 on:mousemove={moveCanvas}>
3717f7fb
RBR
65>
66 <Background x={x} y={y}/>
67 <Hud x={x} y={y} />
c336b229 68
3717f7fb
RBR
69 {#if shouldShowPalette }
70 <Palette/>
71 {/if}
c336b229
RBR
72</div>
73
74<style>
75 .canvas {
76 user-select: none;
c336b229
RBR
77 padding: 0;
78 margin: 0;
79 width: 100vw;
80 height: 100vh;
81 }
c336b229 82</style>