JSFiddle - React, Tailwind, and code Playground
by jaboc83
HTML
<link rel="stylesheet" href="http://static.thomasjbradley.ca/signature-pad/jquery.signaturepad.css">
<link rel="stylesheet" href="http://static.thomasjbradley.ca/demo.70e91e37372c94ade83b3af37602ed1c.min.css">
<script src="http://static.thomasjbradley.ca/signature-pad/jquery.signaturepad.min.js"></script>
<body>
<form id='sigone' method="post" action="#" class="sigPad">
<p class="drawItDesc" style="display: block;">Draw your signature</p>
<ul class="sigNav" style="display: block;">
<li class="drawIt"><a href="#draw-it" class="current">Draw It</a>
</li>
<li class="clearButton" style="display: list-item;"><a href="#clear">Clear</a>
</li>
</ul>
<div class="sig sigWrapper current" style="display: block;">
<div class="typed" style="display: none;"></div>
<canvas class="pad" width="198" height="55"></canvas>
<input type="hidden" name="output" class="output" value="" />
</div>
</form>
<input id='seeRes' type='button' value='Compress' />
<form id='sigtwo' method="post" action="#" class="sigPad">
<p class="drawItDesc" style="display: block;">Draw your signature</p>
<ul class="sigNav" style="display: block;">
<li class="drawIt"><a href="#draw-it" class="current">Draw It</a>
</li>
<li class="clearButton" style="display: list-item;"><a href="#clear">Clear</a>
</li>
</ul>
<div class="sig sigWrapper current" style="display: block;">
<div class="typed" style="display: none;"></div>
<canvas class="pad" width="198" height="55"></canvas>
<input type="hidden" name="output" class="output" value="" />
</div>
</form>
</body>
JavaScript
(function () {
/** Reinflates a compressed signature string:
resolution = a representation of the resolution in
pixels of the canvas which this signature will be drawn
e.g. {x:800,y:200}
*/
var inflateToJsonSignature = function (deflatedSig, resolution) {
var components = [],
modifier = 1,
compressWithResolution = /^(?:\[(\d+)x(\d+)\])?([\w\W]*)/,
parsedSigString = deflatedSig.match(compressWithResolution),
widthModifier, heightModifier, i,
sigString, deflatedLen,
componentsLength;
// If the previously compressed signature had a resolution,
// attempt to scale to fit the new canvas
if (parsedSigString && resolution) {
widthModifier = resolution.x / (parsedSigString[1] || 1);
heightModifier = resolution.y / (parsedSigString[2] || 1);
modifier = widthModifier < heightModifier ? widthModifier : heightModifier;
deflatedSig = parsedSigString[3];
}
// Get each byte of the deflated signature as a unicode char
// and convert from its hex value to a decimal coordinate value.
// e.g. '}' => 125
// Stuff the result in our output array
deflatedLen = deflatedSig.length;
for (i = 0; i < deflatedSig.length; i++) {
components.push((deflatedSig[i].charCodeAt()).toString());
}
// Rebuild the signature string from the result array above.
// Every 4 chars represent the two sets of x,y coordinates
componentsLength = components.length;
sigString = "[";
for (i = 0; i < componentsLength; i = i + 4) {
sigString += (
'{"lx":' + Math.round(components[i] * modifier) +
',"ly":' + Math.round(components[i + 1] * modifier) +
',"mx":' + Math.round(components[i + 2] * modifier) +
',"my":' + Math.round(components[i + 3] * modifier) + '},');
...