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