|
1 |
| -# React Native Sketch |
| 1 | +# 🎨 React Native Sketch |
2 | 2 |
|
3 |
| -[](http://npm.im/react-native-sketch) |
4 |
| -[](http://npm.im/react-native-sketch) |
5 |
| -[](https://gitter.im/jgrancher/react-native-sketch) |
6 |
| -[](http://opensource.org/licenses/MIT) |
| 3 | +[](https://www.npmjs.com/package/react-native-sketch) |
| 4 | +[](https://www.npmjs.com/package/react-native-sketch) |
| 5 | +[](http://opensource.org/licenses/MIT) |
7 | 6 |
|
8 |
| -*A React Native component for touch-based drawing.* |
| 7 | +A React Native component for touch-based drawing. |
9 | 8 |
|
10 | 9 | ")
|
11 | 10 |
|
12 |
| -## Getting started |
| 11 | +## Features |
| 12 | + |
| 13 | +- 👆 Draw with your finger, and export an image from it. |
| 14 | +- 🖍 Change the stroke color and thickness on the fly. |
| 15 | +- 👻 Generate a transparent image (or not). |
| 16 | + |
| 17 | +## Setup |
| 18 | + |
| 19 | +Install the module from npm: |
13 | 20 |
|
14 |
| -Install React Native Sketch: |
15 | 21 | ```bash
|
16 |
| -$ npm install react-native-sketch |
| 22 | +npm install react-native-sketch |
17 | 23 | ```
|
18 | 24 |
|
19 |
| -Then, link it to your project: |
| 25 | +Link the module to your project: |
| 26 | + |
20 | 27 | ```bash
|
21 |
| -$ react-native link |
| 28 | +react-native link react-native-sketch |
22 | 29 | ```
|
23 | 30 |
|
24 |
| -**Note**: If you are using an older version of React Native than `0.31`, you will need to install [rnpm](https://github.com/rnpm/rnpm) to link the module. |
25 |
| - |
26 | 31 | ## Usage
|
27 | 32 |
|
28 | 33 | ```javascript
|
29 | 34 | import React from 'react';
|
30 |
| -import { |
31 |
| - Button, |
32 |
| - StyleSheet, |
33 |
| - View, |
34 |
| -} from 'react-native'; |
| 35 | +import { Alert, Button, View } from 'react-native'; |
35 | 36 | import Sketch from 'react-native-sketch';
|
36 | 37 |
|
37 |
| -const styles = StyleSheet.create({ |
38 |
| - container: { |
39 |
| - flex: 1, |
40 |
| - }, |
41 |
| - sketch: { |
42 |
| - height: 250, // Height needed; Default: 200px |
43 |
| - }, |
44 |
| -}); |
45 |
| - |
46 |
| -class Signature extends React.Component { |
47 |
| - |
48 |
| - constructor(props) { |
49 |
| - super(props); |
50 |
| - this.clear = this.clear.bind(this); |
51 |
| - this.onReset = this.onReset.bind(this); |
52 |
| - this.onSave = this.onSave.bind(this); |
53 |
| - this.onUpdate = this.onUpdate.bind(this); |
54 |
| - } |
55 |
| - |
56 |
| - state = { |
57 |
| - encodedSignature: null, |
| 38 | +export default class MyPaint extends React.Component { |
| 39 | + save = () => { |
| 40 | + this.sketch.save().then(({ path }) => { |
| 41 | + Alert.alert('Image saved!', path); |
| 42 | + }); |
58 | 43 | };
|
59 | 44 |
|
60 |
| - /** |
61 |
| - * Clear / reset the drawing |
62 |
| - */ |
63 |
| - clear() { |
64 |
| - this.sketch.clear(); |
65 |
| - } |
66 |
| - |
67 |
| - /** |
68 |
| - * Do extra things after the sketch reset |
69 |
| - */ |
70 |
| - onReset() { |
71 |
| - console.log('The drawing has been cleared!'); |
72 |
| - } |
73 |
| - |
74 |
| - /** |
75 |
| - * The Sketch component provides a 'saveImage' function (promise), |
76 |
| - * so that you can save the drawing in the device and get an object |
77 |
| - * once the promise is resolved, containing the path of the image. |
78 |
| - */ |
79 |
| - onSave() { |
80 |
| - this.sketch.saveImage(this.state.encodedSignature) |
81 |
| - .then((data) => console.log(data)) |
82 |
| - .catch((error) => console.log(error)); |
83 |
| - } |
84 |
| - |
85 |
| - /** |
86 |
| - * On every update (touch up from the drawing), |
87 |
| - * you'll receive the base64 representation of the drawing as a callback. |
88 |
| - */ |
89 |
| - onUpdate(base64Image) { |
90 |
| - this.setState({ encodedSignature: base64Image }); |
91 |
| - } |
92 |
| - |
93 | 45 | render() {
|
94 | 46 | return (
|
95 |
| - <View style={styles.container}> |
| 47 | + <View style={{ flex: 1 }}> |
96 | 48 | <Sketch
|
97 |
| - fillColor="transparent" |
98 |
| - strokeColor="#111111" |
99 |
| - strokeThickness={2} |
100 |
| - imageType="png" |
101 |
| - onReset={this.onReset} |
102 |
| - onUpdate={this.onUpdate} |
103 |
| - ref={(sketch) => { this.sketch = sketch; }} |
104 |
| - style={styles.sketch} |
105 |
| - /> |
106 |
| - <Button |
107 |
| - onPress={this.clear} |
108 |
| - title="Clear drawing" |
109 |
| - /> |
110 |
| - <Button |
111 |
| - disabled={!this.state.encodedSignature} |
112 |
| - onPress={this.onSave} |
113 |
| - title="Save drawing" |
| 49 | + ref={sketch => { |
| 50 | + this.sketch = sketch; |
| 51 | + }} |
| 52 | + strokeColor="#ff69b4" |
| 53 | + strokeThickness={3} |
114 | 54 | />
|
| 55 | + <Button onPress={this.save} title="Save" /> |
115 | 56 | </View>
|
116 | 57 | );
|
117 | 58 | }
|
118 |
| - |
119 | 59 | }
|
120 |
| - |
121 |
| -export default Signature; |
122 | 60 | ```
|
123 | 61 |
|
| 62 | +## API |
| 63 | + |
| 64 | +Here are the `props` of the the component: |
| 65 | + |
| 66 | +| Name | Type | Default value | Comment | |
| 67 | +| ---- | ---- | ------------- | ---- | |
| 68 | +| `fillColor` | `String` | `null` | The color of the sketch background. Default to null to keep it transparent! *Note: This is different from the `style.backgroundColor` property, as the former will be seen in your exported drawing image, whereas the latter is only used to style the view.* | |
| 69 | +| `imageType` | `String` | `png` | The type of image to export. Can be `png` or `jpg`. Default to `png` to get transparency out of the box. | |
| 70 | +| `onChange` | `Function` | `() => {}` | Callback function triggered after every change on the drawing. The function takes one argument: the actual base64 representation of your drawing.| |
| 71 | +| `onClear` | `Function` | `() => {}` | Callback function triggered after a `clear` has been triggered. | |
| 72 | +| `strokeColor` | `String` | `#000000` | The color of the stroke with which you want to draw. | |
| 73 | +| `strokeThickness` | `Number` | `1` | The stroke thickness in pixels. | |
| 74 | +| `style` | `` | `null` | Some `View` styles if you need. | |
| 75 | + |
| 76 | +The component also has some instance methods: |
| 77 | + |
| 78 | +| Name | Return type | Comment | |
| 79 | +| ---- | ----------- | ------- | |
| 80 | +| `clear()` | `Promise` | Clear the drawing. This method is a Promise mostly to be consistent with the `save()` one, but you could simply type: `this.sketch.clear();` | |
| 81 | +| `save()` | `Promise` | Save the drawing to an image, using the defined props as settings (`imageType`, `fillColor`, etc...). The Promise resolves with an object containing the `path` property of that image. Ex: `this.sketch.save().then(image => console.log(image.path));` | |
| 82 | + |
| 83 | +## Examples |
| 84 | + |
| 85 | +The project contains a folder `examples` that contains few demos of how to use `react-native-sketch`. Just copy and paste the code to your React Native application. |
| 86 | + |
| 87 | +- [`ios-digital-touch.js`](https://github.com/jgrancher/react-native-sketch/tree/master/examples/ios-digital-touch.js): uses few colors buttons to reproduce the behaviour of iOS10 Message Digital Touch. |
| 88 | +- [] |
| 89 | + |
| 90 | +Feel free to play with them! |
| 91 | + |
| 92 | +## Known issues |
| 93 | + |
| 94 | +- Rotating the screen gets to a weird behavior of the sketch view: [#23](https://github.com/jgrancher/react-native-sketch/issues/23) |
| 95 | +- Taping the screen without dragging your finger causes an update but does not display any point: [#25](https://github.com/jgrancher/react-native-sketch/issues/25) |
| 96 | + |
124 | 97 | ## Notes
|
125 | 98 |
|
126 |
| -- The module is available *only on iOS* (for now), as I don't know Android development... But if you think you can help on that matter, please feel free to contact me! |
| 99 | +- The module is available *only on iOS* (for now), as I don't know Android development... But if you think you can help on that matter, please feel free to [contact me](https://twitter.com/jgrancher)! |
127 | 100 | - The module uses this [smooth freehand drawing technique](http://code.tutsplus.com/tutorials/smooth-freehand-drawing-on-ios--mobile-13164) under the hood.
|
128 | 101 |
|
129 | 102 | ## Contributing
|
|
0 commit comments