Code Monkey home page Code Monkey logo

Comments (3)

wcandillon avatar wcandillon commented on May 25, 2024

from react-native-skia.

chiefchief avatar chiefchief commented on May 25, 2024

I strongly recommend to use gesture-handler for such scenario. We are not really supporting this API anymore: https://shopify.github.io/react-native-skia/docs/animations/gestures Based on the code you provided you will reanimated if want to update the path on the UI thread and have it shown directly onto the canvas. I hope this helps. William

On Fri, Feb 23, 2024 at 3:12 PM Silver O @.> wrote: Description When user tap and hold, triggers - "onStart" but no "onActive" on move, immediately after start moving triggers "onEnd" Version 0.1.240 Steps to reproduce Code provided below Snack, code example, screenshot, or link to a repository import { Canvas, Path, Skia, useCanvasRef, useTouchHandler } from @./react-native-skia'; import React, { forwardRef, useEffect, useImperativeHandle } from 'react'; const activePath = Skia.Path.Make(); export const SignaturePad = forwardRef<SignaturePad, SignaturePadProps>(({ height, width, onUpdate }, ref) => { const canvasRef = useCanvasRef(); useEffect(() => { return () => { activePath.reset(); }; }, []); useImperativeHandle(ref, () => ({ clear: () => { activePath.reset(); onUpdate?.(''); }, })); const onTouch = useTouchHandler({ onStart: ({ x, y }) => { activePath.moveTo(x, y); }, onActive: ({ x, y }) => { activePath.lineTo(x, y); }, onEnd: () => { console.log('END', Date.now()); }, }); return ( <Canvas ref={canvasRef} style={{ height, width, backgroundColor: 'white' }} onTouch={onTouch}> ); }); type SignaturePadProps = { width: number; height: number; onUpdate?: (img: string) => void; }; export type SignaturePad = { clear: () => void; }; — Reply to this email directly, view it on GitHub or unsubscribe. You are receiving this email because you are subscribed to this thread. Triage notifications on the go with GitHub Mobile for iOS or Android.

  1. on ios throw error
import { Canvas, Path, Skia, useCanvasRef } from '@shopify/react-native-skia';
import React, { forwardRef, useEffect, useImperativeHandle } from 'react';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';

const activePath = Skia.Path.Make();

const onBeginWorklet = (e) => {
  'worklet';
  activePath.moveTo(e.x, e.y);
};
const onChangeWorklet = (e) => {
  'worklet';
  activePath.lineTo(e.x, e.y);
};

export const SignaturePad = forwardRef<SignaturePad, SignaturePadProps>(({ height, width, onUpdate }, ref) => {
  const canvasRef = useCanvasRef();

  useEffect(() => {
    return () => {
      activePath.reset();
    };
  }, []);

  useImperativeHandle(ref, () => ({
    clear: () => {
      activePath.reset();
      onUpdate?.('');
    },
  }));

  const gesture = Gesture.Pan().onBegin(onBeginWorklet).onChange(onChangeWorklet);

  return (
    <GestureDetector gesture={gesture}>
      <Canvas ref={canvasRef} style={{ height, width, backgroundColor: 'white' }}>
        <Path path={activePath} style={'stroke'} strokeWidth={2} />
      </Canvas>
    </GestureDetector>
  );
});

type SignaturePadProps = {
  width: number;
  height: number;
  onUpdate?: (img: string) => void;
};

export type SignaturePad = {
  clear: () => void;
};
Screenshot 2024-02-23 at 18 30 33
  1. if run on JS the app just crash and no error
const onBeginWorklet = (e) => {
  runOnJS(activePath.moveTo)(e.x, e.y);
};
const onChangeWorklet = (e) => {
  runOnJS(activePath.lineTo)(e.x, e.y);
};
  1. on Android these actions don't trigger at all

The only way on Android that works almost correctly is this one.

import { Canvas, Path, Skia, useCanvasRef } from '@shopify/react-native-skia';
import React, { forwardRef, useEffect, useImperativeHandle } from 'react';

const activePath = Skia.Path.Make();

export const SignaturePad = forwardRef<SignaturePad, SignaturePadProps>(({ height, width, onUpdate }, ref) => {
  const canvasRef = useCanvasRef();

  useEffect(() => {
    return () => {
      activePath.reset();
    };
  }, []);

  useImperativeHandle(ref, () => ({
    clear: () => {
      activePath.reset();
      onUpdate?.('');
    },
  }));

  return (
    <Canvas
      ref={canvasRef}
      style={{ height, width, backgroundColor: 'white' }}
      onTouchStart={(e) => {
        activePath.moveTo(e.nativeEvent.locationX, e.nativeEvent.locationY);
      }}
      onTouchMove={(e) => {
        activePath.lineTo(e.nativeEvent.locationX, e.nativeEvent.locationY);
      }}
    >
      <Path path={activePath} style={'stroke'} strokeWidth={2} />
    </Canvas>
  );
});

type SignaturePadProps = {
  width: number;
  height: number;
  onUpdate?: (img: string) => void;
};

export type SignaturePad = {
  clear: () => void;
};

However, the line appears on the next tap (for example, when the user taps and moves, nothing appears, but on the next tap, the previous line will appear).

from react-native-skia.

chiefchief avatar chiefchief commented on May 25, 2024

useTouchHandler works properly without modal on android

from react-native-skia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.