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.

Filters with images

See original GitHub issue

Hey all, been loving Konva I’ve got a question about filters with images. Basically I need to load multiple images onto a layer in a stage and remove black bg by having a colour threshold and then fill white pixels as a colour. Also on scroll on a div these images would change to the next images in the list that I’ve already cached the image data

I’ve managed to get the Konva setup working with regular Konva.js without react loading the images, caching and adding filters within the onload, then redrawing.

Can we apply filters and threshold to multiple Konva <Image /> without having to add a custom component with onload state and lifecycle methods? I have the pixels/image already so I don’t need to load from a url.

The second FastLayer here contains the images I wish to manipulate with filters and the imaged change on scroll.

<Stage width={width} height={height}>
  <FastLayer>
    <Image x={x} y={y} image={image.getImage()} />
  </FastLayer>
  <FastLayer>
    <Image x={x} y={y} image={arrayImage[0]} />
    <Image x={x} y={y} image={arrayImage[1]} />
  </FastLayer>
</Stage>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lavrtoncommented, Oct 9, 2018

Do not create <canvas> elements inside render() function. It is possible that element can be “rendered” many times. Instead create canvas somewhere in lifecycle methods.

Somethis like this:

export default class CustomImage extends React.Component {
  state = {
    rawCanvas: null
  }
  componentDidMount() {
     const rawCanvas = document.createElement('canvas');
     this.setup(rawCanvas, this.props.image, this.props.threshold);
     this.setState({
       rawCanvas
     });
  }
  setup = (rawCanvas, image, threshold) => {
    const imageCanvas = image;
    const { width, height } = imageCanvas;
    const ctx = rawCanvas.getContext('2d');
    rawCanvas.height = width;
    rawCanvas.width = height;
    const manipulatedImageData = imageCanvas.getOverlay(color, threshold);
    const data = new ImageData(manipulatedImageData, width, height);
    ctx.putImageData(data, 0, 0);
  };
  render() {
    const { size, x, y, opacity } = this.props;
    if (!image) return null;
    return <Image opacity={opacity} size={size} x={x} y={y} image={this.state.rawCanvas} />;
  }
}
0reactions
lavrtoncommented, Oct 18, 2018

@jamesg1 right, you have to call cache manually, but you can add filters inside render() function.

I am closing the issue. Add a comment if you still have questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Photo Effects: BeFunky - Free Online Photo Filters
With BeFunky's Photo Editor you can easily add online photo effects and artsy filters to create unique images that can't be captured by...
Read more >
Photo Effects | PhotoMania: Free Online Photo Effects, Filters ...
Create Amazing Effects with a Single Click. or Use Web Application >
Read more >
Online Photo Effects: Free Photo Filters For Images | Canva
Add creative photo effects to spice up your images with Canva's free online ... We have a bounty of photo filters to choose...
Read more >
Free Photo Filters & Online Photo Effects | Adobe Express
Handpick the perfect filters for your pictures with the free Adobe Express app. Get started editing your image filters quickly and easily online...
Read more >
Free Online Photo Filters and Image Effects Editor ...
PhotoFilters.com: Free and online Photo filter and effects editor. Apply many beautiful filters and effects to your own photos and images.
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