konvajs / react-konva

React + Canvas = Love. JavaScript library for drawing complex canvas graphics using React.
https://konvajs.github.io/docs/react/
MIT License
5.8k stars 260 forks source link

How to update the clipFunc state #806

Closed ilomon10 closed 4 months ago

ilomon10 commented 4 months ago

i have this kind of component to clip the children based on rectRef:

const ArtboardComponent = ({width, height})=>{
  const rectRef = useRef();
  const handleClipFunc = (ctx)=>{
    const rect = rectRef.current;
    ctx.save();
    ctx.rect(0, 0, rect.width(), rect.height())
    ctx.restore();
  }
  return (
    <Group>
      <Rect ref={rectRef} width={width} height={height}/>
      <Group clipFunc={handleClipFunc}>
       // children shape to be cliped
      </Group>
    </Group>
  )
}

the rectRef does not update in the clipFunc after get hot change, it just update one time on initial render

lavrton commented 4 months ago

Please make a demo.

ilomon10 commented 4 months ago

@lavrton i use state to change it not change, even direct change to groupRef it cannot modify the clipping area.

https://codesandbox.io/p/sandbox/konva-clipfunc-sq65y8

lavrton commented 4 months ago

You don't need to reset the clip function. You can put some logs in it to see if it is called correctly.

The issue is in combination with Transformer. Transformer doesn't change the width and height of the rectangle. Instead, it is changing scaleX and scaleY, so it see the effect you may need to do this:

ctx.rect(0, 0, rect.width() * rect.scaleX(), rect.height() * rect.scaleY());
ilomon10 commented 4 months ago

thank you @lavrton it solved now