Skip to content

Commit 6a3a4ac

Browse files
committed
fix(Canvas): Improve performance by skipping unnecessary work when hit canvas is unneeded
1 parent efacef6 commit 6a3a4ac

2 files changed

Lines changed: 45 additions & 32 deletions

File tree

.changeset/violet-gifts-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
fix(Canvas): Improve performance by skipping unnecessary work when hit canvas is unneeded

packages/layerchart/src/lib/components/layout/Canvas.svelte

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -346,45 +346,53 @@
346346
context.restore();
347347
}
348348
349-
// sync hit canvas with main canvas
349+
/*
350+
* Sync hit canvas with main canvas
351+
*/
350352
if (hitCanvasContext) {
351-
// scale hit canvas to match main canvas
352-
scaleCanvas(hitCanvasContext, ctx.containerWidth, ctx.containerHeight);
353-
hitCanvasContext.clearRect(0, 0, ctx.containerWidth, ctx.containerHeight);
354-
355-
// reset and sync transform to the state after retainState components
356-
hitCanvasContext.resetTransform();
357-
hitCanvasContext.setTransform(mainTransformAfterRetain);
358-
359-
// reset color generator
360-
colorGenerator = rgbColorGenerator();
361-
362353
const inactiveMoving = !activeCanvas && transformCtx.moving;
363-
364-
// render retainState components on hit canvas (e.g., Group)
365-
for (const c of retainStateComponents) {
366-
const componentHasEvents = c.events && Object.values(c.events).filter((d) => d).length > 0;
367-
368-
if (componentHasEvents && !inactiveMoving && !transformCtx.dragging) {
369-
// since the transform was already applied via setTransform, skip rendering
370-
// the retainState component's transform again; proceed to its children
371-
continue;
354+
if (disableHitCanvas || transformCtx.dragging || inactiveMoving) {
355+
// Skip rendering hit canvas
356+
hitCanvasContext.clearRect(0, 0, ctx.containerWidth, ctx.containerHeight);
357+
} else {
358+
// scale hit canvas to match main canvas
359+
scaleCanvas(hitCanvasContext, ctx.containerWidth, ctx.containerHeight);
360+
hitCanvasContext.clearRect(0, 0, ctx.containerWidth, ctx.containerHeight);
361+
362+
// reset and sync transform to the state after retainState components
363+
hitCanvasContext.resetTransform();
364+
hitCanvasContext.setTransform(mainTransformAfterRetain);
365+
366+
// reset color generator
367+
colorGenerator = rgbColorGenerator();
368+
369+
// render retainState components on hit canvas (e.g., Group)
370+
for (const c of retainStateComponents) {
371+
const componentHasEvents =
372+
c.events && Object.values(c.events).filter((d) => d).length > 0;
373+
374+
if (componentHasEvents) {
375+
// since the transform was already applied via setTransform, skip rendering
376+
// the retainState component's transform again; proceed to its children
377+
continue;
378+
}
372379
}
373-
}
374380
375-
// render non-retainState components on hit canvas
376-
for (const c of nonRetainStateComponents) {
377-
const componentHasEvents = c.events && Object.values(c.events).filter((d) => d).length > 0;
381+
// render non-retainState components on hit canvas
382+
for (const c of nonRetainStateComponents) {
383+
const componentHasEvents =
384+
c.events && Object.values(c.events).filter((d) => d).length > 0;
378385
379-
if (componentHasEvents && !inactiveMoving && !transformCtx.dragging && !disableHitCanvas) {
380-
const color = getColorStr(colorGenerator.next().value);
381-
const styleOverrides = { styles: { fill: color, stroke: color, _fillOpacity: 0.1 } };
386+
if (componentHasEvents) {
387+
const color = getColorStr(colorGenerator.next().value);
388+
const styleOverrides = { styles: { fill: color, stroke: color, _fillOpacity: 0.1 } };
382389
383-
hitCanvasContext.save();
384-
c.render(hitCanvasContext, styleOverrides);
385-
hitCanvasContext.restore();
390+
hitCanvasContext.save();
391+
c.render(hitCanvasContext, styleOverrides);
392+
hitCanvasContext.restore();
386393
387-
componentByColor.set(color, c);
394+
componentByColor.set(color, c);
395+
}
388396
}
389397
}
390398
}

0 commit comments

Comments
 (0)