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, {
34 * @cfg(int) canvas height
36 canvas_height: '200px',
39 * @cfg(float or function) Radius of a single dot.
44 * @cfg(float) Minimum width of a line. Defaults to 0.5.
49 * @cfg(float) Maximum width of a line. Defaults to 2.5.
54 * @cfg(integer) Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
59 * @cfg(integer) Add the next point only if the previous one is farther than x pixels. Defaults to 5.
64 * @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.
66 bg_color: 'rgba(0, 0, 0, 0)',
69 * @cfg(string) Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
74 * @cfg(float) Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
76 velocity_filter_weight: 0.7,
79 * @cfg(function) Callback when stroke begin.
84 * @cfg(function) Callback when stroke end.
88 getAutoCreate : function()
90 var cls = 'roo-signature column';
93 cls += ' ' + this.cls;
103 for(var i = 0; i < col_sizes.length; i++) {
104 if(this[col_sizes[i]]) {
105 cls += " col-"+col_sizes[i]+"-"+this[col_sizes[i]];
115 cls: 'roo-signature-body',
119 cls: 'roo-signature-body-canvas',
120 height: this.canvas_height,
121 width: this.canvas_width
128 style: 'display: none'
136 initEvents: function()
138 Roo.bootstrap.BezierSignature.superclass.initEvents.call(this);
140 var canvas = this.canvasEl();
142 // mouse && touch event swapping...
143 canvas.dom.style.touchAction = 'none';
144 canvas.dom.style.msTouchAction = 'none';
146 this.mouse_btn_down = false;
147 canvas.on('mousedown', this._handleMouseDown, this);
148 canvas.on('mousemove', this._handleMouseMove, this);
149 Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
151 if (window.PointerEvent) {
152 canvas.on('pointerdown', this._handleMouseDown, this);
153 canvas.on('pointermove', this._handleMouseMove, this);
154 Roo.select('html').first().on('pointerup', this._handleMouseUp, this);
157 if ('ontouchstart' in window) {
158 canvas.on('touchstart', this._handleTouchStart, this);
159 canvas.on('touchmove', this._handleTouchMove, this);
160 canvas.on('touchend', this._handleTouchEnd, this);
163 Roo.EventManager.onWindowResize(this.resize, this, true);
166 this.fileEl().on('change', this.uploadImage, this);
175 var canvas = this.canvasEl().dom;
176 var ctx = this.canvasElCtx();
177 var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height);
179 // setting canvas width will clean img data
182 var style = window.getComputedStyle ?
183 getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle;
185 var padding_left = parseInt(style.paddingLeft) || 0;
186 var padding_right = parseInt(style.paddingRight) || 0;
188 canvas.width = this.el.dom.clientWidth - padding_left - padding_right;
190 ctx.putImageData(img_data, 0, 0);
193 _handleMouseDown: function(e)
195 if (e.browserEvent.which === 1) {
196 this.mouse_btn_down = true;
201 _handleMouseMove: function (e)
203 if (this.mouse_btn_down) {
204 this.strokeMoveUpdate(e);
208 _handleMouseUp: function (e)
210 if (e.browserEvent.which === 1 && this.mouse_btn_down) {
211 this.mouse_btn_down = false;
216 _handleTouchStart: function (e) {
219 if (e.browserEvent.targetTouches.length === 1) {
220 // var touch = e.browserEvent.changedTouches[0];
221 // this.strokeBegin(touch);
223 this.strokeBegin(e); // assume e catching the correct xy...
227 _handleTouchMove: function (e) {
229 // var touch = event.targetTouches[0];
230 // _this._strokeMoveUpdate(touch);
231 this.strokeMoveUpdate(e);
234 _handleTouchEnd: function (e) {
235 var wasCanvasTouched = e.target === this.canvasEl().dom;
236 if (wasCanvasTouched) {
238 // var touch = event.changedTouches[0];
239 // _this._strokeEnd(touch);
245 this._lastPoints = [];
246 this._lastVelocity = 0;
247 this._lastWidth = (this.min_width + this.max_width) / 2;
248 this.canvasElCtx().fillStyle = this.dot_color;
251 strokeMoveUpdate: function(e)
253 this.strokeUpdate(e);
256 this.throttle(this.strokeUpdate, this.throttle);
259 this.strokeUpdate(e);
263 strokeBegin: function(e)
265 var newPointGroup = {
266 color: this.dot_color,
270 if (typeof this.onBegin === 'function') {
274 this.curve_data.push(newPointGroup);
276 this.strokeUpdate(e);
279 strokeUpdate: function(e)
281 var rect = this.canvasEl().dom.getBoundingClientRect();
282 var point = new this.Point(e.xy[0] - rect.left, e.xy[1] - rect.top, new Date().getTime());
283 var lastPointGroup = this.curve_data[this.curve_data.length - 1];
284 var lastPoints = lastPointGroup.points;
285 var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
286 var isLastPointTooClose = lastPoint
287 ? point.distanceTo(lastPoint) <= this.min_distance
289 var color = lastPointGroup.color;
290 if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
291 var curve = this.addPoint(point);
293 this.drawDot({color: color, point: point});
296 this.drawCurve({color: color, curve: curve});
306 strokeEnd: function(e)
308 this.strokeUpdate(e);
309 if (typeof this.onEnd === 'function') {
314 addPoint: function (point) {
315 var _lastPoints = this._lastPoints;
316 _lastPoints.push(point);
317 if (_lastPoints.length > 2) {
318 if (_lastPoints.length === 3) {
319 _lastPoints.unshift(_lastPoints[0]);
321 var widths = this.calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
322 var curve = this.Bezier.fromPoints(_lastPoints, widths, this);
329 calculateCurveWidths: function (startPoint, endPoint) {
330 var velocity = this.velocity_filter_weight * endPoint.velocityFrom(startPoint) +
331 (1 - this.velocity_filter_weight) * this._lastVelocity;
333 var newWidth = Math.max(this.max_width / (velocity + 1), this.min_width);
336 start: this._lastWidth
339 this._lastVelocity = velocity;
340 this._lastWidth = newWidth;
344 drawDot: function (_a) {
345 var color = _a.color, point = _a.point;
346 var ctx = this.canvasElCtx();
347 var width = typeof this.dot_size === 'function' ? this.dot_size() : this.dot_size;
349 this.drawCurveSegment(point.x, point.y, width);
351 ctx.fillStyle = color;
355 drawCurve: function (_a) {
356 var color = _a.color, curve = _a.curve;
357 var ctx = this.canvasElCtx();
358 var widthDelta = curve.endWidth - curve.startWidth;
359 var drawSteps = Math.floor(curve.length()) * 2;
361 ctx.fillStyle = color;
362 for (var i = 0; i < drawSteps; i += 1) {
363 var t = i / drawSteps;
369 var x = uuu * curve.startPoint.x;
370 x += 3 * uu * t * curve.control1.x;
371 x += 3 * u * tt * curve.control2.x;
372 x += ttt * curve.endPoint.x;
373 var y = uuu * curve.startPoint.y;
374 y += 3 * uu * t * curve.control1.y;
375 y += 3 * u * tt * curve.control2.y;
376 y += ttt * curve.endPoint.y;
377 var width = curve.startWidth + ttt * widthDelta;
378 this.drawCurveSegment(x, y, width);
384 drawCurveSegment: function (x, y, width) {
385 var ctx = this.canvasElCtx();
387 ctx.arc(x, y, width, 0, 2 * Math.PI, false);
388 this.is_empty = false;
393 var ctx = this.canvasElCtx();
394 var canvas = this.canvasEl().dom;
395 ctx.fillStyle = this.bg_color;
396 ctx.clearRect(0, 0, canvas.width, canvas.height);
397 ctx.fillRect(0, 0, canvas.width, canvas.height);
398 this.curve_data = [];
400 this.is_empty = true;
405 return this.el.select('input',true).first();
410 return this.el.select('canvas',true).first();
413 canvasElCtx: function()
415 return this.el.select('canvas',true).first().dom.getContext('2d');
418 getImage: function(type)
425 return this.canvasEl().dom.toDataURL('image/'+type, 1);
428 drawFromImage: function(img_src)
430 var img = new Image();
432 img.onload = function(){
433 this.canvasElCtx().drawImage(img, 0, 0);
438 this.is_empty = false;
441 selectImage: function()
443 this.fileEl().dom.click();
446 uploadImage: function(e)
448 var reader = new FileReader();
450 reader.onload = function(e){
451 var img = new Image();
452 img.onload = function(){
454 this.canvasElCtx().drawImage(img, 0, 0);
456 img.src = e.target.result;
459 reader.readAsDataURL(e.target.files[0]);
462 // Bezier Point Constructor
463 Point: (function () {
464 function Point(x, y, time) {
467 this.time = time || Date.now();
469 Point.prototype.distanceTo = function (start) {
470 return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
472 Point.prototype.equals = function (other) {
473 return this.x === other.x && this.y === other.y && this.time === other.time;
475 Point.prototype.velocityFrom = function (start) {
476 return this.time !== start.time
477 ? this.distanceTo(start) / (this.time - start.time)
484 // Bezier Constructor
485 Bezier: (function () {
486 function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
487 this.startPoint = startPoint;
488 this.control2 = control2;
489 this.control1 = control1;
490 this.endPoint = endPoint;
491 this.startWidth = startWidth;
492 this.endWidth = endWidth;
494 Bezier.fromPoints = function (points, widths, scope) {
495 var c2 = this.calculateControlPoints(points[0], points[1], points[2], scope).c2;
496 var c3 = this.calculateControlPoints(points[1], points[2], points[3], scope).c1;
497 return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
499 Bezier.calculateControlPoints = function (s1, s2, s3, scope) {
500 var dx1 = s1.x - s2.x;
501 var dy1 = s1.y - s2.y;
502 var dx2 = s2.x - s3.x;
503 var dy2 = s2.y - s3.y;
504 var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
505 var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
506 var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
507 var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
508 var dxm = m1.x - m2.x;
509 var dym = m1.y - m2.y;
510 var k = l2 / (l1 + l2);
511 var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
512 var tx = s2.x - cm.x;
513 var ty = s2.y - cm.y;
515 c1: new scope.Point(m1.x + tx, m1.y + ty),
516 c2: new scope.Point(m2.x + tx, m2.y + ty)
519 Bezier.prototype.length = function () {
524 for (var i = 0; i <= steps; i += 1) {
526 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
527 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
531 length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
538 Bezier.prototype.point = function (t, start, c1, c2, end) {
539 return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
540 + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
541 + (3.0 * c2 * (1.0 - t) * t * t)
547 throttle: function(fn, wait) {
548 if (wait === void 0) { wait = 250; }
554 var later = function () {
555 previous = Date.now();
557 result = fn.apply(storedContext, storedArgs);
559 storedContext = null;
563 return function wrapper() {
565 for (var _i = 0; _i < arguments.length; _i++) {
566 args[_i] = arguments[_i];
568 var now = Date.now();
569 var remaining = wait - (now - previous);
570 storedContext = this;
572 if (remaining <= 0 || remaining > wait) {
574 clearTimeout(timeout);
578 result = fn.apply(storedContext, storedArgs);
580 storedContext = null;
585 timeout = window.setTimeout(later, remaining);