1.0.2 • Published 1 month ago

react-native-seaart-sketch-canvas v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

react-native-sketch-canvas

Forked from terrylinla/react-native-sketch-canvas as package abandoned in 2018.


A React Native component for drawing by touching on both iOS and Android.

Features

  • Support iOS and Android
  • Stroke thickness and color are changeable while drawing.
  • Can undo strokes one by one.
  • Can serialize path data to JSON. So it can sync other devices or someone else and continue to edit.
  • Save drawing to a non-transparent image (png or jpg) or a transparent image (png only)
  • Use vector concept. So sketches won't be cropped in different sizes of canvas.
  • Support translucent colors and eraser.
  • Support drawing on an image (Thanks to diego-caceres-galvan)
  • High performance (See below. Thanks to jeanregisser)
  • Can draw multiple canvases in the same screen.
  • Can draw multiple multiline text on canvas.

Installation


Install from yarn (only support RN >= 0.40)

yarn add react-native-seaart-sketch-canvas

Usage


● Using without UI component (for customizing UI)

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  View,
} from 'react-native';

import { SketchCanvas } from 'react-native-seaart-sketch-canvas';

export default class example extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={{ flex: 1, flexDirection: 'row' }}>
          <SketchCanvas
            style={{ flex: 1 }}
            strokeColor={'red'}
            strokeWidth={7}
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF',
  },
});

AppRegistry.registerComponent('example', () => example);

Properties


PropTypeDescription
styleobjectStyles to be applied on canvas component
strokeColorstringSet the color of stroke, which can be #RRGGBB or #RRGGBBAA. If strokeColor is set to #00000000, it will automatically become an eraser. NOTE: Once an eraser path is sent to Android, Android View will disable hardware acceleration automatically. It might reduce the canvas performance afterward.
strokeWidthnumberThe thickness of stroke
onStrokeStartfunctionAn optional function which accepts 2 arguments x and y. Called when user's finger touches the canvas (starts to draw)
onStrokeChangedfunctionAn optional function which accepts 2 arguments x and y. Called when user's finger moves
onStrokeEndfunctionAn optional function called when user's finger leaves the canvas (end drawing)
onSketchSavedfunctionAn optional function which accepts 2 arguments success and path. If success is true, image is saved successfully and the saved image path might be in second argument. In Android, image path will always be returned. In iOS, image is saved to camera roll or file system, path will be set to null or image location respectively.
onPathsChangefunctionAn optional function which accepts 1 argument pathsCount, which indicates the number of paths. Useful for UI controls. (Thanks to toblerpwn)
userstringAn identifier to identify who draws the path. Useful when undo between two users
touchEnabledboolIf false, disable touching. Default is true.
localSourceImageobjectRequire an object (see below) which consists of filename, directory(optional) and mode(optional). If set, the image will be loaded and display as a background in canvas. (Thanks to diego-caceres-galvan))(Here for details)
permissionDialogTitlestringAndroid Only: Provide a Dialog Title for the Image Saving PermissionDialog. Defaults to empty string if not set
permissionDialogMessagestringAndroid Only: Provide a Dialog Message for the Image Saving PermissionDialog. Defaults to empty string if not set

Methods


MethodDescription
clear()Clear all the paths
undo()Delete the latest path. Can undo multiple times.
addPath(path)Add a path (see below) to canvas.
deletePath(id)Delete a path with its id
save(imageType, transparent, folder, filename, includeImage, cropToImageSize)Save image to camera roll or filesystem. If localSourceImage is set and a background image is loaded successfully, set includeImage to true to include background image and set cropToImageSize to true to crop output image to background image.Android: Save image in imageType format with transparent background (if transparent sets to True) to /sdcard/Pictures/folder/filename (which is Environment.DIRECTORY_PICTURES).iOS: Save image in imageType format with transparent background (if transparent sets to True) to camera roll or file system. If folder and filename are set, image will save to temporary directory/folder/filename (which is NSTemporaryDirectory())
getPaths()Get the paths that drawn on the canvas
getBase64(imageType, transparent, includeImage, cropToImageSize, callback)Get the base64 of image and receive data in callback function, which called with 2 arguments. First one is error (null if no error) and second one is base64 result.

Constants


ConstantDescription
MAIN_BUNDLEAndroid: empty string, '' iOS: equivalent to [NSBundle mainBundle bundlePath]
DOCUMENTAndroid: empty string, '' iOS: equivalent to NSDocumentDirectory
LIBRARYAndroid: empty string, '' iOS: equivalent to NSLibraryDirectory
CACHESAndroid: empty string, '' iOS: equivalent to NSCachesDirectory

Example


The source code includes 3 examples, using build-in UI components, using with only canvas, and sync between two canvases.

Check full example app in the example folder