1 | # Signature Pad [![npm](https://badge.fury.io/js/signature_pad.svg)](https://www.npmjs.com/package/signature_pad) [![tests](https://github.com/szimek/signature_pad/actions/workflows/test.yml/badge.svg)](https://github.com/szimek/signature_pad/actions/workflows/test.yml) [![Code Climate](https://codeclimate.com/github/szimek/signature_pad.png)](https://codeclimate.com/github/szimek/signature_pad) [![](https://data.jsdelivr.com/v1/package/npm/signature_pad/badge?style=rounded)](https://www.jsdelivr.com/package/npm/signature_pad)
|
2 |
|
3 | Signature Pad is a JavaScript library for drawing smooth signatures. It's HTML5 canvas based and uses variable width Bézier curve interpolation based on [Smoother Signatures](https://developer.squareup.com/blog/smoother-signatures/) post by [Square](https://squareup.com).
|
4 | It works in all modern desktop and mobile browsers and doesn't depend on any external libraries.
|
5 |
|
6 | ![Example](https://f.cloud.github.com/assets/9873/268046/9ced3454-8efc-11e2-816e-a9b170a51004.png)
|
7 |
|
8 | ## Demo
|
9 |
|
10 | [Demo](http://szimek.github.io/signature_pad) works in desktop and mobile browsers. You can check out its [source code](https://github.com/szimek/signature_pad/blob/gh-pages/js/app.js) for some tips on how to handle window resize and high DPI screens. You can also find more about the latter in [HTML5 Rocks tutorial](http://www.html5rocks.com/en/tutorials/canvas/hidpi).
|
11 |
|
12 | ### Other demos
|
13 |
|
14 | - Erase feature: <https://jsfiddle.net/UziTech/xa91e4Lp/>
|
15 | - Undo feature: <https://jsfiddle.net/szimek/osenxvjc/>
|
16 |
|
17 | ## Installation
|
18 |
|
19 | You can install the latest release using npm:
|
20 |
|
21 | ```bash
|
22 | npm install --save signature_pad
|
23 | ```
|
24 |
|
25 | or Yarn:
|
26 |
|
27 | ```bash
|
28 | yarn add signature_pad
|
29 | ```
|
30 |
|
31 | You can also add it directly to your page using `<script>` tag:
|
32 |
|
33 | ```html
|
34 | <script src="https://cdn.jsdelivr.net/npm/signature_pad@4.1.7/dist/signature_pad.umd.min.js"></script>
|
35 | ```
|
36 |
|
37 | You can select a different version at [https://www.jsdelivr.com/package/npm/signature_pad](https://www.jsdelivr.com/package/npm/signature_pad).
|
38 |
|
39 | This library is provided as UMD (Universal Module Definition) and ES6 module.
|
40 |
|
41 | ## Usage
|
42 |
|
43 | ### API
|
44 |
|
45 | ```javascript
|
46 | const canvas = document.querySelector("canvas");
|
47 |
|
48 | const signaturePad = new SignaturePad(canvas);
|
49 |
|
50 | // Returns signature image as data URL (see https://mdn.io/todataurl for the list of possible parameters)
|
51 | signaturePad.toDataURL(); // save image as PNG
|
52 | signaturePad.toDataURL("image/jpeg"); // save image as JPEG
|
53 | signaturePad.toDataURL("image/jpeg", 0.5); // save image as JPEG with 0.5 image quality
|
54 | signaturePad.toDataURL("image/svg+xml"); // save image as SVG data url
|
55 |
|
56 | // Return svg string without converting to base64
|
57 | signaturePad.toSVG(); // "<svg...</svg>"
|
58 | signaturePad.toSVG({includeBackgroundColor: true}); // add background color to svg output
|
59 |
|
60 | // Draws signature image from data URL (mostly uses https://mdn.io/drawImage under-the-hood)
|
61 | // NOTE: This method does not populate internal data structure that represents drawn signature. Thus, after using #fromDataURL, #toData won't work properly.
|
62 | signaturePad.fromDataURL("data:image/png;base64,iVBORw0K...");
|
63 |
|
64 | // Draws signature image from data URL and alters it with the given options
|
65 | signaturePad.fromDataURL("data:image/png;base64,iVBORw0K...", { ratio: 1, width: 400, height: 200, xOffset: 100, yOffset: 50 });
|
66 |
|
67 | // Returns signature image as an array of point groups
|
68 | const data = signaturePad.toData();
|
69 |
|
70 | // Draws signature image from an array of point groups
|
71 | signaturePad.fromData(data);
|
72 |
|
73 | // Draws signature image from an array of point groups, without clearing your existing image (clear defaults to true if not provided)
|
74 | signaturePad.fromData(data, { clear: false });
|
75 |
|
76 | // Clears the canvas
|
77 | signaturePad.clear();
|
78 |
|
79 | // Returns true if canvas is empty, otherwise returns false
|
80 | signaturePad.isEmpty();
|
81 |
|
82 | // Unbinds all event handlers
|
83 | signaturePad.off();
|
84 |
|
85 | // Rebinds all event handlers
|
86 | signaturePad.on();
|
87 | ```
|
88 |
|
89 | ### Options
|
90 |
|
91 | <dl>
|
92 | <dt>dotSize</dt>
|
93 | <dd>(float or function) Radius of a single dot. Also the width of the start of a mark.</dd>
|
94 | <dt>minWidth</dt>
|
95 | <dd>(float) Minimum width of a line. Defaults to <code>0.5</code>.</dd>
|
96 | <dt>maxWidth</dt>
|
97 | <dd>(float) Maximum width of a line. Defaults to <code>2.5</code>.</dd>
|
98 | <dt>throttle</dt>
|
99 | <dd>(integer) Draw the next point at most once per every <code>x</code> milliseconds. Set it to <code>0</code> to turn off throttling. Defaults to <code>16</code>.</dd>
|
100 | <dt>minDistance</dt>
|
101 | <dd>(integer) Add the next point only if the previous one is farther than <code>x</code> pixels. Defaults to <code>5</code>.
|
102 | <dt>backgroundColor</dt>
|
103 | <dd>(string) Color used to clear the background. Can be any color format accepted by <code>context.fillStyle</code>. Defaults to <code>"rgba(0,0,0,0)"</code> (transparent black). Use a non-transparent color e.g. <code>"rgb(255,255,255)"</code> (opaque white) if you'd like to save signatures as JPEG images.</dd>
|
104 | <dt>penColor</dt>
|
105 | <dd>(string) Color used to draw the lines. Can be any color format accepted by <code>context.fillStyle</code>. Defaults to <code>"black"</code>.</dd>
|
106 | <dt>velocityFilterWeight</dt>
|
107 | <dd>(float) Weight used to modify new velocity based on the previous velocity. Defaults to <code>0.7</code>.</dd>
|
108 | <dt>canvasContextOptions</dt>
|
109 | <dd>(CanvasRenderingContext2DSettings) part of the Canvas API, provides the 2D rendering context for the drawing surface of a <code>canvas</code> element. It is used for drawing shapes, text, images, and other objects (<a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getContextAttributes">MDN</a>).</dd>
|
110 | </dl>
|
111 |
|
112 | You can set options during initialization:
|
113 |
|
114 | ```javascript
|
115 | const signaturePad = new SignaturePad(canvas, {
|
116 | minWidth: 5,
|
117 | maxWidth: 10,
|
118 | penColor: "rgb(66, 133, 244)"
|
119 | });
|
120 | ```
|
121 |
|
122 | or during runtime:
|
123 |
|
124 | ```javascript
|
125 | const signaturePad = new SignaturePad(canvas);
|
126 | signaturePad.minWidth = 5;
|
127 | signaturePad.maxWidth = 10;
|
128 | signaturePad.penColor = "rgb(66, 133, 244)";
|
129 | ```
|
130 |
|
131 | ### Events
|
132 |
|
133 | <dl>
|
134 | <dt>beginStroke</dt>
|
135 | <dd>Triggered before stroke begins.<br>Can be canceled with <code>event.preventDefault()</code></dd>
|
136 | <dt>endStroke</dt>
|
137 | <dd>Triggered after stroke ends.</dd>
|
138 | <dt>beforeUpdateStroke</dt>
|
139 | <dd>Triggered before stroke update.</dd>
|
140 | <dt>afterUpdateStroke</dt>
|
141 | <dd>Triggered after stroke update.</dd>
|
142 | </dl>
|
143 |
|
144 | You can add listeners to events with [`.addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener):
|
145 |
|
146 | ```javascript
|
147 | const signaturePad = new SignaturePad(canvas);
|
148 | signaturePad.addEventListener("beginStroke", () => {
|
149 | console.log("Signature started");
|
150 | }, { once: true });
|
151 | ```
|
152 |
|
153 | ### Tips and tricks
|
154 |
|
155 | #### Handling high DPI screens
|
156 |
|
157 | To correctly handle canvas on low and high DPI screens one has to take `devicePixelRatio` into account and scale the canvas accordingly. This scaling is also necessary to properly display signatures loaded via `SignaturePad#fromDataURL`. Here's an example how it can be done:
|
158 |
|
159 | ```javascript
|
160 | function resizeCanvas() {
|
161 | const ratio = Math.max(window.devicePixelRatio || 1, 1);
|
162 | canvas.width = canvas.offsetWidth * ratio;
|
163 | canvas.height = canvas.offsetHeight * ratio;
|
164 | canvas.getContext("2d").scale(ratio, ratio);
|
165 | signaturePad.clear(); // otherwise isEmpty() might return incorrect value
|
166 | }
|
167 |
|
168 | window.addEventListener("resize", resizeCanvas);
|
169 | resizeCanvas();
|
170 | ```
|
171 |
|
172 | Instead of `resize` event you can listen to screen orientation change, if you're using this library only on mobile devices. You can also throttle the `resize` event - you can find some examples on [this MDN page](https://developer.mozilla.org/en-US/docs/Web/Events/resize).
|
173 |
|
174 | #### Handling canvas resize
|
175 |
|
176 | When you modify width or height of a canvas, it will be automatically cleared by the browser. SignaturePad doesn't know about it by itself, so you can call `signaturePad.fromData(signaturePad.toData())` to reset the drawing, or `signaturePad.clear()` to make sure that `signaturePad.isEmpty()` returns correct value in this case.
|
177 |
|
178 | This clearing of the canvas by the browser can be annoying, especially on mobile devices e.g. when screen orientation is changed. There are a few workarounds though, e.g. you can [lock screen orientation](https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation), or read an image from the canvas before resizing it and write the image back after.
|
179 |
|
180 | #### Handling data URI encoded images on the server side
|
181 |
|
182 | If you are not familiar with data URI scheme, you can read more about it on [Wikipedia](http://en.wikipedia.org/wiki/Data_URI_scheme).
|
183 |
|
184 | There are 2 ways you can handle data URI encoded images.
|
185 |
|
186 | You could simply store it in your database as a string and display it in HTML like this:
|
187 |
|
188 | ```html
|
189 | <img src="data:image/png;base64,iVBORw0K..." />
|
190 | ```
|
191 |
|
192 | but this way has many disadvantages - it's not easy to get image dimensions, you can't manipulate it e.g. to create a thumbnail and it also [has some performance issues on mobile devices](http://www.mobify.com/blog/data-uris-are-slow-on-mobile/).
|
193 |
|
194 | Thus, more common way is to decode it and store as a file. Here's an example in Ruby:
|
195 |
|
196 | ```ruby
|
197 | require "base64"
|
198 |
|
199 | data_uri = "data:image/png;base64,iVBORw0K..."
|
200 | encoded_image = data_uri.split(",")[1]
|
201 | decoded_image = Base64.decode64(encoded_image)
|
202 | File.open("signature.png", "wb") { |f| f.write(decoded_image) }
|
203 | ```
|
204 |
|
205 | Here's an example in PHP:
|
206 |
|
207 | ```php
|
208 | $data_uri = "data:image/png;base64,iVBORw0K...";
|
209 | $encoded_image = explode(",", $data_uri)[1];
|
210 | $decoded_image = base64_decode($encoded_image);
|
211 | file_put_contents("signature.png", $decoded_image);
|
212 | ```
|
213 |
|
214 | Here's an example in C# for ASP.NET:
|
215 |
|
216 | ```csharp
|
217 | var dataUri = "data:image/png;base64,iVBORw0K...";
|
218 | var encodedImage = dataUri.Split(',')[1];
|
219 | var decodedImage = Convert.FromBase64String(encodedImage);
|
220 | System.IO.File.WriteAllBytes("signature.png", decodedImage);
|
221 | ```
|
222 |
|
223 | #### Removing empty space around a signature
|
224 |
|
225 | If you'd like to remove (trim) empty space around a signature, you can do it on the server side or the client side. On the server side you can use e.g. ImageMagic and its `trim` option: `convert -trim input.jpg output.jpg`. If you don't have access to the server, or just want to trim the image before submitting it to the server, you can do it on the client side as well. There are a few examples how to do it, e.g. [here](https://github.com/szimek/signature_pad/issues/49#issue-29108215) or [here](https://github.com/szimek/signature_pad/issues/49#issuecomment-260976909) and there's also a tiny library [trim-canvas](https://github.com/agilgur5/trim-canvas) that provides this functionality.
|
226 |
|
227 | #### Drawing over an image
|
228 |
|
229 | Demo: <https://jsfiddle.net/szimek/d6a78gwq/>
|
230 |
|
231 | ## License
|
232 |
|
233 | Released under the [MIT License](http://www.opensource.org/licenses/MIT).
|