0.5.0 • Published 2 years ago

@ivine/react-native-draw v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@ivine/react-native-draw

NPM badge CircleCI Status Platform badge

Cross-platform React Native drawing component based on SVG

Note: Performance may not be great, this is not my main focus right now, but will be tackled via resolving this issue

Drawing Example

Example application with React Native performance overlay

Installation

npm install @ivine/react-native-draw
# or
yarn add @ivine/react-native-draw

Also, you need to install react-native-gesture-handler, @react-native-community/slider and react-native-svg, and follow their installation instructions.

Usage

import React from 'react';
import { Draw, DrawRef } from "@ivine/react-native-draw";

export default function App() {
  const drawRef = useRef<DrawRef>(null);

  const removeLastPath = () {
    drawRef.current.?undo();
  }

  const clearDrawing = () {
    drawRef.current.?clear();
  }

  // ... for more ref functions, look below

  return (
    <Draw
      ref={drawRef}
      height={400}
      width={300}
      initialValues={{
        color: "#B644D0",
        thickness: 10,
        opacity: 0.5,
        paths: []
      }}
      brushPreview="none"
      canvasStyle={{ elevation: 0, backgroundColor: "red" }}
    />
  )
}

Props

All of the props are optional

namedescriptiontypedefault
colorsColor palette colors, specifying the color palette sections each containing rows of colorsstring[][][]DEFAULT_COLORS
heightHeight of the canvasnumberheight of the window - 80
widthWidth of the canvasnumberwidth of the window
autoDismissColorPickerAutomatically close the color picker after selecting a colorbooleanfalse
initialValuesInitial values for color the brush and pathsDrawInitialValuessee below
hideBottomHide all of the bottom section, below the canvas, or only certain functionalitiesboolean or HideBottomfalse
brushPreviewChange brush preview preset or remove itstroke or dot or nonestroke
simplifyOptionsSVG simplification optionsSimplifyOptionssee below
canvasStyleOverride the style of the container of the canvasStyleProp-
buttonStyleOverride the style of the buttonsStyleProp-
onPathsChangeCallback function when paths change(paths: PathType[]) => any-
eraserSizeWidth of eraser (to compensate for path simplification)number5

DrawInitialValues

namedescriptiontypedefault
colorInitial brush color, from the colors providedstringthe first color of the first row in the first section from colors
thicknessInitial thickness of the brush strokesnumber3
opacityInitial opacity of the brush strokesnumber1
pathsPaths to be already drawnPathType[][]
toolInitial tool of the canvasDrawingToolbrush

SimplifyOptions

namedescriptiontypedefault
simplifyPathsEnable SVG path simplification on paths, except the one currently being drawnbooleantrue
simplifyCurrentPathEnable SVG path simplification on the stroke being drawnbooleanfalse
amountAmount of simplification to applynumber10
roundPointsIgnore fractional part in the points. Improves performancebooleantrue

Ref functions

namedescriptiontype
setColorSet the brush colorDispatch<SetStateAction<string>>
undoUndo last brush stroke() => void
clearRemoves all brush strokes() => void
getPathsGet brush strokes data() => PathType[]
addPathAppend a path to the current drawing paths(path: PathType) => void
getSvgGet SVG path string of the drawing() => string

Helper functions

  • If you need to create an SVG path, createSVGPath() is available to create the string representation of an SVG path.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT