From 9d73451d70f4d3df31d85d9ec95d16b676dfbc3b Mon Sep 17 00:00:00 2001 From: "john@roojs.com" Date: Fri, 28 Dec 2018 11:41:52 +0800 Subject: [PATCH] FIX: bezier resizing img lost --- Roo/bootstrap/BezierSignature.js | 11 +++++++- roojs-bootstrap-debug.js | 11 +++++++- roojs-bootstrap.js | 45 ++++++++++++++++---------------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Roo/bootstrap/BezierSignature.js b/Roo/bootstrap/BezierSignature.js index 2dcd4fea17..ae7de0f3d3 100644 --- a/Roo/bootstrap/BezierSignature.js +++ b/Roo/bootstrap/BezierSignature.js @@ -157,6 +157,13 @@ 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 + canvas.width = 0; var style = window.getComputedStyle ? getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle; @@ -164,7 +171,9 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { var padding_left = parseInt(style.paddingLeft) || 0; var padding_right = parseInt(style.paddingRight) || 0; - this.canvasEl().dom.width = this.el.dom.clientWidth - padding_left - padding_right; + canvas.width = this.el.dom.clientWidth - padding_left - padding_right; + + ctx.putImageData(img_data, 0, 0); }, _handleMouseDown: function(e) diff --git a/roojs-bootstrap-debug.js b/roojs-bootstrap-debug.js index 916b94c800..980c3ce65c 100644 --- a/roojs-bootstrap-debug.js +++ b/roojs-bootstrap-debug.js @@ -41658,6 +41658,13 @@ 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 + canvas.width = 0; var style = window.getComputedStyle ? getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle; @@ -41665,7 +41672,9 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component, { var padding_left = parseInt(style.paddingLeft) || 0; var padding_right = parseInt(style.paddingRight) || 0; - this.canvasEl().dom.width = this.el.dom.clientWidth - padding_left - padding_right; + canvas.width = this.el.dom.clientWidth - padding_left - padding_right; + + ctx.putImageData(img_data, 0, 0); }, _handleMouseDown: function(e) diff --git a/roojs-bootstrap.js b/roojs-bootstrap.js index 1663693cd7..a3dafa4642 100644 --- a/roojs-bootstrap.js +++ b/roojs-bootstrap.js @@ -1735,25 +1735,26 @@ Roo.bootstrap.BezierSignature=function(A){Roo.bootstrap.BezierSignature.supercla 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;};}}); +}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