aboutsummaryrefslogtreecommitdiff
path: root/src/lib/components/sensor.svelte
blob: b6d38d83cfefeecb176a36741f24fc27ddcca0f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<script>
  import { browser } from '$app/environment';
  import { maxSize } from '$lib/config';
  import { modulo } from '$lib/math';
  import { canvas } from '$lib/stores/canvas';
  import { sensor } from '$lib/stores/widgets';

  const kSensorSize = 2;

  let left;
  let right;
  let top;
  let bottom;

  let timer;
  const fetchItems = function fetchItems( left, top, right, bottom ) {
    clearTimeout(timer);
		timer = setTimeout(() => {
      sensor.set({ left, top, right, bottom });
		}, 50);
  }

  $: {
    if (browser) {
      left = modulo($canvas.x - window.screen.width * kSensorSize, maxSize);
      top = modulo($canvas.y - window.screen.height * kSensorSize, maxSize);
      right = left + 2 * window.screen.width * kSensorSize;
      bottom = top + 2 * window.screen.height * kSensorSize;

      fetchItems(left, top, right, bottom);
    }
  }

</script>