2 * This script refer to:
5 * Availability: https://github.com/szimek/signature_pad
9 * @class Roo.bootstrap.BezierSignature
10 * @extends Roo.bootstrap.Component
11 * Bootstrap BezierSignature class
14 * Create a new BezierSignature
15 * @param {Object} config The config object
18 Roo.bootstrap.BezierSignature = function(config){
19 Roo.bootstrap.BezierSignature.superclass.constructor.call(this, config);
22 Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, {
26 _mouseButtonDown: true,
29 * @cfg(float or function) Radius of a single dot.
34 * @cfg(float) Minimum width of a line. Defaults to 0.5.
39 * @cfg(float) Maximum width of a line. Defaults to 2.5.
44 * @cfg(integer) Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
49 * @cfg(integer) Add the next point only if the previous one is farther than x pixels. Defaults to 5.
54 * @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.
56 backgroundColor: 'rgba(0,0,0,0)',
59 * @cfg(string) Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
64 * @cfg(float) Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
66 velocityFilterWeight: 0.7,
69 * @cfg(function) Callback when stroke begin.
74 * @cfg(function) Callback when stroke end.
78 getAutoCreate : function()
80 var cls = 'roo-signature';
83 cls += ' ' + this.cls;
92 cls: 'roo-signature-body',
96 cls: 'roo-signature-body-canvas'
106 initEvents: function()
108 Roo.bootstrap.BezierSignature.superclass.initEvents.call(this);
110 var canvas = this.canvasEl();
112 canvas.dom.style.touchAction = 'none';
113 canvas.dom.style.msTouchAction = 'none';
115 this._mouseButtonDown = false;
116 canvas.on('mousedown', this._handleMouseDown, this);
117 canvas.on('mousemove', this._handleMouseMove, this);
118 // catching mouseup for whole doc... any better way to catch it
119 Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
121 if (window.ontouchstart) {
122 canvas.on('touchstart', this._handleTouchStart, this);
123 canvas.on('touchmove', this._handleTouchMove, this);
124 canvas.on('touchend', this._handleTouchEnd, this);
127 // this.canvas.on('click', this.onClick, this);
130 _handleMouseDown: function(e)
132 if (e.browserEvent.which === 1) {
133 this._mouseButtonDown = true;
138 _handleMouseMove: function (e)
140 if (this._mouseButtonDown) {
141 this.strokeMoveUpdate(e);
145 _handleMouseUp: function (e)
147 if (e.browserEvent.which === 1 && this._mouseButtonDown) {
148 this._mouseButtonDown = false;
154 this._lastPoints = [];
155 this._lastVelocity = 0;
156 this._lastWidth = (this.minWidth + this.maxWidth) / 2;
157 this.canvasElCtx().fillStyle = this.penColor;
160 strokeMoveUpdate: function(e)
162 this.strokeUpdate(e);
165 this.throttle(this.strokeUpdate, this.throttle);
168 this.strokeUpdate(e);
172 strokeBegin: function(e)
174 var newPointGroup = {
175 color: this.penColor,
179 if (typeof this.onBegin === 'function') {
183 this._data.push(newPointGroup);
185 this.strokeUpdate(e);
188 strokeUpdate: function(e)
190 var rect = this.canvasEl().dom.getBoundingClientRect();
191 var point = new this.Point(e.browserEvent.clientX - rect.left, e.browserEvent.clientY - rect.top, new Date().getTime());
192 var lastPointGroup = this._data[this._data.length - 1];
193 var lastPoints = lastPointGroup.points;
194 var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
195 var isLastPointTooClose = lastPoint
196 ? point.distanceTo(lastPoint) <= this.minDistance
198 var color = lastPointGroup.color;
199 if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
200 var curve = this.addPoint(point);
202 this.drawDot({color: color, point: point});
205 this.drawCurve({color: color, curve: curve});
215 strokeEnd: function(e)
217 this.strokeUpdate(e);
218 if (typeof this.onEnd === 'function') {
223 addPoint: function (point) {
224 var _lastPoints = this._lastPoints;
225 _lastPoints.push(point);
226 if (_lastPoints.length > 2) {
227 if (_lastPoints.length === 3) {
228 _lastPoints.unshift(_lastPoints[0]);
230 var widths = this.calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
231 var curve = this.Bezier.fromPoints(_lastPoints, widths, this);
238 calculateCurveWidths: function (startPoint, endPoint) {
239 var velocity = this.velocityFilterWeight * endPoint.velocityFrom(startPoint) +
240 (1 - this.velocityFilterWeight) * this._lastVelocity;
242 var newWidth = Math.max(this.maxWidth / (velocity + 1), this.minWidth);
245 start: this._lastWidth
248 this._lastVelocity = velocity;
249 this._lastWidth = newWidth;
253 drawDot: function (_a) {
254 var color = _a.color, point = _a.point;
255 var ctx = this.canvasElCtx();
256 var width = typeof this.dotSize === 'function' ? this.dotSize() : this.dotSize;
258 this.drawCurveSegment(point.x, point.y, width);
260 ctx.fillStyle = color;
264 drawCurve: function (_a) {
265 var color = _a.color, curve = _a.curve;
266 var ctx = this.canvasElCtx();
267 var widthDelta = curve.endWidth - curve.startWidth;
268 var drawSteps = Math.floor(curve.length()) * 2;
270 ctx.fillStyle = color;
271 for (var i = 0; i < drawSteps; i += 1) {
272 var t = i / drawSteps;
278 var x = uuu * curve.startPoint.x;
279 x += 3 * uu * t * curve.control1.x;
280 x += 3 * u * tt * curve.control2.x;
281 x += ttt * curve.endPoint.x;
282 var y = uuu * curve.startPoint.y;
283 y += 3 * uu * t * curve.control1.y;
284 y += 3 * u * tt * curve.control2.y;
285 y += ttt * curve.endPoint.y;
286 var width = curve.startWidth + ttt * widthDelta;
287 this.drawCurveSegment(x, y, width);
293 drawCurveSegment: function (x, y, width) {
294 var ctx = this.canvasElCtx();
296 ctx.arc(x, y, width, 0, 2 * Math.PI, false);
297 this._isEmpty = false;
302 // form cannot detect...
307 return this.el.select('canvas',true).first();
310 canvasElCtx: function()
312 return this.el.select('canvas',true).first().dom.getContext('2d');
315 // Bezier Point Constructor
316 Point: (function () {
317 function Point(x, y, time) {
320 this.time = time || Date.now();
322 Point.prototype.distanceTo = function (start) {
323 return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
325 Point.prototype.equals = function (other) {
326 return this.x === other.x && this.y === other.y && this.time === other.time;
328 Point.prototype.velocityFrom = function (start) {
329 return this.time !== start.time
330 ? this.distanceTo(start) / (this.time - start.time)
337 // Bezier Constructor
338 Bezier: (function () {
339 function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
340 this.startPoint = startPoint;
341 this.control2 = control2;
342 this.control1 = control1;
343 this.endPoint = endPoint;
344 this.startWidth = startWidth;
345 this.endWidth = endWidth;
347 Bezier.fromPoints = function (points, widths, scope) {
348 var c2 = this.calculateControlPoints(points[0], points[1], points[2], scope).c2;
349 var c3 = this.calculateControlPoints(points[1], points[2], points[3], scope).c1;
350 return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
352 Bezier.calculateControlPoints = function (s1, s2, s3, scope) {
353 var dx1 = s1.x - s2.x;
354 var dy1 = s1.y - s2.y;
355 var dx2 = s2.x - s3.x;
356 var dy2 = s2.y - s3.y;
357 var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
358 var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
359 var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
360 var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
361 var dxm = m1.x - m2.x;
362 var dym = m1.y - m2.y;
363 var k = l2 / (l1 + l2);
364 var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
365 var tx = s2.x - cm.x;
366 var ty = s2.y - cm.y;
368 c1: new scope.Point(m1.x + tx, m1.y + ty),
369 c2: new scope.Point(m2.x + tx, m2.y + ty)
372 Bezier.prototype.length = function () {
377 for (var i = 0; i <= steps; i += 1) {
379 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
380 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
384 length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
391 Bezier.prototype.point = function (t, start, c1, c2, end) {
392 return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
393 + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
394 + (3.0 * c2 * (1.0 - t) * t * t)
400 throttle: function(fn, wait) {
401 if (wait === void 0) { wait = 250; }
407 var later = function () {
408 previous = Date.now();
410 result = fn.apply(storedContext, storedArgs);
412 storedContext = null;
416 return function wrapper() {
418 for (var _i = 0; _i < arguments.length; _i++) {
419 args[_i] = arguments[_i];
421 var now = Date.now();
422 var remaining = wait - (now - previous);
423 storedContext = this;
425 if (remaining <= 0 || remaining > wait) {
427 clearTimeout(timeout);
431 result = fn.apply(storedContext, storedArgs);
433 storedContext = null;
438 timeout = window.setTimeout(later, remaining);