6 * @class Roo.bootstrap.BezierSignature
7 * @extends Roo.bootstrap.Component
8 * Bootstrap BezierSignature class
9 * This script refer to:
10 * Title: Signature Pad
12 * Availability: https://github.com/szimek/signature_pad
15 * Create a new BezierSignature
16 * @param {Object} config The config object
19 Roo.bootstrap.BezierSignature = function(config){
20 Roo.bootstrap.BezierSignature.superclass.constructor.call(this, config);
26 Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,
36 * @cfg {int} canvas height
38 canvas_height: '200px',
41 * @cfg {float|function} Radius of a single dot.
46 * @cfg {float} Minimum width of a line. Defaults to 0.5.
51 * @cfg {float} Maximum width of a line. Defaults to 2.5.
56 * @cfg {integer} Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
61 * @cfg {integer} Add the next point only if the previous one is farther than x pixels. Defaults to 5.
66 * @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.
68 bg_color: 'rgba(0, 0, 0, 0)',
71 * @cfg {string} Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
76 * @cfg {float} Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
78 velocity_filter_weight: 0.7,
81 * @cfg {function} Callback when stroke begin.
86 * @cfg {function} Callback when stroke end.
90 getAutoCreate : function()
92 var cls = 'roo-signature column';
95 cls += ' ' + this.cls;
105 for(var i = 0; i < col_sizes.length; i++) {
106 if(this[col_sizes[i]]) {
107 cls += " col-"+col_sizes[i]+"-"+this[col_sizes[i]];
117 cls: 'roo-signature-body',
121 cls: 'roo-signature-body-canvas',
122 height: this.canvas_height,
123 width: this.canvas_width
130 style: 'display: none'
138 initEvents: function()
140 Roo.bootstrap.BezierSignature.superclass.initEvents.call(this);
142 var canvas = this.canvasEl();
144 // mouse && touch event swapping...
145 canvas.dom.style.touchAction = 'none';
146 canvas.dom.style.msTouchAction = 'none';
148 this.mouse_btn_down = false;
149 canvas.on('mousedown', this._handleMouseDown, this);
150 canvas.on('mousemove', this._handleMouseMove, this);
151 Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
153 if (window.PointerEvent) {
154 canvas.on('pointerdown', this._handleMouseDown, this);
155 canvas.on('pointermove', this._handleMouseMove, this);
156 Roo.select('html').first().on('pointerup', this._handleMouseUp, this);
159 if ('ontouchstart' in window) {
160 canvas.on('touchstart', this._handleTouchStart, this);
161 canvas.on('touchmove', this._handleTouchMove, this);
162 canvas.on('touchend', this._handleTouchEnd, this);
165 Roo.EventManager.onWindowResize(this.resize, this, true);
168 this.fileEl().on('change', this.uploadImage, this);
177 var canvas = this.canvasEl().dom;
178 var ctx = this.canvasElCtx();
179 var img_data = false;
181 if(canvas.width > 0) {
182 var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height);
184 // setting canvas width will clean img data
187 var style = window.getComputedStyle ?
188 getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle;
190 var padding_left = parseInt(style.paddingLeft) || 0;
191 var padding_right = parseInt(style.paddingRight) || 0;
193 canvas.width = this.el.dom.clientWidth - padding_left - padding_right;
196 ctx.putImageData(img_data, 0, 0);
200 _handleMouseDown: function(e)
202 if (e.browserEvent.which === 1) {
203 this.mouse_btn_down = true;
208 _handleMouseMove: function (e)
210 if (this.mouse_btn_down) {
211 this.strokeMoveUpdate(e);
215 _handleMouseUp: function (e)
217 if (e.browserEvent.which === 1 && this.mouse_btn_down) {
218 this.mouse_btn_down = false;
223 _handleTouchStart: function (e) {
226 if (e.browserEvent.targetTouches.length === 1) {
227 // var touch = e.browserEvent.changedTouches[0];
228 // this.strokeBegin(touch);
230 this.strokeBegin(e); // assume e catching the correct xy...
234 _handleTouchMove: function (e) {
236 // var touch = event.targetTouches[0];
237 // _this._strokeMoveUpdate(touch);
238 this.strokeMoveUpdate(e);
241 _handleTouchEnd: function (e) {
242 var wasCanvasTouched = e.target === this.canvasEl().dom;
243 if (wasCanvasTouched) {
245 // var touch = event.changedTouches[0];
246 // _this._strokeEnd(touch);
252 this._lastPoints = [];
253 this._lastVelocity = 0;
254 this._lastWidth = (this.min_width + this.max_width) / 2;
255 this.canvasElCtx().fillStyle = this.dot_color;
258 strokeMoveUpdate: function(e)
260 this.strokeUpdate(e);
263 this.throttle(this.strokeUpdate, this.throttle);
266 this.strokeUpdate(e);
270 strokeBegin: function(e)
272 var newPointGroup = {
273 color: this.dot_color,
277 if (typeof this.onBegin === 'function') {
281 this.curve_data.push(newPointGroup);
283 this.strokeUpdate(e);
286 strokeUpdate: function(e)
288 var rect = this.canvasEl().dom.getBoundingClientRect();
289 var point = new this.Point(e.xy[0] - rect.left, e.xy[1] - rect.top, new Date().getTime());
290 var lastPointGroup = this.curve_data[this.curve_data.length - 1];
291 var lastPoints = lastPointGroup.points;
292 var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
293 var isLastPointTooClose = lastPoint
294 ? point.distanceTo(lastPoint) <= this.min_distance
296 var color = lastPointGroup.color;
297 if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
298 var curve = this.addPoint(point);
300 this.drawDot({color: color, point: point});
303 this.drawCurve({color: color, curve: curve});
313 strokeEnd: function(e)
315 this.strokeUpdate(e);
316 if (typeof this.onEnd === 'function') {
321 addPoint: function (point) {
322 var _lastPoints = this._lastPoints;
323 _lastPoints.push(point);
324 if (_lastPoints.length > 2) {
325 if (_lastPoints.length === 3) {
326 _lastPoints.unshift(_lastPoints[0]);
328 var widths = this.calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
329 var curve = this.Bezier.fromPoints(_lastPoints, widths, this);
336 calculateCurveWidths: function (startPoint, endPoint) {
337 var velocity = this.velocity_filter_weight * endPoint.velocityFrom(startPoint) +
338 (1 - this.velocity_filter_weight) * this._lastVelocity;
340 var newWidth = Math.max(this.max_width / (velocity + 1), this.min_width);
343 start: this._lastWidth
346 this._lastVelocity = velocity;
347 this._lastWidth = newWidth;
351 drawDot: function (_a) {
352 var color = _a.color, point = _a.point;
353 var ctx = this.canvasElCtx();
354 var width = typeof this.dot_size === 'function' ? this.dot_size() : this.dot_size;
356 this.drawCurveSegment(point.x, point.y, width);
358 ctx.fillStyle = color;
362 drawCurve: function (_a) {
363 var color = _a.color, curve = _a.curve;
364 var ctx = this.canvasElCtx();
365 var widthDelta = curve.endWidth - curve.startWidth;
366 var drawSteps = Math.floor(curve.length()) * 2;
368 ctx.fillStyle = color;
369 for (var i = 0; i < drawSteps; i += 1) {
370 var t = i / drawSteps;
376 var x = uuu * curve.startPoint.x;
377 x += 3 * uu * t * curve.control1.x;
378 x += 3 * u * tt * curve.control2.x;
379 x += ttt * curve.endPoint.x;
380 var y = uuu * curve.startPoint.y;
381 y += 3 * uu * t * curve.control1.y;
382 y += 3 * u * tt * curve.control2.y;
383 y += ttt * curve.endPoint.y;
384 var width = curve.startWidth + ttt * widthDelta;
385 this.drawCurveSegment(x, y, width);
391 drawCurveSegment: function (x, y, width) {
392 var ctx = this.canvasElCtx();
394 ctx.arc(x, y, width, 0, 2 * Math.PI, false);
395 this.is_empty = false;
400 var ctx = this.canvasElCtx();
401 var canvas = this.canvasEl().dom;
402 ctx.fillStyle = this.bg_color;
403 ctx.clearRect(0, 0, canvas.width, canvas.height);
404 ctx.fillRect(0, 0, canvas.width, canvas.height);
405 this.curve_data = [];
407 this.is_empty = true;
412 return this.el.select('input',true).first();
417 return this.el.select('canvas',true).first();
420 canvasElCtx: function()
422 return this.el.select('canvas',true).first().dom.getContext('2d');
425 getImage: function(type)
432 return this.canvasEl().dom.toDataURL('image/'+type, 1);
435 drawFromImage: function(img_src)
437 var img = new Image();
439 img.onload = function(){
440 this.canvasElCtx().drawImage(img, 0, 0);
445 this.is_empty = false;
448 selectImage: function()
450 this.fileEl().dom.click();
453 uploadImage: function(e)
455 var reader = new FileReader();
457 reader.onload = function(e){
458 var img = new Image();
459 img.onload = function(){
461 this.canvasElCtx().drawImage(img, 0, 0);
463 img.src = e.target.result;
466 reader.readAsDataURL(e.target.files[0]);
469 // Bezier Point Constructor
470 Point: (function () {
471 function Point(x, y, time) {
474 this.time = time || Date.now();
476 Point.prototype.distanceTo = function (start) {
477 return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
479 Point.prototype.equals = function (other) {
480 return this.x === other.x && this.y === other.y && this.time === other.time;
482 Point.prototype.velocityFrom = function (start) {
483 return this.time !== start.time
484 ? this.distanceTo(start) / (this.time - start.time)
491 // Bezier Constructor
492 Bezier: (function () {
493 function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
494 this.startPoint = startPoint;
495 this.control2 = control2;
496 this.control1 = control1;
497 this.endPoint = endPoint;
498 this.startWidth = startWidth;
499 this.endWidth = endWidth;
501 Bezier.fromPoints = function (points, widths, scope) {
502 var c2 = this.calculateControlPoints(points[0], points[1], points[2], scope).c2;
503 var c3 = this.calculateControlPoints(points[1], points[2], points[3], scope).c1;
504 return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
506 Bezier.calculateControlPoints = function (s1, s2, s3, scope) {
507 var dx1 = s1.x - s2.x;
508 var dy1 = s1.y - s2.y;
509 var dx2 = s2.x - s3.x;
510 var dy2 = s2.y - s3.y;
511 var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
512 var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
513 var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
514 var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
515 var dxm = m1.x - m2.x;
516 var dym = m1.y - m2.y;
517 var k = l2 / (l1 + l2);
518 var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
519 var tx = s2.x - cm.x;
520 var ty = s2.y - cm.y;
522 c1: new scope.Point(m1.x + tx, m1.y + ty),
523 c2: new scope.Point(m2.x + tx, m2.y + ty)
526 Bezier.prototype.length = function () {
531 for (var i = 0; i <= steps; i += 1) {
533 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
534 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
538 length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
545 Bezier.prototype.point = function (t, start, c1, c2, end) {
546 return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
547 + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
548 + (3.0 * c2 * (1.0 - t) * t * t)
554 throttle: function(fn, wait) {
555 if (wait === void 0) { wait = 250; }
561 var later = function () {
562 previous = Date.now();
564 result = fn.apply(storedContext, storedArgs);
566 storedContext = null;
570 return function wrapper() {
572 for (var _i = 0; _i < arguments.length; _i++) {
573 args[_i] = arguments[_i];
575 var now = Date.now();
576 var remaining = wait - (now - previous);
577 storedContext = this;
579 if (remaining <= 0 || remaining > wait) {
581 clearTimeout(timeout);
585 result = fn.apply(storedContext, storedArgs);
587 storedContext = null;
592 timeout = window.setTimeout(later, remaining);