All Projects → BenJeau → react-native-draw

BenJeau / react-native-draw

Licence: MIT License
SVG based data-driven React Native drawing component 🎨

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to react-native-draw

SwiftUI-Shapes
Commonly Used Shapes and Utilities In SwiftUI
Stars: ✭ 52 (+26.83%)
Mutual labels:  drawing
at-the-wall
A Game of Thrones scene coded in pure SCSS and HTML
Stars: ✭ 12 (-70.73%)
Mutual labels:  drawing
svg-path-outline
Create an outline surrounding an SVG path
Stars: ✭ 56 (+36.59%)
Mutual labels:  drawing
mapus
A map tool with real-time collaboration 🗺️
Stars: ✭ 2,687 (+6453.66%)
Mutual labels:  drawing
Dynamic-Draw
This is a plugin for Paint.NET that extends it with dynamic brush capabilities. Images can be used as brushes to draw with options to adjust and jitter the brush in different ways.
Stars: ✭ 15 (-63.41%)
Mutual labels:  drawing
Anondraw
Source code of anondraw.com. An artistic collaboration tool with an infinite canvas.
Stars: ✭ 43 (+4.88%)
Mutual labels:  drawing
sdfx
C# bindings for https://github.com/andlabs/libui targeting .NET Standard.
Stars: ✭ 31 (-24.39%)
Mutual labels:  drawing
sinus-studio
Draws and animates a 3D spirograph-like curves.
Stars: ✭ 19 (-53.66%)
Mutual labels:  drawing
Elementa
A simple, declarative GUI library for Minecraft
Stars: ✭ 160 (+290.24%)
Mutual labels:  drawing
vue-draw
Drawing Component for VueJS
Stars: ✭ 26 (-36.59%)
Mutual labels:  drawing
ocaml-cairo
Binding to Cairo, a 2D Vector Graphics Library.
Stars: ✭ 39 (-4.88%)
Mutual labels:  drawing
HoroscopeDrawer
JavaScript-Library to draw celestial charts in SVG-Format.
Stars: ✭ 51 (+24.39%)
Mutual labels:  drawing
isometric
A lightweight JavaScript library, written in TypeScript to create isometric projections using SVGs
Stars: ✭ 53 (+29.27%)
Mutual labels:  drawing
pixelino
Pixel-drawing app for iOS ✍️
Stars: ✭ 45 (+9.76%)
Mutual labels:  drawing
idraw
A simple JavaScript framework for Drawing on the web.(一个面向Web绘图的JavaScript框架)
Stars: ✭ 436 (+963.41%)
Mutual labels:  drawing
durdraw
Animated Unicode, ANSI and ASCII Art Editor for Linux/Unix/macOS
Stars: ✭ 55 (+34.15%)
Mutual labels:  drawing
mvt
Draw Mapbox Vector Tiles (MVT) in Go
Stars: ✭ 46 (+12.2%)
Mutual labels:  drawing
SyncPaint
A web app for synchronized group drawing. Draw together with other people in real time.
Stars: ✭ 42 (+2.44%)
Mutual labels:  drawing
petter
Petter – a vector-graphic-based pattern generator.
Stars: ✭ 23 (-43.9%)
Mutual labels:  drawing
drawsvg-editor
Full SVG Editor to be installed as desktop app
Stars: ✭ 19 (-53.66%)
Mutual labels:  drawing

@benjeau/react-native-draw

NPM badge CircleCI Status Platform badge

Cross-platform React Native drawing component based on SVG

Installation

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

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

Extras

Supporting components, such as CanvasControls, ColorPicker and BrushProperties components, are available as a separate package, @benjeau/react-native-draw-extras

Usage

All the following examples are also available in the example Expo application

Simple example

Here's the most simple example:

import React from 'react';
import { Canvas } from '@benjeau/react-native-draw';

export default () => <Canvas />;
Demo.-.Simple.Example.mp4

Complex example

Here's a more complex example:

Complex example - Code snippet
import React, { useRef } from 'react';
import { Button } from 'react-native';
import { Canvas, CanvasRef } from '@benjeau/react-native-draw';

export default () => {
  const canvasRef = useRef<CanvasRef>(null);

  const handleUndo = () => {
    canvasRef.current?.undo();
  };

  const handleClear = () => {
    canvasRef.current?.clear();
  };

  return (
    <>
      <Canvas
        ref={canvasRef}
        height={600}
        color="red"
        thickness={20}
        opacity={0.6}
        style={{ backgroundColor: 'black' }}
      />
      <Button title="Undo" onPress={handleUndo} />
      <Button title="Clear" onPress={handleClear} />
    </>
  );
};
Demo.-.Complex.Example.mp4

Example with @BenJeau/react-native-draw-extras

This uses the @benjeau/react-native-draw-extras npm package for the color picker and the bottom buttons/brush preview.

As this package does not depend on @BenJeau/react-native-draw-extras, it is completely optional and you can build your own supporting UI, just like the previous example

Extras example - Code snippet
import React, { useRef, useState } from 'react';
import { Animated, StyleSheet, View } from 'react-native';
import {
  BrushProperties,
  Canvas,
  CanvasControls,
  CanvasRef,
  DEFAULT_COLORS,
  DrawingTool,
} from '@benjeau/react-native-draw';

export default () => {
  const canvasRef = useRef<CanvasRef>(null);

  const [color, setColor] = useState(DEFAULT_COLORS[0][0][0]);
  const [thickness, setThickness] = useState(5);
  const [opacity, setOpacity] = useState(1);
  const [tool, setTool] = useState(DrawingTool.Brush);
  const [visibleBrushProperties, setVisibleBrushProperties] = useState(false);

  const handleUndo = () => {
    canvasRef.current?.undo();
  };

  const handleClear = () => {
    canvasRef.current?.clear();
  };

  const handleToggleEraser = () => {
    setTool((prev) =>
      prev === DrawingTool.Brush ? DrawingTool.Eraser : DrawingTool.Brush
    );
  };

  const [overlayOpacity] = useState(new Animated.Value(0));
  const handleToggleBrushProperties = () => {
    if (!visibleBrushProperties) {
      setVisibleBrushProperties(true);

      Animated.timing(overlayOpacity, {
        toValue: 1,
        duration: 200,
        useNativeDriver: true,
      }).start();
    } else {
      Animated.timing(overlayOpacity, {
        toValue: 0,
        duration: 200,
        useNativeDriver: true,
      }).start(() => {
        setVisibleBrushProperties(false);
      });
    }
  };

  return (
    <>
      <Canvas
        ref={canvasRef}
        height={600}
        color={color}
        thickness={thickness}
        opacity={opacity}
        tool={tool}
        style={{
          borderBottomWidth: StyleSheet.hairlineWidth,
          borderColor: '#ccc',
        }}
      />
      <View>
        <CanvasControls
          onUndo={handleUndo}
          onClear={handleClear}
          onToggleEraser={handleToggleEraser}
          onToggleBrushProperties={handleToggleBrushProperties}
          tool={tool}
          color={color}
          opacity={opacity}
          thickness={thickness}
        />
        {visibleBrushProperties && (
          <BrushProperties
            color={color}
            thickness={thickness}
            opacity={opacity}
            onColorChange={setColor}
            onThicknessChange={setThickness}
            onOpacityChange={setOpacity}
            style={{
              position: 'absolute',
              bottom: 80,
              left: 0,
              right: 0,
              padding: 10,
              backgroundColor: '#f2f2f2',
              borderTopEndRadius: 10,
              borderTopStartRadius: 10,
              borderWidth: StyleSheet.hairlineWidth,
              borderBottomWidth: 0,
              borderTopColor: '#ccc',
              opacity: overlayOpacity,
            }}
          />
        )}
      </View>
    </>
  );
};
Demo.-.Extras.Example.mp4

Props

Canvas

name description type default
color Color of the brush strokes string - (required)
thickness Thickness of the brush strokes number - (required)
opacity Opacity of the brush strokes number - (required)
initialPaths Paths to be already drawn PathType[] []
height Height of the canvas number height of the window - 80
width Width of the canvas number width of the window
style Override the style of the container of the canvas StyleProp -
onPathsChange Callback function when paths change (paths: PathType[]) => any -
simplifyOptions SVG simplification options SimplifyOptions see below
eraserSize Width of eraser (to compensate for path simplification) number 5
tool Initial tool of the canvas brush or eraser brush
combineWithLatestPath Combine current path with the last path if it's the same color, thickness, and opacity boolean false

SimplifyOptions

name description type default
simplifyPaths Enable SVG path simplification on paths, except the one currently being drawn boolean true
simplifyCurrentPath Enable SVG path simplification on the stroke being drawn boolean false
amount Amount of simplification to apply number 10
roundPoints Ignore fractional part in the points. Improves performance boolean true

Ref functions

name description type
undo Undo last brush stroke () => void
clear Removes all brush strokes () => void
getPaths Get brush strokes data () => PathType[]
addPath Append a path to the current drawing paths (path: PathType) => void
getSvg Get SVG path string of the drawing () => string

Troubleshooting

If you cannot draw on the canvas, make sure you have followed the extra steps of react-native-gesture-handler

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].