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, {
31 _mouseButtonDown: true,
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 backgroundColor: '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 velocityFilterWeight: 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
131 initEvents: function()
133 Roo.bootstrap.BezierSignature.superclass.initEvents.call(this);
135 var canvas = this.canvasEl();
137 // mouse && touch event swapping...
138 canvas.dom.style.touchAction = 'none';
139 canvas.dom.style.msTouchAction = 'none';
141 this._mouseButtonDown = false;
142 canvas.on('mousedown', this._handleMouseDown, this);
143 canvas.on('mousemove', this._handleMouseMove, this);
144 Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
146 if (window.ontouchstart) {
147 canvas.on('touchstart', this._handleTouchStart, this);
148 canvas.on('touchmove', this._handleTouchMove, this);
149 canvas.on('touchend', this._handleTouchEnd, this);
152 if(this.resize_to_parent_width) {
153 Roo.EventManager.onWindowResize(this.resize, this, true);
162 this.canvasEl().dom.width = this.el.dom.offsetWidth;
165 _handleMouseDown: function(e)
167 if (e.browserEvent.which === 1) {
168 this._mouseButtonDown = true;
173 _handleMouseMove: function (e)
175 if (this._mouseButtonDown) {
176 this.strokeMoveUpdate(e);
180 _handleMouseUp: function (e)
182 if (e.browserEvent.which === 1 && this._mouseButtonDown) {
183 this._mouseButtonDown = false;
188 _handleTouchStart: function (e) {
190 if (e.browserEvent.targetTouches.length === 1) {
191 // var touch = e.browserEvent.changedTouches[0];
192 // this.strokeBegin(touch);
194 this.strokeBegin(e); // assume e catching the correct xy...
198 _handleTouchMove: function (e) {
200 // var touch = event.targetTouches[0];
201 // _this._strokeMoveUpdate(touch);
202 this._strokeMoveUpdate(e);
205 _handleTouchEnd: function (e) {
206 var wasCanvasTouched = e.target === this.canvasEl().dom;
207 if (wasCanvasTouched) {
209 // var touch = event.changedTouches[0];
210 // _this._strokeEnd(touch);
216 this._lastPoints = [];
217 this._lastVelocity = 0;
218 this._lastWidth = (this.minWidth + this.maxWidth) / 2;
219 this.canvasElCtx().fillStyle = this.penColor;
222 strokeMoveUpdate: function(e)
224 this.strokeUpdate(e);
227 this.throttle(this.strokeUpdate, this.throttle);
230 this.strokeUpdate(e);
234 strokeBegin: function(e)
236 var newPointGroup = {
237 color: this.penColor,
241 if (typeof this.onBegin === 'function') {
245 this._data.push(newPointGroup);
247 this.strokeUpdate(e);
250 strokeUpdate: function(e)
252 var rect = this.canvasEl().dom.getBoundingClientRect();
253 var point = new this.Point(e.xy[0] - rect.left, e.xy[1] - rect.top, new Date().getTime());
254 var lastPointGroup = this._data[this._data.length - 1];
255 var lastPoints = lastPointGroup.points;
256 var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
257 var isLastPointTooClose = lastPoint
258 ? point.distanceTo(lastPoint) <= this.minDistance
260 var color = lastPointGroup.color;
261 if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
262 var curve = this.addPoint(point);
264 this.drawDot({color: color, point: point});
267 this.drawCurve({color: color, curve: curve});
277 strokeEnd: function(e)
279 this.strokeUpdate(e);
280 if (typeof this.onEnd === 'function') {
285 addPoint: function (point) {
286 var _lastPoints = this._lastPoints;
287 _lastPoints.push(point);
288 if (_lastPoints.length > 2) {
289 if (_lastPoints.length === 3) {
290 _lastPoints.unshift(_lastPoints[0]);
292 var widths = this.calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
293 var curve = this.Bezier.fromPoints(_lastPoints, widths, this);
300 calculateCurveWidths: function (startPoint, endPoint) {
301 var velocity = this.velocityFilterWeight * endPoint.velocityFrom(startPoint) +
302 (1 - this.velocityFilterWeight) * this._lastVelocity;
304 var newWidth = Math.max(this.maxWidth / (velocity + 1), this.minWidth);
307 start: this._lastWidth
310 this._lastVelocity = velocity;
311 this._lastWidth = newWidth;
315 drawDot: function (_a) {
316 var color = _a.color, point = _a.point;
317 var ctx = this.canvasElCtx();
318 var width = typeof this.dotSize === 'function' ? this.dotSize() : this.dotSize;
320 this.drawCurveSegment(point.x, point.y, width);
322 ctx.fillStyle = color;
326 drawCurve: function (_a) {
327 var color = _a.color, curve = _a.curve;
328 var ctx = this.canvasElCtx();
329 var widthDelta = curve.endWidth - curve.startWidth;
330 var drawSteps = Math.floor(curve.length()) * 2;
332 ctx.fillStyle = color;
333 for (var i = 0; i < drawSteps; i += 1) {
334 var t = i / drawSteps;
340 var x = uuu * curve.startPoint.x;
341 x += 3 * uu * t * curve.control1.x;
342 x += 3 * u * tt * curve.control2.x;
343 x += ttt * curve.endPoint.x;
344 var y = uuu * curve.startPoint.y;
345 y += 3 * uu * t * curve.control1.y;
346 y += 3 * u * tt * curve.control2.y;
347 y += ttt * curve.endPoint.y;
348 var width = curve.startWidth + ttt * widthDelta;
349 this.drawCurveSegment(x, y, width);
355 drawCurveSegment: function (x, y, width) {
356 var ctx = this.canvasElCtx();
358 ctx.arc(x, y, width, 0, 2 * Math.PI, false);
359 this._isEmpty = false;
364 var ctx = this.canvasElCtx();
365 var canvas = this.canvasEl().dom;
366 ctx.fillStyle = this.backgroundColor;
367 ctx.clearRect(0, 0, canvas.width, canvas.height);
368 ctx.fillRect(0, 0, canvas.width, canvas.height);
371 this._isEmpty = true;
376 return this.el.select('canvas',true).first();
379 canvasElCtx: function()
381 return this.el.select('canvas',true).first().dom.getContext('2d');
384 getImage: function(type)
391 return this.canvasEl().dom.toDataURL('image/'+type, false);
394 drawFromImage: function(img_src)
396 var img = new Image();
400 this.canvasElCtx().drawImage(img, 0, 0);
403 // Bezier Point Constructor
404 Point: (function () {
405 function Point(x, y, time) {
408 this.time = time || Date.now();
410 Point.prototype.distanceTo = function (start) {
411 return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
413 Point.prototype.equals = function (other) {
414 return this.x === other.x && this.y === other.y && this.time === other.time;
416 Point.prototype.velocityFrom = function (start) {
417 return this.time !== start.time
418 ? this.distanceTo(start) / (this.time - start.time)
425 // Bezier Constructor
426 Bezier: (function () {
427 function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
428 this.startPoint = startPoint;
429 this.control2 = control2;
430 this.control1 = control1;
431 this.endPoint = endPoint;
432 this.startWidth = startWidth;
433 this.endWidth = endWidth;
435 Bezier.fromPoints = function (points, widths, scope) {
436 var c2 = this.calculateControlPoints(points[0], points[1], points[2], scope).c2;
437 var c3 = this.calculateControlPoints(points[1], points[2], points[3], scope).c1;
438 return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
440 Bezier.calculateControlPoints = function (s1, s2, s3, scope) {
441 var dx1 = s1.x - s2.x;
442 var dy1 = s1.y - s2.y;
443 var dx2 = s2.x - s3.x;
444 var dy2 = s2.y - s3.y;
445 var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
446 var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
447 var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
448 var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
449 var dxm = m1.x - m2.x;
450 var dym = m1.y - m2.y;
451 var k = l2 / (l1 + l2);
452 var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
453 var tx = s2.x - cm.x;
454 var ty = s2.y - cm.y;
456 c1: new scope.Point(m1.x + tx, m1.y + ty),
457 c2: new scope.Point(m2.x + tx, m2.y + ty)
460 Bezier.prototype.length = function () {
465 for (var i = 0; i <= steps; i += 1) {
467 var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
468 var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
472 length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
479 Bezier.prototype.point = function (t, start, c1, c2, end) {
480 return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
481 + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
482 + (3.0 * c2 * (1.0 - t) * t * t)
488 throttle: function(fn, wait) {
489 if (wait === void 0) { wait = 250; }
495 var later = function () {
496 previous = Date.now();
498 result = fn.apply(storedContext, storedArgs);
500 storedContext = null;
504 return function wrapper() {
506 for (var _i = 0; _i < arguments.length; _i++) {
507 args[_i] = arguments[_i];
509 var now = Date.now();
510 var remaining = wait - (now - previous);
511 storedContext = this;
513 if (remaining <= 0 || remaining > wait) {
515 clearTimeout(timeout);
519 result = fn.apply(storedContext, storedArgs);
521 storedContext = null;
526 timeout = window.setTimeout(later, remaining);