Skip to content
This repository was archived by the owner on Aug 12, 2021. It is now read-only.

Commit aaf6e02

Browse files
committed
WiP - Had to commit that.
1 parent e0b5358 commit aaf6e02

File tree

8 files changed

+1573
-103
lines changed

8 files changed

+1573
-103
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"parser": "babel-eslint",
44
"rules": {
55
"react/jsx-filename-extension": "off",
6-
"react/no-unused-prop-types": "warn"
6+
"react/no-unused-prop-types": "warn",
7+
"no-use-before-define": "off"
78
}
89
}

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples

README.md

Lines changed: 67 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,102 @@
1-
# React Native Sketch
1+
# 🎨 React Native Sketch
22

3-
[![Version](https://img.shields.io/npm/v/react-native-sketch.svg?style=flat-square)](http://npm.im/react-native-sketch)
4-
[![Downloads](https://img.shields.io/npm/dm/react-native-sketch.svg?style=flat-square)](http://npm.im/react-native-sketch)
5-
[![Gitter](https://img.shields.io/badge/chat-on%20gitter-1dce73.svg?style=flat-square)](https://gitter.im/jgrancher/react-native-sketch)
6-
[![MIT License](https://img.shields.io/npm/l/react-native-sketch.svg?style=flat-square)](http://opensource.org/licenses/MIT)
3+
[![npm](https://img.shields.io/npm/v/react-native-sketch.svg)](https://www.npmjs.com/package/react-native-sketch)
4+
[![Downloads](https://img.shields.io/npm/dm/react-native-sketch.svg)](https://www.npmjs.com/package/react-native-sketch)
5+
[![MIT License](https://img.shields.io/npm/l/react-native-sketch.svg)](http://opensource.org/licenses/MIT)
76

8-
*A React Native component for touch-based drawing.*
7+
A React Native component for touch-based drawing.
98

109
![Screenshots](https://cloud.githubusercontent.com/assets/5517450/15202227/ca865758-183b-11e6-8c4e-41080bc04538.jpg "Disclaimer: This is not my signature ;)")
1110

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:
1320

14-
Install React Native Sketch:
1521
```bash
16-
$ npm install react-native-sketch
22+
npm install react-native-sketch
1723
```
1824

19-
Then, link it to your project:
25+
Link the module to your project:
26+
2027
```bash
21-
$ react-native link
28+
react-native link react-native-sketch
2229
```
2330

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-
2631
## Usage
2732

2833
```javascript
2934
import React from 'react';
30-
import {
31-
Button,
32-
StyleSheet,
33-
View,
34-
} from 'react-native';
35+
import { Alert, Button, View } from 'react-native';
3536
import Sketch from 'react-native-sketch';
3637

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+
});
5843
};
5944

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-
9345
render() {
9446
return (
95-
<View style={styles.container}>
47+
<View style={{ flex: 1 }}>
9648
<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}
11454
/>
55+
<Button onPress={this.save} title="Save" />
11556
</View>
11657
);
11758
}
118-
11959
}
120-
121-
export default Signature;
12260
```
12361

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+
12497
## Notes
12598

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)!
127100
- The module uses this [smooth freehand drawing technique](http://code.tutsplus.com/tutorials/smooth-freehand-drawing-on-ios--mobile-13164) under the hood.
128101

129102
## Contributing

examples/basic.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* React Native Sketch - Basic example
3+
* Uses the bare minimum to make react-native-sketch working
4+
*/
5+
6+
import React, { Component } from 'react';
7+
import Sketch from 'react-native-sketch';
8+
import { AppRegistry, Button, StyleSheet, View } from 'react-native';
9+
10+
export default class Basic extends Component {
11+
clear = () => {
12+
this.sketch.clear();
13+
};
14+
15+
save = () => {
16+
this.sketch.save().then(({ path }) => {
17+
console.log('The image is saved there:', path); // eslint-disable-line no-console
18+
});
19+
};
20+
21+
render() {
22+
return (
23+
<View style={styles.container}>
24+
<Sketch
25+
ref={(sketch) => {
26+
this.sketch = sketch;
27+
}}
28+
/>
29+
<View style={styles.actionsBar}>
30+
<Button color="#EA212E" onPress={this.clear} title="❌ Clear" />
31+
<Button color="#1DBD21" onPress={this.save} title="Save ✅" />
32+
</View>
33+
</View>
34+
);
35+
}
36+
}
37+
38+
const styles = StyleSheet.create({
39+
container: {
40+
backgroundColor: '#F5FCFF',
41+
flex: 1,
42+
paddingTop: 20,
43+
},
44+
actionsBar: {
45+
alignItems: 'stretch',
46+
flexDirection: 'row',
47+
justifyContent: 'space-between',
48+
padding: 20,
49+
},
50+
});
51+
52+
AppRegistry.registerComponent('Basic', () => Basic);

0 commit comments

Comments
 (0)