Performance: Slow Rerenders, if Image has Filters
See original GitHub issueHey,
first of all: Thanks for your amazing work.
I am currently try to apply filters to a user uploaded image, which I handle as HTMLImageElement internally.
If I try to apply filter to an image, I ran into some performance problems. My draw needs almost 3s and it seems, that the Stage size is not relevant. Also, if I remove image filters, everything works fine.
I also appended a screenshot and the profiler files.
This is one of the elements, which I am using. Other ones are quite similar. KImage is Image from Konva.
import { Brighten } from 'konva/lib/filters/Brighten';
import { HSL } from 'konva/lib/filters/HSL';
import { Contrast } from 'konva/lib/filters/Contrast';
const imageFilters = [
Brighten,
Contrast,
HSL,
];
<Stage
ref={stageRef}
className={className}
width={containerDimensions.width}
height={containerDimensions.height}
x={containerDimensions.width * 0.5}
y={containerDimensions.height * 0.5}
offsetX={containerDimensions.width * 0.5}
offsetY={containerDimensions.height * 0.5}
opacity={0.2}
scaleX={containerScale}
scaleY={containerScale}
rotation={transform.angle}
>
<FastLayer>
<KImage
ref={ref => {
ref?.cache();
ref?.getLayer()?.batchDraw();
}}
x={backgroundImg.width * transform.scale / 2 - (backgroundImg.width * transform.scale - containerDimensions.width) / 2 + transform.x}
y={backgroundImg.height * transform.scale / 2 - (backgroundImg.height * transform.scale - containerDimensions.height) / 2 + transform.y}
offsetX={backgroundImg.width / 2 }
offsetY={backgroundImg.height / 2 }
scaleX={transform.scale}
scaleY={transform.scale}
image={backgroundImg}
filters={imageFilters}
brightness={getBrightnessFromPercentage(effects.brightness)}
contrast={getContrastFromPercentage(effects.contrast)}
saturation={getSaturationFromPercentage(effects.saturation)}
/>
</FastLayer>
</Stage>
Can you give me a hint what to try next?
Best, Johann
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
React re-renders guide: everything, all at once - Developer way
Comprehensive guide on React re-renders. The guide explains what are re-renders, what is necessary and unnecessary re-render, ...
Read more >How to prevent react re-rendering images that are coming ...
The search and sort functions are working dynamically, as soon as I type a letter, it finds the match but re-renders all of...
Read more >Fix the slow render before you fix the re-render - Kent C. Dodds
You should diagnose and fix that first. Once you've fixed that problem, then you can profile your app again and see if you...
Read more >How To Avoid Performance Pitfalls in React with memo ...
Either the component re-renders more than expected or the component has a data-heavy operation that can cause each render to be slow.
Read more >How To Prevent Unnecessary React State Update Re-renders
State management is one of the most important aspects of React development ... This can be expensive for the browser and slow down...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You don’t need to recache if you change:
brightness
).You will have to recache on other changes that affect the image itself (like stroke, or shadow).
Also in some cases you may try to use CSS filters (the usually faster). Take a look here for a related demo: https://lavrton.com/case-study-image-labeling-for-machine-learning/