1.4.0 • Published 3 months ago

react-native-free-canvas v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

react-native-free-canvas

Freehand sketch on canvas based on @shopify/react-native-skia

Install

You need to install following dependencies

"@shopify/react-native-skia": ">=1.0.0",
"react": ">=18.0.0",
"react-native": ">=0.72.0",
"react-native-gesture-handler": ">=2.0.0",
"react-native-reanimated": ">=3.0.0"

Usage

import FreeCanvas from 'react-native-free-canvas';

const App = () => {
  return (
    <>
      <FreeCanvas
        // style={{flex: 1}} 
        style={styles.flex1} //avoid using a new Object to prevent unnecessary re-rendering
      />
    </>
  )
};

Make Line Smoother

import {CornerPathEffect} from '@shopify/react-native-skia';

const effects = useMemo(() => <CornerPathEffect r={32} />, []);

// Add CornerPathEffect component to pathEffect props
<FreeCanvas
  // style={{flex: 1}}
  style={styles.flex1}
  pathEffect={effects} 
/>

Properties

PropertyTypeDescription
styleStyleProp<ViewStyle>Custom styles for the canvas
strokeColorstring | SharedValue<string>Color of the stroke
strokeWidthnumber | SharedValue<number>Width of the stroke
backgroundColorstring | SharedValue<string>Background color of the canvas
backgroundReact.ReactNodeSkia component for background
foregroundReact.ReactNodeSkia component for foreground
pathEffectReact.ReactNodeSkia Path Effects (see documentation)
zoomablebooleanEnable/disable zooming
onDrawEnd() => voidCallback function when drawing ends
onTranslate(x: number, y: number) => voidWorklet function that runs on UI thread when canvas is translated
onScale(scale: number) => voidWorklet function that runs on UI thread when canvas is scaled
onTransformOriginChange(x: number, y: number) => voidWorklet function that runs on UI thread when transform origin changes

Transform & Scale

The order of the transform and scale in animated style for the Canvas while zooming should be:

{
  transform: [
    { translateX: translateSharedVal.value.x },
    { translateY: translateSharedVal.value.y },
    { scale: scaleSharedVal.value },
  ],
  transformOrigin: originSharedVal.value.concat([0]),
}

Methods or Values

MethodParametersReturn TypeDescription
reset-voidResets the canvas
resetZoom(duration?: number)voidResets the zoom level
undo(step?: number)voidUndoes the last drawing action(s)
toBase64(fmt?: ImageFormat, quality?: number)Promise<string | undefined>Converts canvas to base64 string
getSnapshot-Promise<SkImage | undefined> | undefinedGets a snapshot of the canvas
toPaths-DrawnPath[]Returns the drawn paths
drawPaths(paths: DrawnPath[])voidDraws the given paths on the canvas
translateSharedValue-SharedValue<{ x: number; y: number }>Shared value for translation
scaleSharedValue-SharedValue<number>Shared value for scale
transformOriginSharedValue-SharedValue<[number, number]>Shared value for transform origin

Inspired By

wobsoriano/rn-perfect-sketch-canvas A React Native component for drawing perfect pressure-sensitive freehand lines using perfect-freehand and Skia renderer.

1.2.0

5 months ago

1.1.1

6 months ago

1.3.3

4 months ago

1.2.3

4 months ago

1.4.0

3 months ago

1.2.2

5 months ago

1.1.3

5 months ago

1.2.1

5 months ago

1.1.2

5 months ago

1.1.0

6 months ago

1.0.4

6 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago