Skip to content

Commit ef0483e

Browse files
committed
v0.1.0 - only use a canvas element, with no styling needed
- No other elements are rendered by this component other than a canvas (as it should be for a library) - Styling is not _necessary_ but can be added to surrounding elements or by passing a `canvasProps` object prop, which can define `style` or `className` and is directly passed to underlying canvas element - Use PropTypes and ES6 function binding instead of calling .bind() - clear() no longer takes an event as a prop as it's not attached to a button anymore - Also use ev var instead of event to prevent confusion with event objects - remove toDataURL() and create getCanvas() instead - create getTrimmedCanvas() to return a trimmed copy of the canvas, removing any whitespace of the canvas - Align closer to standard style where code was changed - 2 space, no semicolon. - Use ES6 let + import as well - Move example code to its own directory - Configure webpack-dev-server to property use this directory - Remove lib and don't push compiled artifacts to the repo - Users can use npmcdn if they really need to - gitignore the build/ directory - Add better documentation for all Props + API methods - Bump version to 0.1.0 + add .npmignore
1 parent c5e141a commit ef0483e

File tree

14 files changed

+363
-401
lines changed

14 files changed

+363
-401
lines changed

.gitignore

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2-
#
3-
# If you find yourself ignoring temporary files generated by your text editor
4-
# or operating system, you probably want to add a global ignore instead:
5-
# git config --global core.excludesfile '~/.gitignore_global'
1+
build/
62

7-
/tmp
8-
/build
3+
npm-debug.log*
4+
node_modules/
5+
6+
*.tgz
97

108
.DS_Store
11-
npm-debug.log
12-
/node_modules/*
13-
react-signature-pad-*.tgz
9+
*~

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
example/
2+
webpack.*
3+
4+
npm-debug.log*
5+
node_modules/
6+
7+
*.tgz
8+
9+
.DS_Store
10+
*~

README.md

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,89 @@
1-
[![npm package](https://img.shields.io/badge/npm-0.0.5-orange.svg?style=flat-square)](https://www.npmjs.com/package/react-signature-pad)
1+
# React Signature Canvas
22

3-
# React Signature Pad
4-
A [signature pad](https://github.com/szimek/signature_pad) implementation for react.
3+
[![NPM](https://nodei.co/npm/react-signature-canvas.png)](https://npmjs.org/package/react-signature-canvas)
54

6-
# Basic Usage
5+
A [signature pad](https://github.com/szimek/signature_pad) implementation in
6+
React.
77

8-
```javascript
9-
var React = require('react');
10-
var SignaturePad = require('react-signature-pad');
8+
This is an _unopinionated_ fork of
9+
[react-signature-pad](https://github.com/blackjk3/react-signature-pad)
10+
that does not impose any styling or wrap any other unwanted elements around
11+
your canvas -- it's just a wrapper around a single canvas element! Hence the
12+
naming difference.
1113

12-
React.render(
13-
<SignaturePad clearButton="true" />,
14-
document.body
15-
)
16-
```
14+
This fork also allows you to directly pass [props](#props) to the underlying
15+
canvas element, has small [API differences](#api), and
16+
[documents the props](#props) you can pass to it.
1717

18-
# Methods
18+
## Installation
1919

20-
```javascript
21-
<SignaturePad clearButton="true" ref="mySignature" />
22-
...
20+
`npm i -S react-signature-canvas`
21+
22+
## Usage
2323

24-
var signature = this.refs.mySignature;
24+
```javascript
25+
import React from 'react'
26+
import ReactDOM from 'react-dom'
27+
import SignatureCanvas from 'react-signature-canvas'
28+
29+
ReactDOM.render(
30+
<SignatureCanvas penColor='green'
31+
canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />,
32+
document.getElementById('react-container')
33+
)
34+
```
2535

26-
// Methods
36+
### Props
2737

28-
// ===============================================
29-
// isEmpty() - returns boolean
30-
// ===============================================
38+
The props of SignatureCanvas mainly control the properties of the pen stroke
39+
used in drawing. All props are **optional**.
3140

32-
signature.isEmpty();
41+
- `velocityFilterWeight` : `number`, default: `0.7`
42+
- `minWidth` : `number`, default: `0.5`
43+
- `maxWidth` : `number`, default: `2.5`
44+
- `dotSize` : `number` or `function`,
45+
default: `() => (this.props.minWidth + this.props.maxWidth) / 2`
46+
- `penColor` : `string`, default: `'black'`
3347

34-
// ===============================================
35-
// clear() - clears canvas
36-
// ===============================================
48+
There are also two callbacks that will be called when a stroke ends and one
49+
begins, respectively.
3750

38-
signature.clear();
51+
- `onEnd` : `function`
52+
- `onBegin` : `function`
3953

40-
// ===============================================
41-
// toDataURL() - retrieves image as a data url
42-
// ===============================================
54+
Two additional props are used to control the canvas element. `canvasProps` are
55+
directly passed to the underlying `<canvas />` element and `backgroundColor` is
56+
used in the [API's](#api) `clear` convenience method (which itself is called
57+
internally during resizes)
4358

44-
signature.toDataURL();
59+
- `canvasProps`: `object`
60+
- `backgroundColor` : `string`, default: `'rgba(0,0,0,0)'`
4561

46-
// ===============================================
47-
// fromDataURL() - writes a base64 image to canvas
48-
// ===============================================
62+
### API
4963

50-
signature.fromDataURL(base64String);
64+
All API methods require a ref to the SignatureCanvas in order to use and are
65+
instance methods of the ref.
5166

67+
```javascript
68+
<SignatureCanvas ref={(ref) => { this.sigCanvas = ref }} />
5269
```
5370

54-
# CSS
55-
In order to make the signature pad work correctly you will need the css as well. All the relevant styles are in [this file](style.css).
71+
- `isEmpty()` : `boolean`, self-explanatory
72+
- `clear()` : `void`, clears the canvas using the `backgroundColor` prop
73+
- `fromDataURL(base64String)` : `void`, writes a base64 image to canvas
74+
- `getCanvas()`: `canvas`, returns the underlying canvas ref. Allows you to
75+
modify the canvas however you want or call methods such as `toDataURL()`
76+
- `getTrimmedCanvas()`: `canvas`, creates a copy of the canvas and returns a
77+
trimmed version of it, with all whitespace removed.
78+
79+
## Example
5680

57-
# Example
5881
```bash
5982
$ npm start
6083
```
61-
Then navigate to http://localhost:8080/ in your browser and you should be able to see the signature pad in action.
84+
85+
Navigate to [http://localhost:8080/](http://localhost:8080/) in your browser
86+
and you should be able to see the signature canvas in action.
87+
88+
The source code for this example is found in the [`example/`](example/)
89+
directory.

app.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

example/app.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { Component } from 'react'
2+
import ReactDOM from 'react-dom'
3+
4+
import SignaturePad from '../src/index.js'
5+
6+
import styles from './styles.cssm'
7+
8+
class App extends Component {
9+
state = {trimmedDataURL: null}
10+
sigPad = {}
11+
clear = () => {
12+
this.sigPad.clear()
13+
}
14+
trim = () => {
15+
this.setState({trimmedDataURL: this.sigPad.getTrimmedCanvas()
16+
.toDataURL('image/png')})
17+
}
18+
render () {
19+
let {trimmedDataURL} = this.state
20+
return <div className={styles.container}>
21+
<div className={styles.sigContainer}>
22+
<SignaturePad canvasProps={{className: styles.sigPad}}
23+
ref={(ref) => { this.sigPad = ref }} />
24+
</div>
25+
<div>
26+
<button className={styles.buttons} onClick={this.clear}>
27+
Clear
28+
</button>
29+
<button className={styles.buttons} onClick={this.trim}>
30+
Trim
31+
</button>
32+
</div>
33+
{trimmedDataURL
34+
? <img className={styles.sigImage}
35+
style={{backgroundImage: 'url(' + trimmedDataURL + ')'}} />
36+
: null}
37+
</div>
38+
}
39+
}
40+
41+
ReactDOM.render(<App />, document.getElementById('container'))

example/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Signature Pad Example</title>
4+
</head>
5+
<body>
6+
<div id='container'></div>
7+
<script src='build/bundle.js'></script>
8+
</body>
9+
</html>

example/styles.cssm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
body {
2+
background-color: gray;
3+
-moz-user-select: none;
4+
-webkit-user-select: none;
5+
-ms-user-select: none;
6+
}
7+
8+
.container {
9+
width: 100%;
10+
height: 100%;
11+
top: 10%;
12+
left: 10%;
13+
}
14+
15+
.sigContainer {
16+
width: 80%;
17+
height: 80%;
18+
margin: 0 auto;
19+
background-color: #fff;
20+
}
21+
22+
.sigPad {
23+
width: 100%;
24+
height: 100%;
25+
}
26+
27+
.buttons {
28+
width: 100%;
29+
height: 30px;
30+
}
31+
32+
.sigImage {
33+
background-size: 200px 50px;
34+
width: 200px;
35+
height: 50px;
36+
}

index.html

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/app.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
11
{
2-
"name": "react-signature-pad",
3-
"version": "0.0.5",
4-
"description": "A signature pad implementation for react.",
5-
"main": "lib/app.js",
2+
"name": "react-signature-canvas",
3+
"version": "0.1.0",
4+
"description": "A signature pad implementation in React",
5+
"main": "build/index.js",
66
"scripts": {
77
"test": "webpack && npm pack",
8-
"start": "webpack-dev-server --hot --inline",
8+
"start": "webpack-dev-server -d --inline --hot",
99
"dist": "webpack -p --config webpack.production.config.js"
1010
},
1111
"keywords": [
1212
"react",
1313
"react-component",
14+
"component",
1415
"signature",
15-
"form",
16-
"ui"
16+
"sign",
17+
"canvas",
18+
"draw",
19+
"pad",
20+
"signature-pad",
21+
"react-signature-pad"
1722
],
1823
"repository": {
1924
"type": "git",
20-
"url": "https://github.com/blackjk3/react-signature-pad.git"
25+
"url": "https://github.com/agilgur5/react-signature-canvas.git"
2126
},
22-
"author": "Jason Kadrmas",
27+
"author": "Anton Gilgur",
2328
"license": "MIT",
2429
"devDependencies": {
25-
"babel-core": "^5.6.15",
26-
"babel-loader": "^5.3.1",
27-
"node-libs-browser": "^0.5.2",
28-
"react": "^0.14.3",
29-
"react-dom": "^0.14.3",
30+
"babel-core": "^6.0.14",
31+
"babel-loader": "^6.0.0",
32+
"babel-preset-es2015": "^6.14.0",
33+
"babel-preset-react": "^6.11.1",
34+
"babel-preset-stage-2": "^6.13.0",
35+
"css-loader": "^0.24.0",
36+
"react": "^15.3.0",
37+
"react-dom": "^15.3.0",
3038
"react-hot-loader": "^1.2.7",
31-
"webpack": "^1.10.1",
39+
"style-loader": "^0.13.1",
40+
"webpack": "^1.12.2",
3241
"webpack-dev-server": "^1.10.1"
3342
},
3443
"peerDependencies": {
3544
"react": "^0.14.0 || ^15.0.0",
3645
"react-dom": "^0.14.0 || ^15.0.0"
46+
},
47+
"dependencies": {
48+
"copy-canvas": "^1.0.0",
49+
"trim-canvas": "^0.1.0"
3750
}
3851
}

0 commit comments

Comments
 (0)