2 * @class Roo.bootstrap.BezierSignature
3 * @extends Roo.bootstrap.Component
4 * Bootstrap BezierSignature class
5 * This script refer to:
8 * Availability: https://github.com/szimek/signature_pad
11 * Create a new BezierSignature
12 * @param {Object} config The config object
15 Roo.bootstrap.BezierSignature = function(config){
16 Roo.bootstrap.BezierSignature.superclass.constructor.call(this, config);
22 Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,
32 * @cfg {int} canvas height
34 canvas_height: '200px',
37 * @cfg {float|function} Radius of a single dot.
42 * @cfg {float} Minimum width of a line. Defaults to 0.5.
47 * @cfg {float} Maximum width of a line. Defaults to 2.5.
52 * @cfg {integer} Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
57 * @cfg {integer} Add the next point only if the previous one is farther than x pixels. Defaults to 5.
62 * @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.
64 bg_color: 'rgba(0, 0, 0, 0)',
67 * @cfg {string} Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
72 * @cfg {float} Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
74 velocity_filter_weight: 0.7,
77 * @cfg {function} Callback when stroke begin.
82 * @cfg {function} Callback when stroke end.
86 getAutoCreate : function()
88 var cls = 'roo-signature column';
91 cls += ' ' + this.cls;
101 for(var i = 0; i < col_sizes.length; i++) {
102 if(this[col_sizes[i]]) {
103 cls += " col-"+col_sizes[i]+"-"+this[col_sizes[i]];
113 cls: 'roo-signature-body',
117 cls: 'roo-signature-body-canvas',
118 height: this.canvas_height,
119 width: this.canvas_width
126 style: 'display: none'
134 initEvents: function()
136 Roo.bootstrap.BezierSignature.superclass.initEvents.call(this);
138 var canvas = this.canvasEl();
140 // mouse && touch event swapping...
141 canvas.dom.style.touchAction = 'none';
142 canvas.dom.style.msTouchAction = 'none';
144 this.mouse_btn_down = false;
145 canvas.on('mousedown', this._handleMouseDown, this);
146 canvas.on('mousemove', this._handleMouseMove, this);
147 Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
149 if (window.PointerEvent) {
150 canvas.on('pointerdown', this._handleMouseDown, this);
151 canvas.on('pointermove', this._handleMouseMove, this);
152 Roo.select('html').first().on('pointerup', this._handleMouseUp, this);
155 if ('ontouchstart' in window) {
156 canvas.on('touchstart', this._handleTouchStart, this);
157 canvas.on('touchmove', this._handleTouchMove, this);
158 canvas.on('touchend', this._handleTouchEnd, this);
161 Roo.EventManager.onWindowResize(this.resize, this, true);
164 this.fileEl().on('change', this.uploadImage, this);
173 var canvas = this.canvasEl().dom;
174 var ctx = this.canvasElCtx();
175 var img_data = false;
177 if(canvas.width > 0) {
178 var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height);
180 // setting canvas width will clean img data
183 var style = window.getComputedStyle ?
184 getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle;
186 var padding_left = parseInt(style.paddingLeft) || 0;
187 var padding_right = parseInt(style.paddingRight) || 0;
189 canvas.width = this.el.dom.clientWidth - padding_left - padding_right;
192 ctx.putImageData(img_data, 0, 0);
196 _handleMouseDown: function(e)
198 if (e.browserEvent.which === 1) {
199 this.mouse_btn_down = true;
204 _handleMouseMove: function (e)
206 if (this.mouse_btn_down) {
207 this.strokeMoveUpdate(e);
211 _handleMouseUp: function (e)
213 if (e.browserEvent.which === 1 && this.mouse_btn_down) {
214 this.mouse_btn_down = false;
219 _handleTouchStart: function (e) {
222 if (e.browserEvent.targetTouches.length === 1) {
223 // var touch = e.browserEvent.changedTouches[0];
224 // this.strokeBegin(touch);
226 this.strokeBegin(e); // assume e catching the correct xy...
230 _handleTouchMove: function (e) {
232 // var touch = event.targetTouches[0];
233 // _this._strokeMoveUpdate(touch);
234 this.strokeMoveUpdate(e);
237 _handleTouchEnd: function (e) {
238 var wasCanvasTouched = e.target === this.canvasEl().dom;
239 if (wasCanvasTouched) {
241 // var touch = event.changedTouches[0];
242 // _this._strokeEnd(touch);
248 this._lastPoints = [];
249 this._lastVelocity = 0;
250 this._lastWidth = (this.min_width + this.max_width) / 2;
251 this.canvasElCtx().fillStyle = this.dot_color;
254 strokeMoveUpdate: function(e)
256 this.strokeUpdate(e);
259 this.throttleStroke(this.strokeUpdate, this.throttle);
262 this.strokeUpdate(e);
266 strokeBegin: function(e)
268 var newPointGroup = {
269 color: this.dot_color,
273 if (typeof this.onBegin === 'function') {
277 this.curve_data.push(newPointGroup);
279 this.strokeUpdate(e);
282 strokeUpdate: function(e)
284 var rect = this.canvasEl().dom.getBoundingClientRect();
285 var point = new this.Point(e.xy[0] - rect.left, e.xy[1] - rect.top, new Date().getTime());
286 var lastPointGroup = this.curve_data[this.curve_data.length - 1];
287 var lastPoints = lastPointGroup.points;
288 var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
289 var isLastPointTooClose = lastPoint
290 ? point.distanceTo(lastPoint) <= this.min_distance
292 var color = lastPointGroup.color;
293 if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
294 var curve = this.addPoint(point);
296 this.drawDot({color: color, point: point});
299 this.drawCurve({color: color, curve: curve});
309 strokeEnd: function(e)
311 this.strokeUpdate(e);
312 if (typeof this.onEnd === 'function') {
317 addPoint: function (point) {
318 var _lastPoints = this._lastPoints;
319 _lastPoints.push(point);
320 if (_lastPoints.length > 2) {
321 if (_lastPoints.length === 3) {
322 _lastPoints.unshift(_lastPoints[0]);
324 var widths = this.calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
325 var curve = this.Bezier.fromPoints(_lastPoints, widths, this);
332 calculateCurveWidths: function (startPoint, endPoint) {
333 var velocity = this.velocity_filter_weight * endPoint.velocityFrom(startPoint) +
334 (1 - this.velocity_filter_weight) * this._lastVelocity;
336 var newWidth = Math.max(this.max_width / (velocity + 1), this.min_width);
339 start: this._lastWidth
342 this._lastVelocity = velocity;
343 this._lastWidth = newWidth;
347 drawDot: function (_a) {
348 var color = _a.color, point = _a.point;
349 var ctx = this.canvasElCtx();
350 var width = typeof this.dot_size === 'function' ? this.dot_size() : this.dot_size;
352 this.drawCurveSegment(point.x, point.y, width);
354 ctx.fillStyle = color;
358 drawCurve: function (_a) {
359 var color = _a.color, curve = _a.curve;
360 var ctx = this.canvasElCtx();
361 var widthDelta = curve.endWidth - curve.startWidth;
362 var drawSteps = Math.floor(curve.length()) * 2;
364 ctx.fillStyle = color;
365 for (var i = 0; i < drawSteps; i += 1) {
366 var t = i / drawSteps;
372 var x = uuu * curve.startPoint.x;
373 x += 3 * uu * t * curve.control1.x;
374 x += 3 * u * tt * curve.control2.x;
375 x += ttt * curve.endPoint.x;
376 var y = uuu * curve.startPoint.y;
377 y += 3 * uu * t * curve.control1.y;
378 y += 3 * u * tt * curve.control2.y;
379 y += ttt * curve.endPoint.y;
380 var width = curve.startWidth + ttt * widthDelta;
381 this.drawCurveSegment(x, y, width);
387 drawCurveSegment: function (x, y, width) {
388 var ctx = this.canvasElCtx();
390 ctx.arc(x, y, width, 0, 2 * Math.PI, false);
391 this.is_empty = false;
396 var ctx = this.canvasElCtx();
397 var canvas = this.canvasEl().dom;
398 ctx.fillStyle = this.bg_color;
399 ctx.clearRect(0, 0, canvas.width, canvas.height);
400 ctx.fillRect(0, 0, canvas.width, canvas.height);
401 this.curve_data = [];
403 this.is_empty = true;
408 return this.el.select('input',true).first();
413 return this.el.select('canvas',true).first();
416 canvasElCtx: function()
418 return this.el.select('canvas',true).first().dom.getContext('2d');
421 getImage: function(type)
428 return this.canvasEl().dom.toDataURL('image/'+type, 1);
431 drawFromImage: function(img_src)
433 var img = new Image();
435 img.onload = function(){
436 this.canvasElCtx().drawImage(img, 0, 0);
441 this.is_empty = false;
444 selectImage: function()
446 this.fileEl().dom.click();
449 uploadImage: function(e)
451 var reader = new FileReader();
453 reader.onload = function(e){
454 var img = new Image();
455 img.onload = function(){
457 this.canvasElCtx().drawImage(img, 0, 0);
459 img.src = e.target.result;
462 reader.readAsDataURL(e.target.files[0]);
465 // Bezier Point Constructor
466 Point: (function () {
467 function Point(x, y, time) {
470 this.time = time || Date.now();
472 Point.prototype.distanceTo = function (start) {
473 return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
475 Point.prototype.equals = function (other) {
476 return this.x === other.x && this.y === other.y && this.time === other.time;
478 Point.prototype.velocityFrom = function (start) {
479 return this.time !== start.time
480 ? this.distanceTo(start) / (this.time - start.time)
487 // Bezier Constructor
488 Bezier: (function () {
489 function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
490 this.startPoint = startPoint;
491 this.control2 = control2;
492 this.control1 = control1;
493 this.endPoint = endPoint;
494 this.startWidth = startWidth;
495 this.endWidth = endWidth;
497 Bezier.fromPoints = function (points, widths, scope) {
498 var c2 = this.calculateControlPoints(points[0], points[1], points[2], scope).c2;
499 var c3 = this.calculateControlPoints(points[1], points[2], points[3], scope).c1;
500 return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
502 Bezier.calculateControlPoints = function (s1, s2, s3, scope) {
503 var dx1 = s1.x - s2.x;
504 var dy1 = s1.y - s2.y;
505 var dx2 = s2.x - s3.x;
506 var dy2 = s2.y - s3.y;
507 var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
508 var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
509 var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
510 var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
511 var dxm = m1.x - m2.x;
512 var dym = m1.y - m2.y;
513 var k = l2 / (l1 + l2);
514 var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
515 var tx = s2.x - cm.x;
516 var ty = s2.y - cm.y;
518 c1: new scope.Point(m1.x + tx, m1.y + ty),
519 c2: new scope.Point(m2.x + tx, m2.y + ty)
522 Bezier.prototype.length = function () {
527 for (var i = 0; i <= steps; i += 1) {
529 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
530 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
534 length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
541 Bezier.prototype.point = function (t, start, c1, c2, end) {
542 return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
543 + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
544 + (3.0 * c2 * (1.0 - t) * t * t)
550 throttleStroke: function(fn, wait) {
551 if (wait === void 0) { wait = 250; }
557 var later = function () {
558 previous = Date.now();
560 result = fn.apply(storedContext, storedArgs);
562 storedContext = null;
566 return function wrapper() {
568 for (var _i = 0; _i < arguments.length; _i++) {
569 args[_i] = arguments[_i];
571 var now = Date.now();
572 var remaining = wait - (now - previous);
573 storedContext = this;
575 if (remaining <= 0 || remaining > wait) {
577 clearTimeout(timeout);
581 result = fn.apply(storedContext, storedArgs);
583 storedContext = null;
588 timeout = window.setTimeout(later, remaining);