forked from thread-pond/signature-pad
-
Notifications
You must be signed in to change notification settings - Fork 28
/
jquery.signaturepad.js
executable file
·1135 lines (981 loc) · 35.5 KB
/
jquery.signaturepad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Usage for accepting signatures:
* $('.sigPad').signaturePad()
*
* Usage for displaying previous signatures:
* $('.sigPad').signaturePad({displayOnly:true}).regenerate(sig)
* or
* var api = $('.sigPad').signaturePad({displayOnly:true})
* api.regenerate(sig)
*/
(function ($) {
function SignaturePad (selector, options) {
/**
* Reference to the object for use in public methods
*
* @private
*
* @type {Object}
*/
var self = this
/**
* Holds the merged default settings and user passed settings
*
* @private
*
* @type {Object}
*/
, settings = $.extend({}, $.fn.signaturePad.defaults, options)
/**
* The current context, as passed by jQuery, of selected items
*
* @private
*
* @type {Object}
*/
, context = $(selector)
/**
* jQuery reference to the canvas element inside the signature pad
*
* @private
*
* @type {Object}
*/
, canvas = $(settings.canvas, context)
/**
* Dom reference to the canvas element inside the signature pad
*
* @private
*
* @type {Object}
*/
, element = canvas.get(0)
/**
* The drawing context for the signature canvas
*
* @private
*
* @type {Object}
*/
, canvasContext = null
/**
* Holds the previous point of drawing
* Disallows drawing over the same location to make lines more delicate
*
* @private
*
* @type {Object}
*/
, previous = {'x': null, 'y': null}
/**
* An array holding all the points and lines to generate the signature
* Each item is an object like:
* {
* mx: moveTo x coordinate
* my: moveTo y coordinate
* lx: lineTo x coordinate
* lx: lineTo y coordinate
* }
*
* @private
*
* @type {Array}
*/
, output = []
/**
* Stores a timeout for when the mouse leaves the canvas
* If the mouse has left the canvas for a specific amount of time
* Stops drawing on the canvas
*
* @private
*
* @type {Object}
*/
, mouseLeaveTimeout = false
/**
* Whether the mouse button is currently pressed down or not
*
* @private
*
* @type {Boolean}
*/
, mouseButtonDown = false
/**
* Whether the browser is a touch event browser or not
*
* @private
*
* @type {Boolean}
*/
, touchable = false
/**
* Whether events have already been bound to the canvas or not
*
* @private
*
* @type {Boolean}
*/
, eventsBound = false
/**
* Remembers the default font-size when typing, and will allow it to be scaled for bigger/smaller names
*
* @private
*
* @type {Number}
*/
, typeItDefaultFontSize = 30
/**
* Remembers the current font-size when typing
*
* @private
*
* @type {Number}
*/
, typeItCurrentFontSize = typeItDefaultFontSize
/**
* Remembers how many characters are in the name field, to help with the scaling feature
*
* @private
*
* @type {Number}
*/
, typeItNumChars = 0
, strokePoints = []
/**
* Clears the mouseLeaveTimeout
* Resets some other variables that may be active
*
* @private
*/
function clearMouseLeaveTimeout () {
clearTimeout(mouseLeaveTimeout)
mouseLeaveTimeout = false
mouseButtonDown = false
}
/**
* Draws a line on canvas using the mouse position
* Checks previous position to not draw over top of previous drawing
* (makes the line really thick and poorly anti-aliased)
*
* @private
*
* @param {Object} e The event object
* @param {Number} newYOffset A pixel value for drawing the newY, used for drawing a single dot on click
*/
function drawLine (e, newYOffset) {
var offset, newX, newY
e.preventDefault()
offset = $(e.target).offset()
clearTimeout(mouseLeaveTimeout)
mouseLeaveTimeout = false
if (typeof e.changedTouches !== 'undefined') {
newX = Math.floor(e.changedTouches[0].pageX - offset.left)
newY = Math.floor(e.changedTouches[0].pageY - offset.top)
} else {
newX = Math.floor(e.pageX - offset.left)
newY = Math.floor(e.pageY - offset.top)
}
if (previous.x === newX && previous.y === newY)
return true
if (previous.x === null)
previous.x = newX
if (previous.y === null)
previous.y = newY
if (newYOffset)
newY += newYOffset
// We always draw the linear bits
canvasContext.beginPath()
canvasContext.moveTo(previous.x, previous.y)
canvasContext.lineTo(newX, newY)
canvasContext.lineCap = settings.penCap
canvasContext.stroke()
canvasContext.closePath()
// But if we want beziers, we erase the straight lines
// As soon as we have enough points to draw a bezier over it
if (settings.drawBezierCurves === true) {
strokePoints.push({'lx': newX, 'ly': newY,
'mx': previous.x, 'my': previous.y});
// 4 points to define a bezier * the number of points we need for skipping
var maxCacheLength = 4 * settings.bezierSkip;
if (strokePoints.length >= maxCacheLength) {
// "Erase" the linear part that we drew and replace it with the bezier
var retrace = output.slice(output.length - maxCacheLength + 2, output.length);
canvasContext.strokeStyle = settings.bgColour;
for (i in retrace) {
var point = retrace[i];
canvasContext.beginPath()
canvasContext.moveTo(point.mx, point.my)
canvasContext.lineTo(point.lx, point.ly)
canvasContext.lineCap = settings.penCap
canvasContext.stroke()
canvasContext.closePath()
}
canvasContext.strokeStyle = settings.penColour
strokePath(strokePoints, canvasContext);
strokePoints = strokePoints.slice(maxCacheLength - 1, maxCacheLength);
}
}
output.push({
'lx' : newX
, 'ly' : newY
, 'mx' : previous.x
, 'my' : previous.y
})
previous.x = newX
previous.y = newY
if (settings.onDraw && typeof settings.onDraw === 'function')
settings.onDraw.apply(self)
}
/**
* Callback registered to mouse/touch events of the canvas
* Stops the drawing abilities
*
* @private
*
* @param {Object} e The event object
*/
function stopDrawing (e) {
if (!!e && !(e.type === "touchend" || e.type == "touchcancel")) {
drawLine(e, 1)
} else {
if (touchable) {
canvas.each(function () {
this.removeEventListener('touchmove', drawLine)
})
} else {
canvas.unbind('mousemove.signaturepad')
}
if (output.length > 0) {
if (settings.onDrawEnd && typeof settings.onDrawEnd === 'function')
settings.onDrawEnd.apply(self)
strokePoints = [];
resetCanvas();
drawSignature(output, canvasContext, false);
}
}
previous.x = null
previous.y = null
if (settings.output && output.length > 0)
$(settings.output, context).val(JSON.stringify(output))
}
/**
* Draws the signature line
*
* @private
*/
function drawSigLine () {
if (!settings.lineWidth)
return false
canvasContext.beginPath()
canvasContext.lineWidth = settings.lineWidth
canvasContext.strokeStyle = settings.lineColour
canvasContext.moveTo(settings.lineMargin, settings.lineTop)
canvasContext.lineTo(element.width - settings.lineMargin, settings.lineTop)
canvasContext.stroke()
canvasContext.closePath()
}
function resetCanvas() {
canvasContext.clearRect(0, 0, element.width, element.height)
canvasContext.fillStyle = settings.bgColour
canvasContext.fillRect(0, 0, element.width, element.height)
if (!settings.displayOnly)
drawSigLine()
canvasContext.lineWidth = settings.penWidth
canvasContext.strokeStyle = settings.penColour
}
/**
* Clears all drawings off the canvas and redraws the signature line
*
* @private
*/
function clearCanvas () {
resetCanvas();
$(settings.output, context).val('')
output = []
stopDrawing()
}
/**
* Callback registered to mouse/touch events of the canvas
* Draws a line at the mouse cursor location, starting a new line if necessary
*
* @private
*
* @param {Object} e The event object
* @param {Object} o The object context registered to the event; canvas
*/
function onMouseMove(e, o) {
if (previous.x == null) {
drawLine(e, 1)
} else {
drawLine(e, o)
}
}
/**
* Callback registered to mouse/touch events of canvas
* Triggers the drawLine function
*
* @private
*
* @param {Object} e The event object
* @param {Object} touchObject The object context registered to the event; canvas
*/
function startDrawing (e, touchObject) {
if (touchable) {
touchObject.addEventListener('touchmove', onMouseMove, false)
} else {
canvas.bind('mousemove.signaturepad', onMouseMove)
}
// Draws a single point on initial mouse down, for people with periods in their name
drawLine(e, 1)
}
/**
* Removes all the mouse events from the canvas
*
* @private
*/
function disableCanvas () {
eventsBound = false
canvas.each(function () {
if (this.removeEventListener) {
this.removeEventListener('touchend', stopDrawing)
this.removeEventListener('touchcancel', stopDrawing)
this.removeEventListener('touchmove', drawLine)
}
if (this.ontouchstart)
this.ontouchstart = null;
})
$(document).unbind('mouseup.signaturepad')
canvas.unbind('mousedown.signaturepad')
canvas.unbind('mousemove.signaturepad')
canvas.unbind('mouseleave.signaturepad')
$(settings.clear, context).unbind('click.signaturepad')
}
/**
* Lazy touch event detection
* Uses the first press on the canvas to detect either touch or mouse reliably
* Will then bind other events as needed
*
* @private
*
* @param {Object} e The event object
*/
function initDrawEvents (e) {
if (eventsBound)
return false
eventsBound = true
// Closes open keyboards to free up space
$('input').blur();
if (typeof e.changedTouches !== 'undefined')
touchable = true
if (touchable) {
canvas.each(function () {
this.addEventListener('touchend', stopDrawing, false)
this.addEventListener('touchcancel', stopDrawing, false)
})
canvas.unbind('mousedown.signaturepad')
} else {
$(document).bind('mouseup.signaturepad', function () {
if (mouseButtonDown) {
stopDrawing()
clearMouseLeaveTimeout()
}
})
canvas.bind('mouseleave.signaturepad', function (e) {
if (mouseButtonDown) stopDrawing(e)
if (mouseButtonDown && !mouseLeaveTimeout) {
mouseLeaveTimeout = setTimeout(function () {
stopDrawing()
clearMouseLeaveTimeout()
}, 500)
}
})
canvas.each(function () {
this.ontouchstart = null
})
}
}
/**
* Triggers the abilities to draw on the canvas
* Sets up mouse/touch events, hides and shows descriptions and sets current classes
*
* @private
*/
function drawIt () {
$(settings.typed, context).hide()
clearCanvas()
canvas.each(function () {
this.ontouchstart = function (e) {
e.preventDefault()
mouseButtonDown = true
initDrawEvents(e)
startDrawing(e, this)
}
})
canvas.bind('mousedown.signaturepad', function (e) {
e.preventDefault()
mouseButtonDown = true
initDrawEvents(e)
startDrawing(e)
})
$(settings.clear, context).bind('click.signaturepad', function (e) { e.preventDefault(); clearCanvas() })
$(settings.typeIt, context).bind('click.signaturepad', function (e) { e.preventDefault(); typeIt() })
$(settings.drawIt, context).unbind('click.signaturepad')
$(settings.drawIt, context).bind('click.signaturepad', function (e) { e.preventDefault() })
$(settings.typeIt, context).removeClass(settings.currentClass)
$(settings.drawIt, context).addClass(settings.currentClass)
$(settings.sig, context).addClass(settings.currentClass)
$(settings.typeItDesc, context).hide()
$(settings.drawItDesc, context).show()
$(settings.clear, context).show()
}
/**
* Triggers the abilities to type in the input for generating a signature
* Sets up mouse events, hides and shows descriptions and sets current classes
*
* @private
*/
function typeIt () {
clearCanvas()
disableCanvas()
$(settings.typed, context).show()
$(settings.drawIt, context).bind('click.signaturepad', function (e) { e.preventDefault(); drawIt() })
$(settings.typeIt, context).unbind('click.signaturepad')
$(settings.typeIt, context).bind('click.signaturepad', function (e) { e.preventDefault() })
$(settings.output, context).val('')
$(settings.drawIt, context).removeClass(settings.currentClass)
$(settings.typeIt, context).addClass(settings.currentClass)
$(settings.sig, context).removeClass(settings.currentClass)
$(settings.drawItDesc, context).hide()
$(settings.clear, context).hide()
$(settings.typeItDesc, context).show()
typeItCurrentFontSize = typeItDefaultFontSize = $(settings.typed, context).css('font-size').replace(/px/, '')
}
/**
* Callback registered on key up and blur events for input field
* Writes the text fields value as Html into an element
*
* @private
*
* @param {String} val The value of the input field
*/
function type (val) {
var typed = $(settings.typed, context)
, cleanedVal = val.replace(/>/g, '>').replace(/</g, '<').trim()
, oldLength = typeItNumChars
, edgeOffset = typeItCurrentFontSize * 0.5
typeItNumChars = cleanedVal.length
typed.html(cleanedVal)
if (!cleanedVal) {
typed.css('font-size', typeItDefaultFontSize + 'px')
return
}
if (typeItNumChars > oldLength && typed.outerWidth() > element.width) {
while (typed.outerWidth() > element.width) {
typeItCurrentFontSize--
typed.css('font-size', typeItCurrentFontSize + 'px')
}
}
if (typeItNumChars < oldLength && typed.outerWidth() + edgeOffset < element.width && typeItCurrentFontSize < typeItDefaultFontSize) {
while (typed.outerWidth() + edgeOffset < element.width && typeItCurrentFontSize < typeItDefaultFontSize) {
typeItCurrentFontSize++
typed.css('font-size', typeItCurrentFontSize + 'px')
}
}
}
/**
* Default onBeforeValidate function to clear errors
*
* @private
*
* @param {Object} context current context object
* @param {Object} settings provided settings
*/
function onBeforeValidate (context, settings) {
$('p.' + settings.errorClass, context).remove()
context.removeClass(settings.errorClass)
$('input, label', context).removeClass(settings.errorClass)
}
/**
* Default onFormError function to show errors
*
* @private
*
* @param {Object} errors object contains validation errors (e.g. nameInvalid=true)
* @param {Object} context current context object
* @param {Object} settings provided settings
*/
function onFormError (errors, context, settings) {
if (errors.nameInvalid) {
context.prepend(['<p class="', settings.errorClass, '">', settings.errorMessage, '</p>'].join(''))
$(settings.name, context).focus()
$(settings.name, context).addClass(settings.errorClass)
$('label[for=' + $(settings.name).attr('id') + ']', context).addClass(settings.errorClass)
}
if (errors.drawInvalid)
context.prepend(['<p class="', settings.errorClass, '">', settings.errorMessageDraw, '</p>'].join(''))
}
/**
* Validates the form to confirm a name was typed in the field
* If drawOnly also confirms that the user drew a signature
*
* @private
*
* @return {Boolean}
*/
function validateForm () {
var valid = true
, errors = {drawInvalid: false, nameInvalid: false}
, onBeforeArguments = [context, settings]
, onErrorArguments = [errors, context, settings]
if (settings.onBeforeValidate && typeof settings.onBeforeValidate === 'function') {
settings.onBeforeValidate.apply(self,onBeforeArguments)
} else {
onBeforeValidate.apply(self, onBeforeArguments)
}
if (settings.drawOnly && output.length < 1) {
errors.drawInvalid = true
valid = false
}
if ($(settings.name, context).val() === '') {
errors.nameInvalid = true
valid = false
}
if (settings.onFormError && typeof settings.onFormError === 'function') {
settings.onFormError.apply(self,onErrorArguments)
} else {
onFormError.apply(self, onErrorArguments)
}
return valid
}
/**
* Draws a 3-sized piece of the signature. Supposed to be reused by
* drawSignature and drawLine
*
* @private
*
* @param {Array} Array of at least `bezierSkip` * 4 points
* @param {Object} context the canvas context to draw on
*/
function strokePath(paths, context) {
var showSampledPoints = false;
/* OK, a few thoughts here:
- if we have a signature with 250 points and we are going to draw 3 points each time,
then we would have ~83 calls to this function .
- I think I remembered why I didn't implement the autoscaling feature yesterday...
it was because it is really only valid for *drawOnly* mode... think about it, the user
draws the signature, then when drawSignature is called, the signature is centered, and
zoomed in/out... not a good UX, so maybe autoscaling should only be enabled when
drawOnly is enabled and it would be only part of drawSignature when called by regenerate
function.
*/
var bezierSkip = 4; // this program samples too fast, so even if we spline it,
// the result is choppy. need to throw away points or do
// a best fit curve of some sort. throwing away points
// is easier. Only use 1/bezierSkip points.
var section = []; // section is an array of path points that will be used
// to compute the bezier control points
var sections = []; // sections is an array of the preceding section arrays
// Separate the sampled points into contiguous sections
for (var i = 0; i < paths.length - 1; i++) {
// this method of separating the contiguous paths is fucking stupid
if (typeof(paths[i]) === 'object' && typeof(paths[i + 1]) === 'object') {
var source = paths[i];
var destination = paths[i + 1];
if (source.mx == source.lx && source.my == source.ly) {
// don't put duplicated elements in, it screws up
// the curves. do nothing here.
continue;
} else {
section.push(source);
}
if ( !(source.lx == destination.mx && source.ly == destination.my) &&
!(source.mx == destination.lx && source.my == destination.ly) ) {
// when we reach the endpoint or starting point of a signaturepad segment
// (i.e. somewhere where the user has either STARTED clicking and dragging,
// or somewhehire the user has lifted the mouse), one of these 2 conditions
// is met. save this as a separate section (so we don't try to stroke beziers
// where the user has lifted the mouse)
// First save the section as an independent piece in our sections array
sections.push(section);
// Now reset the section array to start recording the next section
section = [];
}
if (i == paths.length - 2) {
section.push(destination);
sections.push(section);
}
}
}
/*
Now we have sections of points that we have sampled.
Next step is to compute the Bezier control points that will render
curves that pass through all the sampled points.
*/
var lengths = [];
for (k = 0; k < sections.length; k++) {
// Clean the sections for use. Make sure we're keeping the endpoints, and strip out every 1/bezierSkip points.
var lastPoint = sections[k].pop();
sections[k] = sections[k].filter(function(element, index) { return index % settings.bezierSkip == 0; });
sections[k].push(lastPoint);
// compute total length information so we can do some variable width stuff
var section = sections[k];
for (j = 0; j < section.length; j++) {
var point = section[j];
// city blocks distance is good enough and doesn't require squaring and square rooting
var length = Math.abs(point.lx - point.mx) + Math.abs(point.ly - point.my);
lengths.push(length);
// console.log(point.lx, point.mx, point.ly , point.my);
}
}
var signatureStats = stats(lengths); //stats() is in beziers.js
signatureStats.length = numeric.sum(lengths);
signatureStats.mean *= 3; // the number of segments per bezier? Sort of.
signatureStats.deviation *= 3 // this isn't accurate, but its good enough for me for today and it makes the signature pretty-ish)
for (k = 0; k < sections.length; k++) {
var section = sections[k];
var simpleTuples = section.map(function(n) {return[n.lx, n.ly]});
var beziers = getBezierControlPoints(simpleTuples);
for (var i in beziers) {
var p0 = beziers[i][0],
p1 = beziers[i][1],
p2 = beziers[i][2],
p3 = beziers[i][3];
if (settings.variableStrokeWidth === true) {
var bezierSegmentLength = (
Math.abs(p0[0] - p1[0]) +
Math.abs(p1[0] - p2[0]) +
Math.abs(p2[0] - p3[0]) +
Math.abs(p0[1] - p1[1]) +
Math.abs(p1[1] - p2[1]) +
Math.abs(p2[1] - p3[1])
);
// is this long or short compared to the average segment?
// how many standard deviations from the mean (this data set isn't gaussian... but good enough)
var zscore = (bezierSegmentLength - signatureStats.mean) / signatureStats.deviation;
if (zscore > 0) {
// fast
var width = 3 - zscore / 2.5;
} else if (zscore <= 0) {
var width = 3 - zscore * 2;
}
// Ugh, this is straight up hacky. Basically, the distribution isn't gaussian.
// In particular, to the downside (negative z's, the magitude of zscores tend to
// not vary so far from the mean, but to the upside (positive z's, fast pen strokes),
// they tend to be really big numbers, like 4+ zscores. so this is stupidly applying
// different scaling on both sides of zero.
// console.log("len:", parseInt(bezierSegmentLength),'z-score:', +zscore.toFixed(3), 'width:', +width.toFixed(3));
}
// p0 and p3 are the start and end points of the bezier curve.
// i want to see these plotted.
if (showSampledPoints === true) {
var pixelSize = 2;
context.fillStyle = '#FF0000';
context.fillRect(p0[0], p0[1], pixelSize, pixelSize);
context.fillRect(p3[0], p3[1], pixelSize, pixelSize);
}
context.beginPath()
context.moveTo(p0[0], p0[1])
context.bezierCurveTo(
p1[0], p1[1],
p2[0], p2[1],
p3[0], p3[1]
);
context.lineWidth = settings.penWidth
context.lineWidth = width;
context.lineCap = settings.penCap
context.stroke()
context.closePath();
}
}
}
/**
* Redraws the signature on a specific canvas
*
* @private
*
* @param {Array} paths the signature JSON
* @param {Object} context the canvas context to draw on
* @param {Boolean} saveOutput whether to write the path to the output array or not
*/
function drawSignature (paths, context, saveOutput) {
if (settings.autoscale) {
var maxX = 0,
maxY = 0,
minX = $(canvas).width(),
minY = $(canvas).height();
$.each(paths, function(idx, el) {
maxX = Math.max(el.mx, el.lx, maxX);
minX = Math.min(el.mx, el.lx, minX);
maxY = Math.max(el.my, el.ly, maxY);
minY = Math.min(el.my, el.ly, minY);
});
var padding = 0.15;
maxX *= (1 + padding);
minX *= (1 - padding);
maxY *= (1 + padding);
minY *= (1 - padding);
var signatureWidth = maxX - minX;
var signatureHeight = maxY - minY;
var signatureAspectRatio = signatureWidth / signatureHeight;
var canvasAspectRatio = canvas.width() / canvas.height();
if (signatureAspectRatio > canvasAspectRatio) {
// signature is "fat"
// width is the constraining factor, so we need to limit this by width
var scaleFactor = canvas.width() / signatureWidth;
} else {
// signature is "tall"
var scaleFactor = canvas.height() / signatureHeight;
}
// there's whitespace to the top and left of the signature
// that gets scaled. translate it back to center
// first translate balances out the whitespace.
// this only fixes the whitespace on the CONSTRAINING
// dimenson. i.e. the x axis for "fat" signatures
context.translate(-minX * scaleFactor, -minY * scaleFactor);
context.scale.apply(context, [scaleFactor, scaleFactor]);
// now to center on the non-constraining dimension
// one of these will be zero (the constraining dimension)
// then the other gets translated to the middle
context.translate(
(canvas.width() / scaleFactor - signatureWidth) / 2,
(canvas.height() / scaleFactor - signatureHeight) / 2);
} else {
context.scale.apply(context, settings.scale);
}
for(var i in paths) {
if (typeof paths[i] === 'object') {
if (settings.drawBezierCurves === false) {
context.beginPath()
context.moveTo(paths[i].mx, paths[i].my)
context.lineTo(paths[i].lx, paths[i].ly)
context.lineCap = settings.penCap
context.stroke()
context.closePath()
}
if (saveOutput) {
output.push({
'lx' : paths[i].lx
, 'ly' : paths[i].ly
, 'mx' : paths[i].mx
, 'my' : paths[i].my
})
}
}
} /* end linear segments */
if (settings.drawBezierCurves === true) {
strokePath(paths, context);
} /* end bezier curves */
}
/**
* Initialisation function, called immediately after all declarations
* Technically public, but only should be used internally
*
* @private
*/
function init () {
// Fixes the jQuery.fn.offset() function for Mobile Safari Browsers i.e. iPod Touch, iPad and iPhone
// https://gist.github.com/661844
// http://bugs.jquery.com/ticket/6446
if (parseFloat(((/CPU.+OS ([0-9_]{3}).*AppleWebkit.*Mobile/i.exec(navigator.userAgent)) || [0,'4_2'])[1].replace('_','.')) < 4.1) {
$.fn.Oldoffset = $.fn.offset;
$.fn.offset = function () {
var result = $(this).Oldoffset()
result.top -= window.scrollY
result.left -= window.scrollX
return result
}
}
// Disable selection on the typed div and canvas
$(settings.typed, context).bind('selectstart.signaturepad', function (e) { return $(e.target).is(':input') })
canvas.bind('selectstart.signaturepad', function (e) { return $(e.target).is(':input') })
if (!element.getContext && FlashCanvas)
FlashCanvas.initElement(element)
if (element.getContext) {
canvasContext = element.getContext('2d')
$(settings.sig, context).show()
if (!settings.displayOnly) {
if (!settings.drawOnly) {
$(settings.name, context).bind('keyup.signaturepad', function () {
type($(this).val())
})
$(settings.name, context).bind('blur.signaturepad', function () {
type($(this).val())
})
$(settings.drawIt, context).bind('click.signaturepad', function (e) {
e.preventDefault()
drawIt()
})
}
if (settings.drawOnly || settings.defaultAction === 'drawIt') {
drawIt()
} else {
typeIt()
}
if (settings.validateFields) {
if ($(selector).is('form')) {
$(selector).bind('submit.signaturepad', function () { return validateForm() })
} else {
$(selector).parents('form').bind('submit.signaturepad', function () { return validateForm() })
}
}
$(settings.sigNav, context).show()
}
}
}
$.extend(self, {
/**
* Initializes SignaturePad
*/
init : function () { init() }
/**
* Allows options to be updated after initialization
*
* @param {Object} options An object containing the options to be changed
*/
, updateOptions : function (options) {
$.extend(settings, options)
}
/**
* Regenerates a signature on the canvas using an array of objects
* Follows same format as object property
* @see var object
*
* @param {Array} paths An array of the lines and points
*/
, regenerate : function (paths) {
self.clearCanvas()
$(settings.typed, context).hide()
if (typeof paths === 'string')
paths = JSON.parse(paths)
drawSignature(paths, canvasContext, true)
if (settings.output && $(settings.output, context).length > 0)
$(settings.output, context).val(JSON.stringify(output))
}
/**
* Clears the canvas
* Redraws the background colour and the signature line
*/
, clearCanvas : function () { clearCanvas() }
/**
* Returns the signature as a Js array
*