]> git.r.bdr.sh - rbdr/canvas/blob - src/lib/components/sensor.svelte
Save WIP
[rbdr/canvas] / src / lib / components / sensor.svelte
1 <script>
2 import { browser } from '$app/environment';
3 import { maxSize } from '$lib/config';
4 import { modulo } from '$lib/math';
5 import { canvas } from '$lib/stores/canvas';
6 import { sensor } from '$lib/stores/widgets';
7
8 const kSensorSize = 2;
9
10 let left;
11 let right;
12 let top;
13 let bottom;
14
15 let timer;
16 const fetchItems = function fetchItems(left, top, right, bottom) {
17 clearTimeout(timer);
18 timer = setTimeout(() => {
19 sensor.set({ left, top, right, bottom });
20 }, 50);
21 };
22
23 $: {
24 if (browser) {
25 left = modulo($canvas.x - window.screen.width * kSensorSize, maxSize);
26 top = modulo($canvas.y - window.screen.height * kSensorSize, maxSize);
27 right = left + 2 * window.screen.width * kSensorSize;
28 bottom = top + 2 * window.screen.height * kSensorSize;
29
30 fetchItems(left, top, right, bottom);
31 }
32 }
33 </script>