konvajs / konva

Konva.js is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.
http://konvajs.org/
Other
11.07k stars 896 forks source link

Cannot read 'isDragging' even though importing (unused) Core #1763

Closed rienheuver closed 1 month ago

rienheuver commented 1 month ago

I'm (also) running into the below error, as was mentioned in #1730

Uncaught TypeError: Cannot read properties of undefined (reading 'isDragging')
    at Object.isDragging (Global.js:46:36)
    at Stage._pointerleave (Stage.js:349:46)
    at content.addEventListener.passive (Stage.js:308:32)

However, if I import the following line it doesn't fix it, the problem persists.

import Konva from 'konva/lib/Core';

And there's another problem with that: vscode sees it as an unused import since I import all other Konva-object directly as below. So therefore it will want to remove that import. (I can prevent that from happening with a hack of course, but it doesn't fix the isDragging either way.)

import { Layer } from "konva/lib/Layer";
import { KonvaEventObject } from "konva/lib/Node";
import { Stage } from "konva/lib/Stage";
import { Tween } from "konva/lib/Tween";
import { Line } from "konva/lib/shapes/Line";
rienheuver commented 1 month ago

I'm currently using a workaround that fixes both problem:

import { Stage } from "konva/lib/Stage";

const stage = new Konva.Stage();

Now the Konva core import is used and the error is gone. But it's not the best fix of course, since I'd rather import Stage directly just as all other classes.

lavrton commented 1 month ago

Please make a small demo. I can't reproduce.

rienheuver commented 1 month ago

Sure thing! https://github.com/rienheuver/konva-repro

lavrton commented 1 month ago

Looks like vite automatically removes unused import of import Konva from 'konva/lib/Core';. There are two solutions:

  1. Use the import as you did in new Konva.Stage();
  2. Use unnamed import import 'konva/lib/Core';

Technically, it is possible to avoid crash on Cannot read properties of undefined (reading 'isDragging'), but I still think core must be loaded.

rienheuver commented 4 weeks ago

Sorry for the late reply. I'll use the new Konva.Stage() for now as a fix. Maybe it's a good idea to document this somewhere in case others use a similar setup?