Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I update Path without rerendering #2638

Open
doublelam opened this issue Sep 17, 2024 · 3 comments
Open

How can I update Path without rerendering #2638

doublelam opened this issue Sep 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@doublelam
Copy link

Description

const [path, setPath] = useState<SkPath>(Skia.Path.Make());

const panGesture = useMemo(
    () =>
      Gesture.Pan()
        .onBegin(e => {
          'worklet';

          const coords = getRelativeCoords(viewRef as any, e.absoluteX, e.absoluteY);
          path.moveTo(coords?.x||0, coords?.y||0)
        })
        .onUpdate(e => {
          'worklet';

          const coords = getRelativeCoords(viewRef as any, e.absoluteX, e.absoluteY);
          path.lineTo(coords?.x||0, coords?.y||0);
        })
    [],
  );

return (
    <GestureDetector gesture={panGesture}>
      <View ref={viewRef} style={styles.canvas}>
        <Canvas style={{flex: 1}}>
          {/* Drawing path */}
            <Path
              path={path}
              // style="stroke"
              color={strokeColor}
              stroke={{width: typeof strokeWidth === 'number' ? strokeWidth : strokeWidth.value}}
            />
        </Canvas>
      </View>
    </GestureDetector>
  );

it doesn't work. how can I change Path without rerendering?

Version

1.3.11

Steps to reproduce

Above code

Snack, code example, screenshot, or link to a repository

no

@doublelam doublelam added the bug Something isn't working label Sep 17, 2024
@mrEuler
Copy link
Contributor

mrEuler commented Sep 18, 2024

@doublelam Use sharedValue if you don't want to rerender entire screen:

const currentPath = useSharedValue(Skia.Path.Make());
const panGesture = useMemo(
    () =>
      Gesture.Pan()
        .onBegin(e => {
          'worklet';
          currentPath.modify((v) => {
                const coords = getRelativeCoords(viewRef as any, e.absoluteX, e.absoluteY);

                v.lineTo(coords.x || 0, coords.y || 0);
                return v;
            });
        })
        .onUpdate(e => {
          'worklet';

          const coords = getRelativeCoords(viewRef as any, e.absoluteX, e.absoluteY);
          currentPath.modify((v) => {
                const coords = getRelativeCoords(viewRef as any, e.absoluteX, e.absoluteY);

                v.lineTo(coords.x || 0, coords.y || 0);
                return v;
            });       
          })
    [],
  );
..... // next code stays same

@mrEuler
Copy link
Contributor

mrEuler commented Sep 18, 2024

@wcandillon issue can be closed

@doublelam
Copy link
Author

doublelam commented Sep 19, 2024

Ok now it works, but I need to use derived value or it does not work

 const derived = useDerivedValue(() => {
  return pathSharedVal.value.toSVGString()
});

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants