question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance: Slow Rerenders, if Image has Filters

See original GitHub issue

Hey,

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.

Screenshot from 2020-02-18 19-04-43

filtercache.txt

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:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
lavrtoncommented, Feb 18, 2020

You don’t need to recache if you change:

  1. transform (such as position, rotation or scale)
  2. or filter properties (such as brightness).

You will have to recache on other changes that affect the image itself (like stroke, or shadow).

1reaction
lavrtoncommented, Feb 18, 2020

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/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found