Bezier signature prototype
[roojs1] / Roo / bootstrap / BezierSignature.js
1 /**
2 *    This script refer to:
3 *    Title: Signature Pad
4 *    Author: szimek
5 *    Availability: https://github.com/szimek/signature_pad
6 **/
7
8 /**
9  * @class Roo.bootstrap.BezierSignature
10  * @extends Roo.bootstrap.Component
11  * Bootstrap BezierSignature class
12  * 
13  * @constructor
14  * Create a new BezierSignature
15  * @param {Object} config The config object
16  */
17
18 Roo.bootstrap.BezierSignature = function(config){
19     Roo.bootstrap.BezierSignature.superclass.constructor.call(this, config);
20     
21     this.addEvents({
22         // raw events
23         /**
24          * @event click
25          * When a Brick is click
26          * @param {Roo.bootstrap.Brick} this
27          * @param {Roo.EventObject} e
28          */
29         "click" : true
30     });
31 };
32
33 Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
34     
35     /**
36      * @cfg(float or function) Radius of a single dot.
37      */ 
38     dotSize: false,
39     
40     /**
41      * @cfg(float) Minimum width of a line. Defaults to 0.5.
42      */
43     minWidth: 0.5,
44     
45     /**
46      * @cfg(float) Maximum width of a line. Defaults to 2.5.
47      */
48     maxWidth: 2.5,
49     
50     /**
51      * @cfg(integer) Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
52      */
53     throttle: 16,
54     
55     /**
56      * @cfg(integer) Add the next point only if the previous one is farther than x pixels. Defaults to 5.
57      */
58     minDistance: 5,
59     
60     /**
61      * @cfg(string) Color used to clear the background. Can be any color format accepted by context.fillStyle. Defaults to "rgba(0,0,0,0)" (transparent black). Use a non-transparent color e.g. "rgb(255,255,255)" (opaque white) if you'd like to save signatures as JPEG images.
62      */
63     backgroundColor: 'rgba(0,0,0,0)',
64     
65     /**
66      * @cfg(string) Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
67      */
68     penColor: 'black',
69     
70     /**
71      * @cfg(float) Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
72      */
73     velocityFilterWeight: 0.7,
74     
75     /**
76      * @cfg(function) Callback when stroke begin.
77      */
78     onBegin: false,
79     
80     /**
81      * @cfg(function) Callback when stroke end.
82      */
83     onEnd: false,
84     
85     Point: (function () {
86         function Point(x, y, time) {
87             this.x = x;
88             this.y = y;
89             this.time = time || Date.now();
90         }
91         Point.prototype.distanceTo = function (start) {
92             return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
93         };
94         Point.prototype.equals = function (other) {
95             return this.x === other.x && this.y === other.y && this.time === other.time;
96         };
97         Point.prototype.velocityFrom = function (start) {
98             return this.time !== start.time
99             ? this.distanceTo(start) / (this.time - start.time)
100             : 0;
101         };
102         return Point;
103     }()),
104     
105     Bezier: (function () {
106         function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
107             this.startPoint = startPoint;
108             this.control2 = control2;
109             this.control1 = control1;
110             this.endPoint = endPoint;
111             this.startWidth = startWidth;
112             this.endWidth = endWidth;
113         }
114         Bezier.fromPoints = function (points, widths) {
115             var c2 = this.calculateControlPoints(points[0], points[1], points[2]).c2;
116             var c3 = this.calculateControlPoints(points[1], points[2], points[3]).c1;
117             return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
118         };
119         Bezier.calculateControlPoints = function (s1, s2, s3) {
120             var dx1 = s1.x - s2.x;
121             var dy1 = s1.y - s2.y;
122             var dx2 = s2.x - s3.x;
123             var dy2 = s2.y - s3.y;
124             var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
125             var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
126             var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
127             var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
128             var dxm = m1.x - m2.x;
129             var dym = m1.y - m2.y;
130             var k = l2 / (l1 + l2);
131             var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
132             var tx = s2.x - cm.x;
133             var ty = s2.y - cm.y;
134             return {
135                 c1: new Point(m1.x + tx, m1.y + ty),
136                 c2: new Point(m2.x + tx, m2.y + ty)
137             };
138         };
139         Bezier.prototype.length = function () {
140             var steps = 10;
141             var length = 0;
142             var px;
143             var py;
144             for (var i = 0; i <= steps; i += 1) {
145                 var t = i / steps;
146                 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
147                 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
148                 if (i > 0) {
149                     var xdiff = cx - px;
150                     var ydiff = cy - py;
151                     length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
152                 }
153                 px = cx;
154                 py = cy;
155             }
156             return length;
157         };
158         Bezier.prototype.point = function (t, start, c1, c2, end) {
159             return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
160             + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
161             + (3.0 * c2 * (1.0 - t) * t * t)
162             + (end * t * t * t);
163         };
164         return Bezier;
165     }()),
166     
167     getAutoCreate : function()
168     {
169         var cls = 'roo-signature';
170         
171         if(this.cls){
172             cls += ' ' + this.cls;
173         }
174         
175         var cfg = {
176             tag: 'div',
177             cls: cls,
178             cn: [
179                 {
180                     tag: 'div',
181                     cls: 'roo-signature-body',
182                     cn: [
183                         {
184                             tag: 'canvas',
185                             cls: 'roo-signature-footer'
186                         }
187                     ]
188                 }
189             ]
190         };
191         
192         return cfg;
193     },
194     
195     initEvents: function() 
196     {
197         // assign all object in here...
198     },
199     
200     isValid: function()
201     {
202         // form cannot detect...
203     }
204     
205 });
206
207  
208
209