Skip to content

Commit

Permalink
Update drag action to calculate cursor position offset on the node it…
Browse files Browse the repository at this point in the history
…self. (#8)
  • Loading branch information
rihardsgravis authored Jan 22, 2021
1 parent b55ae35 commit 3285e47
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 7 additions & 1 deletion core/src/actions/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ const useDrag: Action<OnDrag> = (node, onDrag) => {
return;
}

let offset: number;

const onMousedown = (event: MouseEvent | TouchEvent) => {
if (event.target !== node && (event.target as HTMLElement).getAttribute('draggable') === null) {
return;
}

const clientX = 'clientX' in event ? event.clientX : event.touches[0].clientX;
offset = clientX - node.getBoundingClientRect().left;

window.addEventListener('mousemove', onMousemove, { passive: true });
window.addEventListener('touchmove', onMousemove, { passive: true });
window.addEventListener('mouseup', onMouseup, { passive: true });
Expand All @@ -36,7 +42,7 @@ const useDrag: Action<OnDrag> = (node, onDrag) => {
const clientX = 'clientX' in event ? event.clientX : event.touches[0].clientX;
const clientY = 'clientY' in event ? event.clientY : event.touches[0].clientY;

const x = clientX + scrollX - box.width / 2;
const x = clientX + scrollX - offset;
const y = clientY + scrollY - BAR_HEIGHT;

onDrag(x, y, box);
Expand Down
2 changes: 0 additions & 2 deletions modules/src/LFO.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
$: node.frequency.value = state.freq;
$: node.type = state.type;
$: console.log(state.freq);
</script>

<Faceplate title="LFO" color="var(--color-2)" light>
Expand Down

1 comment on commit 3285e47

@vercel
Copy link

@vercel vercel bot commented on 3285e47 Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.