Coder Social home page Coder Social logo

aprihodko / react-native-sketch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jgrancher/react-native-sketch

5.0 2.0 2.0 78 KB

A react-native <Sketch /> component for touch-based drawing.

Home Page: https://www.npmjs.com/package/react-native-sketch

License: MIT License

Objective-C 81.80% JavaScript 18.20%

react-native-sketch's Introduction

react-native-sketch

Version Downloads Gitter MIT License

A react-native component for touch-based drawing with an ability to draw over the images.

Screenshots

Getting started

Use react-native to install and link this component to your project:

$ react-native install react-native-sketch

Usage

Solid background

import React, { Component } from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import Sketch from 'react-native-sketch';

const styles = StyleSheet.create({
  container: {
    padding: 20,    
  },
  instructions: {
    fontSize: 16,
    marginBottom: 20,
    textAlign: 'center',
  },
  sketch: {
    height: 250, // Height needed; Default: 200px
    marginBottom: 20,
    backgroundColor: '#ffffff', // Solid White background. Replacement for the fillColor property
  },
  button: {
    alignItems: 'center',
    backgroundColor: '#111111',
    padding: 20,
  },
  buttonText: {
    color: '#ffffff',
    fontSize: 16,
  },
});

class Signature extends Component {

  constructor(props) {
    super(props);
    this.onReset = this.onReset.bind(this);
    this.onSave = this.onSave.bind(this);
    this.onUpdate = this.onUpdate.bind(this);
  }

  state = {
    encodedSignature: null,
  };

  /**
   * Do extra things after the sketch reset
   */
  onReset() {
    console.log('The drawing has been cleared!');
  }

  /**
   * The Sketch component provides a 'saveImage' function (promise),
   * so that you can save the drawing in the device and get an object
   * once the promise is resolved, containing the path of the image.
   */
  onSave() {
    this.sketch.saveImage(this.state.encodedSignature)
      .then((data) => console.log(data))
      .catch((error) => console.log(error));
  }

  /**
   * On every update (touch up from the drawing),
   * you'll receive the base64 representation of the drawing as a callback.
   */
  onUpdate(base64Image) {
    this.setState({ encodedSignature: base64Image });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.instructions}>
          Use your finger on the screen to sign.
        </Text>
        <Sketch
          strokeColor="#111111"
          strokeThickness={2}
          onReset={this.onReset}
          onUpdate={this.onUpdate}
          ref={(sketch) => { this.sketch = sketch; }}
          style={styles.sketch}
        />
        <TouchableOpacity
          disabled={!this.state.encodedSignature}
          style={styles.button}
          onPress={this.onSave}
        >
          <Text style={styles.buttonText}>Save</Text>
        </TouchableOpacity>
      </View>
    );
  }

}

export default Signature;

Render over the image (transparent background)

  1. Set the transparent backgroundColor style property.
const styles = StyleSheet.create({
  sketch: {
    height: 250, 
    backgroundColor: 'rgba(255, 255, 255, 0)', // Transparent background
  }
});
  1. Wrap the Sketch component into the Image component
  <Image
    style={ styles.imageStyle }
    source={ {uri: imageUri} }
  >
    <Sketch
      strokeColor="#111111"
      strokeThickness={2}
      onReset={this.onReset}
      onUpdate={this.onUpdate}
      ref={(sketch) => { this.sketch = sketch; }}
      style={styles.sketch}
    />
  </Image>

Roadmap

  • Define a way for an external component to clear the current drawing.
  • Improve the documentation.
  • Make some tests.
  • Android support (help wanted ¯\(ツ)/¯).

Notes

This component uses this smooth freehand drawing technique under the hood.

Contributing

Feel free to contribute by sending a pull request or creating an issue.

License

MIT

react-native-sketch's People

Contributors

aprihodko avatar gitter-badger avatar jgrancher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nperez0111 kotay

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.