konvajs / konva

Konva.js is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.

Home Page:http://konvajs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

rienheuver opened this issue · comments

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";

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.

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

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.