radames commited on
Commit
2fe0733
1 Parent(s): 2d55581

zoom issues

Browse files
frontend/src/lib/PaintCanvas.svelte CHANGED
@@ -1,5 +1,5 @@
1
  <script lang="ts">
2
- import { zoom } from 'd3-zoom';
3
  import { select } from 'd3-selection';
4
  import { onMount } from 'svelte';
5
  import { PUBLIC_UPLOADS } from '$env/static/public';
@@ -15,7 +15,7 @@
15
  const height = 512 * 4;
16
  const width = 512 * 4;
17
 
18
- export let canvasEl: HTMLCanvasElement = undefined;
19
 
20
  let containerEl: HTMLDivElement;
21
  let canvasCtx: CanvasRenderingContext2D;
@@ -38,34 +38,29 @@
38
  }
39
 
40
  onMount(() => {
41
- const scale = width / containerEl.clientWidth;
42
- const translatePadding = 0.1;
43
- const scalePadding = 3;
44
  const zoomHandler = zoom()
45
- .scaleExtent([1 / scale / scalePadding, 1])
46
- // .extent([
47
- // [0, 0],
48
- // [width, height]
49
- // ])
50
  .translateExtent([
51
- [-width * translatePadding, -height * translatePadding],
52
- [width * (1 + translatePadding), height * (1 + translatePadding)]
53
  ])
54
  .tapDistance(10)
55
  .on('zoom', zoomed);
56
 
57
  const selection = select(canvasEl.parentElement)
58
  .call(zoomHandler as any)
59
- .call(zoomHandler.scaleTo as any, 1 / scale / 2)
 
60
  .on('pointermove', handlePointerMove)
61
  .on('pointerleave', handlePointerLeave);
62
 
63
  canvasCtx = canvasEl.getContext('2d') as CanvasRenderingContext2D;
64
  function zoomReset() {
65
- console.log('zoom reset');
66
- const scale = width / containerEl.clientWidth;
67
- zoomHandler.scaleExtent([1 / scale / scalePadding, 1]);
68
- selection.call(zoomHandler.scaleTo as any, 1 / scale / 2);
69
  }
70
  window.addEventListener('resize', zoomReset);
71
  return () => {
 
1
  <script lang="ts">
2
+ import { zoom, zoomIdentity } from 'd3-zoom';
3
  import { select } from 'd3-selection';
4
  import { onMount } from 'svelte';
5
  import { PUBLIC_UPLOADS } from '$env/static/public';
 
15
  const height = 512 * 4;
16
  const width = 512 * 4;
17
 
18
+ export let canvasEl: HTMLCanvasElement = document.createElement('canvas');
19
 
20
  let containerEl: HTMLDivElement;
21
  let canvasCtx: CanvasRenderingContext2D;
 
38
  }
39
 
40
  onMount(() => {
41
+ const padding = 200;
42
+ const scale = (width + padding * 2) / containerEl.clientWidth;
 
43
  const zoomHandler = zoom()
44
+ .scaleExtent([1 / scale / 2, 3])
 
 
 
 
45
  .translateExtent([
46
+ [-padding, -padding],
47
+ [width + padding, height + padding]
48
  ])
49
  .tapDistance(10)
50
  .on('zoom', zoomed);
51
 
52
  const selection = select(canvasEl.parentElement)
53
  .call(zoomHandler as any)
54
+ .call(zoomHandler.transform as any, zoomIdentity)
55
+ .call(zoomHandler.scaleTo as any, 1 / scale)
56
  .on('pointermove', handlePointerMove)
57
  .on('pointerleave', handlePointerLeave);
58
 
59
  canvasCtx = canvasEl.getContext('2d') as CanvasRenderingContext2D;
60
  function zoomReset() {
61
+ const scale = (width + padding * 2) / containerEl.clientWidth;
62
+ selection.call(zoomHandler.transform as any, zoomIdentity);
63
+ selection.call(zoomHandler.scaleTo as any, 1 / scale);
 
64
  }
65
  window.addEventListener('resize', zoomReset);
66
  return () => {
frontend/src/lib/PaintFrame.svelte CHANGED
@@ -30,19 +30,25 @@
30
  }
31
 
32
  function dragged(event: Event) {
33
- const x = round(transform.invertX(event.x) - 512 / 2);
34
- const y = round(transform.invertY(event.y) - 512 / 2);
35
  position = {
36
  x,
37
  y
38
  };
 
 
 
 
 
 
39
  }
40
 
41
  function dragended(event: Event) {
42
  isDragging = false;
43
 
44
- const x = round(transform.invertX(event.x) - 512 / 2);
45
- const y = round(transform.invertY(event.y) - 512 / 2);
46
 
47
  myPresence.update({
48
  frame: {
@@ -51,11 +57,39 @@
51
  }
52
  });
53
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  const dragHandler = drag().on('start', dragstarted).on('drag', dragged).on('end', dragended);
55
  select(frameElement).call(dragHandler as any);
56
  });
57
  </script>
58
 
59
  <div bind:this={frameElement}>
60
- <Frame {color} {position} loadingState={$loadingState} {prompt} {transform} {isDragging} {interactive} />
 
 
 
 
 
 
 
 
61
  </div>
 
30
  }
31
 
32
  function dragged(event: Event) {
33
+ const x = round(transform.invertX(event.x)) - 256;
34
+ const y = round(transform.invertY(event.y)) - 256;
35
  position = {
36
  x,
37
  y
38
  };
39
+ myPresence.update({
40
+ cursor: {
41
+ x: x + 256,
42
+ y: y + 256
43
+ }
44
+ });
45
  }
46
 
47
  function dragended(event: Event) {
48
  isDragging = false;
49
 
50
+ const x = round(transform.invertX(event.x)) - 256;
51
+ const y = round(transform.invertY(event.y)) - 256;
52
 
53
  myPresence.update({
54
  frame: {
 
57
  }
58
  });
59
  }
60
+ // Update cursor presence to current pointer location
61
+ function handlePointerMove(event: PointerEvent) {
62
+ event.preventDefault();
63
+ const x = round(transform.invertX(event.clientX));
64
+ const y = round(transform.invertY(event.clientY));
65
+
66
+ myPresence.update({
67
+ cursor: {
68
+ x,
69
+ y
70
+ }
71
+ });
72
+ }
73
+
74
+ // When the pointer leaves the page, set cursor presence to null
75
+ function handlePointerLeave() {
76
+ myPresence.update({
77
+ cursor: null
78
+ });
79
+ }
80
  const dragHandler = drag().on('start', dragstarted).on('drag', dragged).on('end', dragended);
81
  select(frameElement).call(dragHandler as any);
82
  });
83
  </script>
84
 
85
  <div bind:this={frameElement}>
86
+ <Frame
87
+ {color}
88
+ {position}
89
+ loadingState={$loadingState}
90
+ {prompt}
91
+ {transform}
92
+ {isDragging}
93
+ {interactive}
94
+ />
95
  </div>