question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DragBoundFunc position incorrect on transformed stage

See original GitHub issue

I implemented emulated screen scrolling of the canvas, using method 4 here: https://konvajs.org/docs/sandbox/Canvas_Scrolling.html#How-to-display-and-scroll-a-very-big-html5-canvas

Esentially the stage is re-positioned when we scroll like this:

      const PADDING = 500;
      function repositionStage() {
        var dx = scrollContainer.current.scrollLeft - padding;
        var dy = scrollContainer.current.scrollTop - padding;
        stage.current.container().style.transform = 'translate(' + dx + 'px, ' + dy + 'px)';
        stage.current.x(-dx);
        stage.current.y(-dy);
      }

Then I have a draggable rectangle with a dragBoundFunc like this:

<Rect
          height={32}
          width={120}
          fill={'red'}
          cornerRadius={8}
          draggable
          dragBoundFunc((pos) => { 
             console.log('position rectangle: ', pos) 
          }
/>

When the stage is initial (top: 0 , left: 0) the dragboundfunc returns the correct position. But as soon as I scroll the stage a bit , the position provided by the dragboundfunc is incorrect…

With the normal scrolling (not emulated) the dragBoundFunc position is always correct (even when the stage is scrolled)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lavrtoncommented, Apr 20, 2022

Just use dragmove event. Work with relative coordinates. They are much simpler to understand.

 onDragmove={(e) => {
                const pos = e.target.position();
                let newPosXgrid = Math.round(pos.x / 80) * 80;
                let newPosYgrid = Math.round(pos.y / 80) * 80;
                e.target.position({
                  x: newPosXgrid,
                  y: newPosYgrid
                });
}}
0reactions
jellohousecommented, Apr 20, 2022

Ok that works great! Makes sense - I will start using onDragMove instead.

Thanks so much for everything @lavrton - you we’re super helpful!

Read more comments on GitHub >

github_iconTop Results From Across the Web

konvajs/konva - Gitter
So newPos in dragBoundFunc must be “absolute position" of the node. Absolute position is not its top-left point.
Read more >
HTML5 Canvas Complex Drag and Drop Bounds - Konva
When I zoom in/out by setting the scale of the stage, like shown in another demo here, the snapping occurs at the wrong...
Read more >
React Konva Transformer Error "Cannot read property ...
There is an Masking component which imports Konva's Stage , Layout and Image and as well as the MaskingRectangle which can be transformed....
Read more >
konva - UNPKG
87, - Fix wrong `mouseleave` trigger for `Konva.Stage` ... It will fire BEFORE actual position of a node is changed. 554. 555, ##...
Read more >
JavaScript error with Ruby 3.1 and latest commit - Jumpstart Pro
indexOf(Xt(r))<0;){var o=me(r);if(o.transform!== ... You must return new absolute position from dragBoundFunc.")}(!this._lastPos||this.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found