X-Git-Url: https://git.r.bdr.sh/rbdr/canvas/blobdiff_plain/c336b229424eeb95b29a2d450b298c5d2f44fc01..2df937df1372359babd123d1361e702a1a4b8168:/src/routes/index.svelte?ds=inline

diff --git a/src/routes/index.svelte b/src/routes/index.svelte
index f2544cf..2943d23 100644
--- a/src/routes/index.svelte
+++ b/src/routes/index.svelte
@@ -1,30 +1,25 @@
 <script>
+  import Sensor from '$lib/components/sensor.svelte';
+  import Background from '$lib/components/background.svelte';
+  import Palette from '$lib/components/palette.svelte';
+  import Hud from '$lib/components/hud.svelte';
+  import { modulo } from '$lib/math';
 
-  import { browser } from '$app/env';
+  import { coordinateLength, maxSize } from '$lib/config';
 
-  const dotSize = 24;
   $: x = 0;
   $: y = 0;
-
-  if (browser) {
-    document.documentElement.style.setProperty('--dot-size', dotSize + 'px');
-  }
-
-
-  $: {
-    if (browser) {
-      document.documentElement.style.setProperty('--dot-x', dotSize - x % dotSize + 'px');
-      document.documentElement.style.setProperty('--dot-y', dotSize - y % dotSize + 'px');
-    }
-  }
+  $: shouldShowPalette = false;
 
   let dragging = false;
 
   const moveCanvas = function moveCanvas(event) {
 
     if (dragging) {
-      x -= event.x - dragging.x;
-      y -= event.y - dragging.y;
+      let deltaX = event.x - dragging.x;
+      let deltaY = event.y - dragging.y;
+      x = modulo(x - deltaX, maxSize);
+      y = modulo(y - deltaY, maxSize);
       dragging.x = event.x;
       dragging.y = event.y;
     }
@@ -41,40 +36,43 @@
     dragging = null;
   }
 
-  const formatCoordinate = function formatCoordinate(coordinate) {
-    const sign = Math.sign(coordinate) === 1 ? '' : '-';
-    const value = Math.abs(coordinate).toString(16).padStart(4, 0);
-    return sign + value;
+  const showPalette = function showPalette(event) {
+    event.preventDefault();
+    dragging = null;
+    document.documentElement.style.setProperty('--palette-x', event.clientX + 'px');
+    document.documentElement.style.setProperty('--palette-y', event.clientY + 'px');
+    shouldShowPalette = true;
   }
 
+  const hidePalette = function hidePalette() {
+    event.preventDefault();
+    shouldShowPalette = false;
+  }
 </script>
 
-<div class="canvas" on:mousedown={startDragging} on:mouseup={stopDragging} on:mousemove={moveCanvas}>
-</div>
-
-<div class="hud">
-  ({ formatCoordinate(x) }, { formatCoordinate(y) })
+<div
+  class="canvas"
+  on:click={hidePalette}
+  on:contextmenu={showPalette}
+  on:mousedown={startDragging}
+  on:mouseup={stopDragging}
+  on:mousemove={moveCanvas}>
+>
+  <Sensor x={x} y={y} />
+  <Background x={x} y={y} />
+  <Hud x={x} y={y} />
+
+  {#if shouldShowPalette }
+    <Palette/>
+  {/if}
 </div>
 
 <style>
   .canvas {
     user-select: none;
-    background: radial-gradient(#aaa, #aaa 1px, #fff 1px, #fff var(--dot-size));
-    background-size: var(--dot-size) var(--dot-size);
-    background-position: var(--dot-x) var(--dot-y);
     padding: 0;
     margin: 0;
     width: 100vw;
     height: 100vh;
   }
-
-  .hud {
-    background-color: #fff;
-    padding: 4px;
-    position: fixed;
-    bottom: 2px;
-    right: 2px;
-    font-smooth: never;
-    -webkit-font-smoothing : none;
-  }
 </style>
