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);
25 Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,
35 * @cfg {int} canvas height
37 canvas_height: '200px',
40 * @cfg {float|function} Radius of a single dot.
45 * @cfg {float} Minimum width of a line. Defaults to 0.5.
50 * @cfg {float} Maximum width of a line. Defaults to 2.5.
55 * @cfg {integer} Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
60 * @cfg {integer} Add the next point only if the previous one is farther than x pixels. Defaults to 5.
65 * @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.
67 bg_color: 'rgba(0, 0, 0, 0)',
70 * @cfg {string} Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
75 * @cfg {float} Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
77 velocity_filter_weight: 0.7,
80 * @cfg {function} Callback when stroke begin.
85 * @cfg {function} Callback when stroke end.
89 getAutoCreate : function()
91 var cls = 'roo-signature column';
94 cls += ' ' + this.cls;
104 for(var i = 0; i < col_sizes.length; i++) {
105 if(this[col_sizes[i]]) {
106 cls += " col-"+col_sizes[i]+"-"+this[col_sizes[i]];
116 cls: 'roo-signature-body',
120 cls: 'roo-signature-body-canvas',
121 height: this.canvas_height,
122 width: this.canvas_width
129 style: 'display: none'
137 initEvents: function()
139 Roo.bootstrap.BezierSignature.superclass.initEvents.call(this);
141 var canvas = this.canvasEl();
143 // mouse && touch event swapping...
144 canvas.dom.style.touchAction = 'none';
145 canvas.dom.style.msTouchAction = 'none';
147 this.mouse_btn_down = false;
148 canvas.on('mousedown', this._handleMouseDown, this);
149 canvas.on('mousemove', this._handleMouseMove, this);
150 Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
152 if (window.PointerEvent) {
153 canvas.on('pointerdown', this._handleMouseDown, this);
154 canvas.on('pointermove', this._handleMouseMove, this);
155 Roo.select('html').first().on('pointerup', this._handleMouseUp, this);
158 if ('ontouchstart' in window) {
159 canvas.on('touchstart', this._handleTouchStart, this);
160 canvas.on('touchmove', this._handleTouchMove, this);
161 canvas.on('touchend', this._handleTouchEnd, this);
164 Roo.EventManager.onWindowResize(this.resize, this, true);
167 this.fileEl().on('change', this.uploadImage, this);
176 var canvas = this.canvasEl().dom;
177 var ctx = this.canvasElCtx();
178 var img_data = false;
180 if(canvas.width > 0) {
181 var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height);
183 // setting canvas width will clean img data
186 var style = window.getComputedStyle ?
187 getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle;
189 var padding_left = parseInt(style.paddingLeft) || 0;
190 var padding_right = parseInt(style.paddingRight) || 0;
192 canvas.width = this.el.dom.clientWidth - padding_left - padding_right;
195 ctx.putImageData(img_data, 0, 0);
199 _handleMouseDown: function(e)
201 if (e.browserEvent.which === 1) {
202 this.mouse_btn_down = true;
207 _handleMouseMove: function (e)
209 if (this.mouse_btn_down) {
210 this.strokeMoveUpdate(e);
214 _handleMouseUp: function (e)
216 if (e.browserEvent.which === 1 && this.mouse_btn_down) {
217 this.mouse_btn_down = false;
222 _handleTouchStart: function (e) {
225 if (e.browserEvent.targetTouches.length === 1) {
226 // var touch = e.browserEvent.changedTouches[0];
227 // this.strokeBegin(touch);
229 this.strokeBegin(e); // assume e catching the correct xy...
233 _handleTouchMove: function (e) {
235 // var touch = event.targetTouches[0];
236 // _this._strokeMoveUpdate(touch);
237 this.strokeMoveUpdate(e);
240 _handleTouchEnd: function (e) {
241 var wasCanvasTouched = e.target === this.canvasEl().dom;
242 if (wasCanvasTouched) {
244 // var touch = event.changedTouches[0];
245 // _this._strokeEnd(touch);
251 this._lastPoints = [];
252 this._lastVelocity = 0;
253 this._lastWidth = (this.min_width + this.max_width) / 2;
254 this.canvasElCtx().fillStyle = this.dot_color;
257 strokeMoveUpdate: function(e)
259 this.strokeUpdate(e);
262 this.throttle(this.strokeUpdate, this.throttle);
265 this.strokeUpdate(e);
269 strokeBegin: function(e)
271 var newPointGroup = {
272 color: this.dot_color,
276 if (typeof this.onBegin === 'function') {
280 this.curve_data.push(newPointGroup);
282 this.strokeUpdate(e);
285 strokeUpdate: function(e)
287 var rect = this.canvasEl().dom.getBoundingClientRect();
288 var point = new this.Point(e.xy[0] - rect.left, e.xy[1] - rect.top, new Date().getTime());
289 var lastPointGroup = this.curve_data[this.curve_data.length - 1];
290 var lastPoints = lastPointGroup.points;
291 var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
292 var isLastPointTooClose = lastPoint
293 ? point.distanceTo(lastPoint) <= this.min_distance
295 var color = lastPointGroup.color;
296 if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
297 var curve = this.addPoint(point);
299 this.drawDot({color: color, point: point});
302 this.drawCurve({color: color, curve: curve});
312 strokeEnd: function(e)
314 this.strokeUpdate(e);
315 if (typeof this.onEnd === 'function') {
320 addPoint: function (point) {
321 var _lastPoints = this._lastPoints;
322 _lastPoints.push(point);
323 if (_lastPoints.length > 2) {
324 if (_lastPoints.length === 3) {
325 _lastPoints.unshift(_lastPoints[0]);
327 var widths = this.calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
328 var curve = this.Bezier.fromPoints(_lastPoints, widths, this);
335 calculateCurveWidths: function (startPoint, endPoint) {
336 var velocity = this.velocity_filter_weight * endPoint.velocityFrom(startPoint) +
337 (1 - this.velocity_filter_weight) * this._lastVelocity;
339 var newWidth = Math.max(this.max_width / (velocity + 1), this.min_width);
342 start: this._lastWidth
345 this._lastVelocity = velocity;
346 this._lastWidth = newWidth;
350 drawDot: function (_a) {
351 var color = _a.color, point = _a.point;
352 var ctx = this.canvasElCtx();
353 var width = typeof this.dot_size === 'function' ? this.dot_size() : this.dot_size;
355 this.drawCurveSegment(point.x, point.y, width);
357 ctx.fillStyle = color;
361 drawCurve: function (_a) {
362 var color = _a.color, curve = _a.curve;
363 var ctx = this.canvasElCtx();
364 var widthDelta = curve.endWidth - curve.startWidth;
365 var drawSteps = Math.floor(curve.length()) * 2;
367 ctx.fillStyle = color;
368 for (var i = 0; i < drawSteps; i += 1) {
369 var t = i / drawSteps;
375 var x = uuu * curve.startPoint.x;
376 x += 3 * uu * t * curve.control1.x;
377 x += 3 * u * tt * curve.control2.x;
378 x += ttt * curve.endPoint.x;
379 var y = uuu * curve.startPoint.y;
380 y += 3 * uu * t * curve.control1.y;
381 y += 3 * u * tt * curve.control2.y;
382 y += ttt * curve.endPoint.y;
383 var width = curve.startWidth + ttt * widthDelta;
384 this.drawCurveSegment(x, y, width);
390 drawCurveSegment: function (x, y, width) {
391 var ctx = this.canvasElCtx();
393 ctx.arc(x, y, width, 0, 2 * Math.PI, false);
394 this.is_empty = false;
399 var ctx = this.canvasElCtx();
400 var canvas = this.canvasEl().dom;
401 ctx.fillStyle = this.bg_color;
402 ctx.clearRect(0, 0, canvas.width, canvas.height);
403 ctx.fillRect(0, 0, canvas.width, canvas.height);
404 this.curve_data = [];
406 this.is_empty = true;
411 return this.el.select('input',true).first();
416 return this.el.select('canvas',true).first();
419 canvasElCtx: function()
421 return this.el.select('canvas',true).first().dom.getContext('2d');
424 getImage: function(type)
431 return this.canvasEl().dom.toDataURL('image/'+type, 1);
434 drawFromImage: function(img_src)
436 var img = new Image();
438 img.onload = function(){
439 this.canvasElCtx().drawImage(img, 0, 0);
444 this.is_empty = false;
447 selectImage: function()
449 this.fileEl().dom.click();
452 uploadImage: function(e)
454 var reader = new FileReader();
456 reader.onload = function(e){
457 var img = new Image();
458 img.onload = function(){
460 this.canvasElCtx().drawImage(img, 0, 0);
462 img.src = e.target.result;
465 reader.readAsDataURL(e.target.files[0]);
468 // Bezier Point Constructor
469 Point: (function () {
470 function Point(x, y, time) {
473 this.time = time || Date.now();
475 Point.prototype.distanceTo = function (start) {
476 return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
478 Point.prototype.equals = function (other) {
479 return this.x === other.x && this.y === other.y && this.time === other.time;
481 Point.prototype.velocityFrom = function (start) {
482 return this.time !== start.time
483 ? this.distanceTo(start) / (this.time - start.time)
490 // Bezier Constructor
491 Bezier: (function () {
492 function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
493 this.startPoint = startPoint;
494 this.control2 = control2;
495 this.control1 = control1;
496 this.endPoint = endPoint;
497 this.startWidth = startWidth;
498 this.endWidth = endWidth;
500 Bezier.fromPoints = function (points, widths, scope) {
501 var c2 = this.calculateControlPoints(points[0], points[1], points[2], scope).c2;
502 var c3 = this.calculateControlPoints(points[1], points[2], points[3], scope).c1;
503 return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
505 Bezier.calculateControlPoints = function (s1, s2, s3, scope) {
506 var dx1 = s1.x - s2.x;
507 var dy1 = s1.y - s2.y;
508 var dx2 = s2.x - s3.x;
509 var dy2 = s2.y - s3.y;
510 var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
511 var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
512 var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
513 var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
514 var dxm = m1.x - m2.x;
515 var dym = m1.y - m2.y;
516 var k = l2 / (l1 + l2);
517 var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
518 var tx = s2.x - cm.x;
519 var ty = s2.y - cm.y;
521 c1: new scope.Point(m1.x + tx, m1.y + ty),
522 c2: new scope.Point(m2.x + tx, m2.y + ty)
525 Bezier.prototype.length = function () {
530 for (var i = 0; i <= steps; i += 1) {
532 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
533 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
537 length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
544 Bezier.prototype.point = function (t, start, c1, c2, end) {
545 return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
546 + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
547 + (3.0 * c2 * (1.0 - t) * t * t)
553 throttle: function(fn, wait) {
554 if (wait === void 0) { wait = 250; }
560 var later = function () {
561 previous = Date.now();
563 result = fn.apply(storedContext, storedArgs);
565 storedContext = null;
569 return function wrapper() {
571 for (var _i = 0; _i < arguments.length; _i++) {
572 args[_i] = arguments[_i];
574 var now = Date.now();
575 var remaining = wait - (now - previous);
576 storedContext = this;
578 if (remaining <= 0 || remaining > wait) {
580 clearTimeout(timeout);
584 result = fn.apply(storedContext, storedArgs);
586 storedContext = null;
591 timeout = window.setTimeout(later, remaining);