a bit better performance

This commit is contained in:
Anton Lavrenov 2023-04-17 09:55:10 -05:00
parent d18d528d45
commit 6921855566
5 changed files with 29 additions and 19129 deletions

3
.gitignore vendored
View File

@ -61,3 +61,6 @@ dwsync.xml
intermediate
publish
.idea
konva.js
konva.min.js

19087
konva.js

File diff suppressed because it is too large Load Diff

12
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -2317,28 +2317,23 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
}
_getProtoListeners(eventType) {
let listeners = this._cache.get(ALL_LISTENERS);
// if no cache for listeners, we need to pre calculate it
if (!listeners) {
listeners = {};
const allListeners = this._cache.get(ALL_LISTENERS) ?? {};
let events = allListeners?.[eventType];
if (events === undefined) {
//recalculate cache
events = [];
let obj = Object.getPrototypeOf(this);
while (obj) {
if (!obj.eventListeners) {
obj = Object.getPrototypeOf(obj);
continue;
}
for (var event in obj.eventListeners) {
const newEvents = obj.eventListeners[event];
const oldEvents = listeners[event] || [];
listeners[event] = newEvents.concat(oldEvents);
}
const hierarchyEvents = obj.eventListeners?.[eventType] ?? [];
events.push(...hierarchyEvents);
obj = Object.getPrototypeOf(obj);
}
this._cache.set(ALL_LISTENERS, listeners);
// update cache
allListeners[eventType] = events;
this._cache.set(ALL_LISTENERS, allListeners);
}
return listeners[eventType];
return events;
}
_fire(eventType, evt) {
evt = evt || {};

View File

@ -37,20 +37,21 @@
});
const layer = new Konva.Layer();
stage.add(layer);
const rect = new Konva.Rect({
x: 0,
y: 0,
width: 100,
height: 100,
fill: 'red',
filters: [Konva.Filters.Blur],
blurRadius: 10,
});
layer.add(rect);
rect.cache();
setInterval(() => {
rect.blurRadius(rect.blurRadius() + 1);
}, 100);
console.time('load');
for (var i = 0; i < 30000; i++) {
const shape = new Konva.Circle({
x: Math.random() * stage.width(),
y: Math.random() * stage.height(),
radius: 10,
draggable: true,
fill: Konva.Util.getRandomColor(),
});
layer.add(shape);
}
console.timeEnd('load');
console.time('draw');
layer.draw();
console.timeEnd('draw');
</script>
</body>
</html>