From a391fa1cdcac294e0580da0e3e5483a57e7983c8 Mon Sep 17 00:00:00 2001 From: "john@roojs.com" Date: Fri, 28 Dec 2018 15:36:42 +0800 Subject: [PATCH] FIX: bezier signature --- Roo/bootstrap/BezierSignature.js | 17 +++++++---- roojs-bootstrap-debug.js | 17 +++++++---- roojs-bootstrap.js | 48 ++++++++++++++++---------------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/Roo/bootstrap/BezierSignature.js b/Roo/bootstrap/BezierSignature.js index ae7de0f3d3..2eabfc47e1 100644 --- a/Roo/bootstrap/BezierSignature.js +++ b/Roo/bootstrap/BezierSignature.js @@ -143,7 +143,13 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { canvas.on('mousemove', this._handleMouseMove, this); Roo.select('html').first().on('mouseup', this._handleMouseUp, this); - if (window.ontouchstart) { + if (window.PointerEvent) { + canvas.on('pointerdown', this._handleMouseDown, this); + canvas.on('pointermove', this._handleMouseMove, this); + Roo.select('html').first().on('pointerup', this._handleMouseUp, this); + } + + if ('ontouchstart' in window) { canvas.on('touchstart', this._handleTouchStart, this); canvas.on('touchmove', this._handleTouchMove, this); canvas.on('touchend', this._handleTouchEnd, this); @@ -157,17 +163,17 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { }, resize: function(){ + var canvas = this.canvasEl().dom; var ctx = this.canvasElCtx(); - var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height); - // setting canvas width will clean img data inside + // setting canvas width will clean img data canvas.width = 0; var style = window.getComputedStyle ? getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle; - + var padding_left = parseInt(style.paddingLeft) || 0; var padding_right = parseInt(style.paddingRight) || 0; @@ -200,6 +206,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { }, _handleTouchStart: function (e) { + e.preventDefault(); if (e.browserEvent.targetTouches.length === 1) { // var touch = e.browserEvent.changedTouches[0]; @@ -213,7 +220,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { e.preventDefault(); // var touch = event.targetTouches[0]; // _this._strokeMoveUpdate(touch); - this._strokeMoveUpdate(e); + this.strokeMoveUpdate(e); }, _handleTouchEnd: function (e) { diff --git a/roojs-bootstrap-debug.js b/roojs-bootstrap-debug.js index 980c3ce65c..33d1a1ea00 100644 --- a/roojs-bootstrap-debug.js +++ b/roojs-bootstrap-debug.js @@ -41644,7 +41644,13 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { canvas.on('mousemove', this._handleMouseMove, this); Roo.select('html').first().on('mouseup', this._handleMouseUp, this); - if (window.ontouchstart) { + if (window.PointerEvent) { + canvas.on('pointerdown', this._handleMouseDown, this); + canvas.on('pointermove', this._handleMouseMove, this); + Roo.select('html').first().on('pointerup', this._handleMouseUp, this); + } + + if ('ontouchstart' in window) { canvas.on('touchstart', this._handleTouchStart, this); canvas.on('touchmove', this._handleTouchMove, this); canvas.on('touchend', this._handleTouchEnd, this); @@ -41658,17 +41664,17 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { }, resize: function(){ + var canvas = this.canvasEl().dom; var ctx = this.canvasElCtx(); - var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height); - // setting canvas width will clean img data inside + // setting canvas width will clean img data canvas.width = 0; var style = window.getComputedStyle ? getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle; - + var padding_left = parseInt(style.paddingLeft) || 0; var padding_right = parseInt(style.paddingRight) || 0; @@ -41701,6 +41707,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { }, _handleTouchStart: function (e) { + e.preventDefault(); if (e.browserEvent.targetTouches.length === 1) { // var touch = e.browserEvent.changedTouches[0]; @@ -41714,7 +41721,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { e.preventDefault(); // var touch = event.targetTouches[0]; // _this._strokeMoveUpdate(touch); - this._strokeMoveUpdate(e); + this.strokeMoveUpdate(e); }, _handleTouchEnd: function (e) { diff --git a/roojs-bootstrap.js b/roojs-bootstrap.js index a3dafa4642..3bfe3b9385 100644 --- a/roojs-bootstrap.js +++ b/roojs-bootstrap.js @@ -1734,27 +1734,27 @@ return false;}return true;},validate:function(){if(this.disabled||this.allowBlan Roo.bootstrap.BezierSignature=function(A){Roo.bootstrap.BezierSignature.superclass.constructor.call(this,A);this.addEvents({"resize":true});};Roo.extend(Roo.bootstrap.BezierSignature,Roo.bootstrap.Component,{curve_data:[],is_empty:true,mouse_btn_down:true,canvas_height:'200px',dot_size:false,min_width:0.5,max_width:2.5,throttle:16,min_distance:5,bg_color:'rgba(0, 0, 0, 0)',dot_color:'black',velocity_filter_weight:0.7,onBegin:false,onEnd:false,getAutoCreate:function(){var A='roo-signature column'; if(this.cls){A+=' '+this.cls;}var B=['lg','md','sm','xs'];for(var i=0;i0&&D[D.length-1];var F=E?B.distanceTo(E)<=this.min_distance:false;var G=C.color;if(!E||!(E&&F)){var H=this.addPoint(B);if(!E){this.drawDot({color:G,point:B});}else if(H){this.drawCurve({color:G,curve:H});}D.push({time:B.time,x:B.x,y:B.y} -);}},strokeEnd:function(e){this.strokeUpdate(e);if(typeof this.onEnd==='function'){this.onEnd(e);}},addPoint:function(A){var B=this._lastPoints;B.push(A);if(B.length>2){if(B.length===3){B.unshift(B[0]);}var C=this.calculateCurveWidths(B[1],B[2]);var D=this.Bezier.fromPoints(B,C,this); -B.shift();return D;}return null;},calculateCurveWidths:function(A,B){var C=this.velocity_filter_weight*B.velocityFrom(A)+(1-this.velocity_filter_weight)*this._lastVelocity;var D=Math.max(this.max_width/(C+1),this.min_width);var E={end:D,start:this._lastWidth} -;this._lastVelocity=C;this._lastWidth=D;return E;},drawDot:function(_a){var A=_a.color,B=_a.point;var C=this.canvasElCtx();var D=typeof this.dot_size==='function'?this.dot_size():this.dot_size;C.beginPath();this.drawCurveSegment(B.x,B.y,D);C.closePath();C.fillStyle=A; -C.fill();},drawCurve:function(_a){var A=_a.color,B=_a.curve;var C=this.canvasElCtx();var D=B.endWidth-B.startWidth;var E=Math.floor(B.length())*2;C.beginPath();C.fillStyle=A;for(var i=0;i0){var C=cx-px;var D=cy-py;B+=Math.sqrt(C*C+D*D); -}px=cx;py=cy;}return B;};Bezier.prototype.point=function(t,A,c1,c2,B){return (A*(1.0-t)*(1.0-t)*(1.0-t))+(3.0*c1*(1.0-t)*(1.0-t)*t)+(3.0*c2*(1.0-t)*t*t)+(B*t*t*t);};return Bezier;}()),throttle:function(fn,A){if(A===void 0){A=250;}var B=0;var C=null;var D; -var E;var F;var G=function(){B=Date.now();C=null;D=fn.apply(E,F);if(!C){E=null;F=[];}};return function H(){var I=[];for(var _i=0;_iA){if(C){clearTimeout(C);C=null; -}B=J;D=fn.apply(E,F);if(!C){E=null;F=[];}}else if(!C){C=window.setTimeout(G,K);}return D;};}}); +A.on('mousemove',this._handleMouseMove,this);Roo.select('html').first().on('mouseup',this._handleMouseUp,this);if(window.PointerEvent){A.on('pointerdown',this._handleMouseDown,this);A.on('pointermove',this._handleMouseMove,this);Roo.select('html').first().on('pointerup',this._handleMouseUp,this); +}if('ontouchstart' in window){A.on('touchstart',this._handleTouchStart,this);A.on('touchmove',this._handleTouchMove,this);A.on('touchend',this._handleTouchEnd,this);}Roo.EventManager.onWindowResize(this.resize,this,true);this.clear();this.resize();},resize:function(){var A=this.canvasEl().dom; +var B=this.canvasElCtx();var C=B.getImageData(0,0,A.width,A.height);A.width=0;var D=window.getComputedStyle?getComputedStyle(this.el.dom,null):this.el.dom.currentStyle;var E=parseInt(D.paddingLeft)||0;var F=parseInt(D.paddingRight)||0;A.width=this.el.dom.clientWidth-E-F; +B.putImageData(C,0,0);},_handleMouseDown:function(e){if(e.browserEvent.which===1){this.mouse_btn_down=true;this.strokeBegin(e);}},_handleMouseMove:function(e){if(this.mouse_btn_down){this.strokeMoveUpdate(e);}},_handleMouseUp:function(e){if(e.browserEvent.which===1&&this.mouse_btn_down){this.mouse_btn_down=false; +this.strokeEnd(e);}},_handleTouchStart:function(e){e.preventDefault();if(e.browserEvent.targetTouches.length===1){this.strokeBegin(e);}},_handleTouchMove:function(e){e.preventDefault();this.strokeMoveUpdate(e);},_handleTouchEnd:function(e){var A=e.target===this.canvasEl().dom; +if(A){e.preventDefault();this.strokeEnd(e);}},reset:function(){this._lastPoints=[];this._lastVelocity=0;this._lastWidth=(this.min_width+this.max_width)/2;this.canvasElCtx().fillStyle=this.dot_color;},strokeMoveUpdate:function(e){this.strokeUpdate(e);if(this.throttle){this.throttle(this.strokeUpdate,this.throttle); +}else{this.strokeUpdate(e);}},strokeBegin:function(e){var A={color:this.dot_color,points:[]};if(typeof this.onBegin==='function'){this.onBegin(e);}this.curve_data.push(A);this.reset();this.strokeUpdate(e);},strokeUpdate:function(e){var A=this.canvasEl().dom.getBoundingClientRect(); +var B=new this.Point(e.xy[0]-A.left,e.xy[1]-A.top,new Date().getTime());var C=this.curve_data[this.curve_data.length-1];var D=C.points;var E=D.length>0&&D[D.length-1];var F=E?B.distanceTo(E)<=this.min_distance:false;var G=C.color;if(!E||!(E&&F)){var H=this.addPoint(B); +if(!E){this.drawDot({color:G,point:B});}else if(H){this.drawCurve({color:G,curve:H});}D.push({time:B.time,x:B.x,y:B.y});}},strokeEnd:function(e){this.strokeUpdate(e);if(typeof this.onEnd==='function'){this.onEnd(e);}},addPoint:function(A){var B=this._lastPoints; +B.push(A);if(B.length>2){if(B.length===3){B.unshift(B[0]);}var C=this.calculateCurveWidths(B[1],B[2]);var D=this.Bezier.fromPoints(B,C,this);B.shift();return D;}return null;},calculateCurveWidths:function(A,B){var C=this.velocity_filter_weight*B.velocityFrom(A)+(1-this.velocity_filter_weight)*this._lastVelocity; +var D=Math.max(this.max_width/(C+1),this.min_width);var E={end:D,start:this._lastWidth};this._lastVelocity=C;this._lastWidth=D;return E;},drawDot:function(_a){var A=_a.color,B=_a.point;var C=this.canvasElCtx();var D=typeof this.dot_size==='function'?this.dot_size():this.dot_size; +C.beginPath();this.drawCurveSegment(B.x,B.y,D);C.closePath();C.fillStyle=A;C.fill();},drawCurve:function(_a){var A=_a.color,B=_a.curve;var C=this.canvasElCtx();var D=B.endWidth-B.startWidth;var E=Math.floor(B.length())*2;C.beginPath();C.fillStyle=A;for(var i=0; +i0){var C=cx-px;var D=cy-py;B+=Math.sqrt(C*C+D*D);}px=cx;py=cy;}return B;};Bezier.prototype.point=function(t,A,c1,c2,B){return (A*(1.0-t)*(1.0-t)*(1.0-t))+(3.0*c1*(1.0-t)*(1.0-t)*t)+(3.0*c2*(1.0-t)*t*t)+(B*t*t*t);};return Bezier;}()),throttle:function(fn,A){if(A===void 0){A=250; +}var B=0;var C=null;var D;var E;var F;var G=function(){B=Date.now();C=null;D=fn.apply(E,F);if(!C){E=null;F=[];}};return function H(){var I=[];for(var _i=0;_iA){if(C){clearTimeout(C); +C=null;}B=J;D=fn.apply(E,F);if(!C){E=null;F=[];}}else if(!C){C=window.setTimeout(G,K);}return D;};}}); -- 2.39.2