From af45808d071f3ffae1553efbd4c0d73df8af10f3 Mon Sep 17 00:00:00 2001 From: Anton Lavrenov Date: Thu, 14 Nov 2019 11:18:12 -0500 Subject: [PATCH] Fix `path.getClientRect()` calculations for `Konva.Path` --- CHANGELOG.md | 2 ++ konva.js | 14 ++++++++-- konva.min.js | 4 +-- src/shapes/Path.ts | 11 +++++++- test/unit/shapes/Path-test.js | 52 ++++++++++++++++++++++++++++++++++- 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f42adc7..c2de9a7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Not released: +* Fix `path.getClientRect()` calculations for `Konva.Path` +* Fix wrong fire of `click` and `tap` events on stopped drag events. ## 4.0.17 - 2019-11-08 diff --git a/konva.js b/konva.js index 0ed17379..4e6690b1 100644 --- a/konva.js +++ b/konva.js @@ -8,7 +8,7 @@ * Konva JavaScript Framework v4.0.17 * http://konvajs.org/ * Licensed under the MIT - * Date: Tue Nov 12 2019 + * Date: Thu Nov 14 2019 * * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS) * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva) @@ -11524,7 +11524,17 @@ Path.prototype.getSelfRect = function () { var points = []; this.dataArray.forEach(function (data) { - points = points.concat(data.points); + if (data.command === 'A') { + points = points.concat([ + data.points[0] - data.points[2], + data.points[1] - data.points[3], + data.points[0] + data.points[2], + data.points[1] + data.points[3] + ]); + } + else { + points = points.concat(data.points); + } }); var minX = points[0]; var maxX = points[0]; diff --git a/konva.min.js b/konva.min.js index cdb2fa40..645d01c9 100644 --- a/konva.min.js +++ b/konva.min.js @@ -3,10 +3,10 @@ * Konva JavaScript Framework v4.0.17 * http://konvajs.org/ * Licensed under the MIT - * Date: Tue Nov 12 2019 + * Date: Thu Nov 14 2019 * * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS) * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva) * * @license - */var e=Math.PI/180;function t(t){var e=t.toLowerCase(),i=/(chrome)[ /]([\w.]+)/.exec(e)||/(webkit)[ /]([\w.]+)/.exec(e)||/(opera)(?:.*version|)[ /]([\w.]+)/.exec(e)||/(msie) ([\w.]+)/.exec(e)||e.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(e)||[],n=!!t.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i),r=!!t.match(/IEMobile/i);return{browser:i[1]||"",version:i[2]||"0",isIE:function(t){var e=t.indexOf("msie ");if(0>16&255,g:e>>8&255,b:255&e}},getRandomColor:function(){for(var t=(16777215*Math.random()<<0).toString(16);t.length<6;)t="0"+t;return"#"+t},get:function(t,e){return void 0===t?e:t},getRGB:function(t){var e;return t in p?{r:(e=p[t])[0],g:e[1],b:e[2]}:"#"===t[0]?this._hexToRgb(t.substring(1)):"rgb("===t.substr(0,4)?(e=u.exec(t.replace(/ /g,"")),{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)}):{r:0,g:0,b:0}},colorToRGBA:function(t){return t=t||"black",E._namedColorToRBA(t)||E._hex3ColorToRGBA(t)||E._hex6ColorToRGBA(t)||E._rgbColorToRGBA(t)||E._rgbaColorToRGBA(t)||E._hslColorToRGBA(t)},_namedColorToRBA:function(t){var e=p[t.toLowerCase()];return e?{r:e[0],g:e[1],b:e[2],a:1}:null},_rgbColorToRGBA:function(t){if(0===t.indexOf("rgb(")){var e=(t=t.match(/rgb\(([^)]+)\)/)[1]).split(/ *, */).map(Number);return{r:e[0],g:e[1],b:e[2],a:1}}},_rgbaColorToRGBA:function(t){if(0===t.indexOf("rgba(")){var e=(t=t.match(/rgba\(([^)]+)\)/)[1]).split(/ *, */).map(Number);return{r:e[0],g:e[1],b:e[2],a:e[3]}}},_hex6ColorToRGBA:function(t){if("#"===t[0]&&7===t.length)return{r:parseInt(t.slice(1,3),16),g:parseInt(t.slice(3,5),16),b:parseInt(t.slice(5,7),16),a:1}},_hex3ColorToRGBA:function(t){if("#"===t[0]&&4===t.length)return{r:parseInt(t[1]+t[1],16),g:parseInt(t[2]+t[2],16),b:parseInt(t[3]+t[3],16),a:1}},_hslColorToRGBA:function(t){if(/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.test(t)){var e=/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(t),i=(e[0],e.slice(1)),n=Number(i[0])/360,r=Number(i[1])/100,o=Number(i[2])/100,a=void 0,s=void 0,h=void 0;if(0==r)return h=255*o,{r:Math.round(h),g:Math.round(h),b:Math.round(h),a:1};for(var d=2*o-(a=o<.5?o*(1+r):o+r-o*r),l=[0,0,0],c=0;c<3;c++)(s=n+1/3*-(c-1))<0&&s++,1t.x+t.width||e.x+e.widtht.y+t.height||e.y+e.heighte.length){var a=e;e=t,t=a}for(n=0;n=this.parent.children.length)&&E.warn("Unexpected value "+t+" for zIndex property. zIndex is just index of a node in children of its parent. Expected value is from 0 to "+(this.parent.children.length-1)+".");var e=this.index;return this.parent.children.splice(e,1),this.parent.children.splice(t,0,this),this.parent._setChildrenIndices(),this},pt.prototype.getAbsoluteOpacity=function(){return this._getCache(J,this._getAbsoluteOpacity)},pt.prototype._getAbsoluteOpacity=function(){var t=this.opacity(),e=this.getParent();return e&&!e._isUnderCache&&(t*=e.getAbsoluteOpacity()),t},pt.prototype.moveTo=function(t){return this.getParent()!==t&&(this._remove(),t.add(this)),this},pt.prototype.toObject=function(){var t,e,i,n={},r=this.getAttrs();for(t in n.attrs={},r)e=r[t],E.isObject(e)&&!E._isPlainObject(e)&&!E._isArray(e)||(i="function"==typeof this[t]&&this[t],delete r[t],(i?i.call(this):null)!==(r[t]=e)&&(n.attrs[t]=e));return n.className=this.getClassName(),E._prepareToStringify(n)},pt.prototype.toJSON=function(){return JSON.stringify(this.toObject())},pt.prototype.getParent=function(){return this.parent},pt.prototype.findAncestors=function(t,e,i){var n=[];e&&this._isMatch(t)&&n.push(this);for(var r=this.parent;r;){if(r===i)return n;r._isMatch(t)&&n.push(r),r=r.parent}return n},pt.prototype.isAncestorOf=function(t){return!1},pt.prototype.findAncestor=function(t,e,i){return this.findAncestors(t,e,i)[0]},pt.prototype._isMatch=function(t){if(!t)return!1;if("function"==typeof t)return t(this);var e,i,n=t.replace(/ /g,"").split(","),r=n.length;for(e=0;ethis.duration?this.yoyo?(this._time=this.duration,this.reverse()):this.finish():t<0?this.yoyo?(this._time=0,this.play()):this.reset():(this._time=t,this.update())},Re.prototype.getTime=function(){return this._time},Re.prototype.setPosition=function(t){this.prevPos=this._pos,this.propFunc(t),this._pos=t},Re.prototype.getPosition=function(t){return void 0===t&&(t=this._time),this.func(t,this.begin,this._change,this.duration)},Re.prototype.play=function(){this.state=2,this._startTime=this.getTimer()-this._time,this.onEnterFrame(),this.fire("onPlay")},Re.prototype.reverse=function(){this.state=3,this._time=this.duration-this._time,this._startTime=this.getTimer()-this._time,this.onEnterFrame(),this.fire("onReverse")},Re.prototype.seek=function(t){this.pause(),this._time=t,this.update(),this.fire("onSeek")},Re.prototype.reset=function(){this.pause(),this._time=0,this.update(),this.fire("onReset")},Re.prototype.finish=function(){this.pause(),this._time=this.duration,this.update(),this.fire("onFinish")},Re.prototype.update=function(){this.setPosition(this.getPosition(this._time))},Re.prototype.onEnterFrame=function(){var t=this.getTimer()-this._startTime;2===this.state?this.setTime(t):3===this.state&&this.setTime(this.duration-t)},Re.prototype.pause=function(){this.state=1,this.fire("onPause")},Re.prototype.getTimer=function(){return(new Date).getTime()},Re);function Re(t,e,i,n,r,o,a){this.prop=t,this.propFunc=e,this.begin=n,this._pos=n,this.duration=o,this._change=0,this.prevPos=0,this.yoyo=a,this._time=0,this._position=0,this._startTime=0,this._finish=0,this.func=i,this._change=r-this.begin,this.pause()}var Oe=(Le.prototype._addAttr=function(t,e){var i,n,r,o,a,s,h,d,l=this.node,c=l._id;if((r=Le.tweens[c][t])&&delete Le.attrs[c][r][t],i=l.getAttr(t),E._isArray(e))if(n=[],a=Math.max(e.length,i.length),"points"===t&&e.length!==i.length&&(e.length>i.length?(h=i,i=E._prepareArrayForTween(i,e,l.closed())):(s=e,e=E._prepareArrayForTween(e,i,l.closed()))),0===t.indexOf("fill"))for(o=0;othis.dataArray[i].pathLength;)t-=this.dataArray[i].pathLength,++i;if(i===n)return{x:(e=this.dataArray[i-1].points.slice(-2))[0],y:e[1]};if(t<.01)return{x:(e=this.dataArray[i].points.slice(0,2))[0],y:e[1]};var r=this.dataArray[i],o=r.points;switch(r.command){case"L":return ui.getPointOnLine(t,r.start.x,r.start.y,o[0],o[1]);case"C":return ui.getPointOnCubicBezier(t/r.pathLength,r.start.x,r.start.y,o[0],o[1],o[2],o[3],o[4],o[5]);case"Q":return ui.getPointOnQuadraticBezier(t/r.pathLength,r.start.x,r.start.y,o[0],o[1],o[2],o[3]);case"A":var a=o[0],s=o[1],h=o[2],d=o[3],l=o[4],c=o[5],p=o[6];return l+=c*t/r.pathLength,ui.getPointOnEllipticalArc(a,s,h,d,l,p)}return null},ui.getLineLength=function(t,e,i,n){return Math.sqrt((i-t)*(i-t)+(n-e)*(n-e))},ui.getPointOnLine=function(t,e,i,n,r,o,a){void 0===o&&(o=e),void 0===a&&(a=i);var s=(r-i)/(n-e+1e-8),h=Math.sqrt(t*t/(1+s*s));n>>1,k=_.slice(0,1+P),T=this._getTextWidth(k)+v;T<=d?(b=1+P,w=k+(g?"…":""),C=T):x=P}if(!w)break;if(f){var A,M=_[w.length];0<(A=(" "===M||"-"===M)&&C<=d?w.length:Math.max(w.lastIndexOf(" "),w.lastIndexOf("-"))+1)&&(b=A,w=w.slice(0,b),C=this._getTextWidth(w))}if(w=w.trimRight(),this._addTextLine(w),i=Math.max(i,C),c+=n,!u||s&&le?g=pi.getPointOnLine(e,f.x,f.y,v.points[0],v.points[1],f.x,f.y):v=void 0;break;case"A":var a=v.points[4],s=v.points[5],h=v.points[4]+s;0===m?m=a+1e-8:iv.pathLength?1e-8:e/v.pathLength:ithis.findOne(".bottom-right").x()?-1:1,p=this.findOne(".top-left").y()>this.findOne(".bottom-right").y()?-1:1;e=n*this.cos*c,i=n*this.sin*p,this.findOne(".top-left").x(this.findOne(".bottom-right").x()-e-2*l),this.findOne(".top-left").y(this.findOne(".bottom-right").y()-i-2*l)}}else if("top-center"===this._movingAnchorName)this.findOne(".top-left").y(r.y());else if("top-right"===this._movingAnchorName){d&&(n=Math.sqrt(Math.pow(r.x()-this.findOne(".bottom-left").x()-2*l,2)+Math.pow(this.findOne(".bottom-left").y()-r.y()-2*l,2)),c=this.findOne(".top-right").x()this.findOne(".bottom-left").y()?-1:1,e=n*this.cos*c,i=n*this.sin*p,this.findOne(".top-right").x(e+l),this.findOne(".top-right").y(this.findOne(".bottom-left").y()-i-2*l));var u=r.position();this.findOne(".top-left").y(u.y),this.findOne(".bottom-right").x(u.x)}else if("middle-left"===this._movingAnchorName)this.findOne(".top-left").x(r.x());else if("middle-right"===this._movingAnchorName)this.findOne(".bottom-right").x(r.x());else if("bottom-left"===this._movingAnchorName)d&&(n=Math.sqrt(Math.pow(this.findOne(".top-right").x()-r.x()-2*l,2)+Math.pow(r.y()-this.findOne(".top-right").y()-2*l,2)),c=this.findOne(".top-right").x()this.findOne(".bottom-right").x()?-1:1,p=this.findOne(".top-left").y()>this.findOne(".bottom-right").y()?-1:1,e=n*this.cos*c,i=n*this.sin*p,this.findOne(".bottom-right").x(e+l),this.findOne(".bottom-right").y(i+l));else if("rotater"===this._movingAnchorName){var f=this._getNodeRect();e=r.x()-f.width/2,i=-r.y()+f.height/2;var g=Math.atan2(-i,e)+Math.PI/2;f.height<0&&(g-=Math.PI);for(var v=I.getAngle(this.rotation()),y=E._radToDeg(v)+E._radToDeg(g),m=I.getAngle(this.getNode().rotation()),_=E._degToRad(y),S=this.rotationSnaps(),b=0;b>N,0!==C?(C=255/C,k[s]=(d*z>>N)*C,k[s+1]=(l*z>>N)*C,k[s+2]=(c*z>>N)*C):k[s]=k[s+1]=k[s+2]=0,d-=u,l-=f,c-=g,p-=v,u-=F.r,f-=F.g,g-=F.b,v-=F.a,o=h+((o=i+e+1)>N,0>N)*C,k[o+1]=(l*z>>N)*C,k[o+2]=(c*z>>N)*C):k[o]=k[o+1]=k[o+2]=0,d-=u,l-=f,c-=g,p-=v,u-=F.r,f-=F.g,g-=F.b,v-=F.a,o=i+((o=n+O)>16&255,g:e>>8&255,b:255&e}},getRandomColor:function(){for(var t=(16777215*Math.random()<<0).toString(16);t.length<6;)t="0"+t;return"#"+t},get:function(t,e){return void 0===t?e:t},getRGB:function(t){var e;return t in p?{r:(e=p[t])[0],g:e[1],b:e[2]}:"#"===t[0]?this._hexToRgb(t.substring(1)):"rgb("===t.substr(0,4)?(e=u.exec(t.replace(/ /g,"")),{r:parseInt(e[1],10),g:parseInt(e[2],10),b:parseInt(e[3],10)}):{r:0,g:0,b:0}},colorToRGBA:function(t){return t=t||"black",E._namedColorToRBA(t)||E._hex3ColorToRGBA(t)||E._hex6ColorToRGBA(t)||E._rgbColorToRGBA(t)||E._rgbaColorToRGBA(t)||E._hslColorToRGBA(t)},_namedColorToRBA:function(t){var e=p[t.toLowerCase()];return e?{r:e[0],g:e[1],b:e[2],a:1}:null},_rgbColorToRGBA:function(t){if(0===t.indexOf("rgb(")){var e=(t=t.match(/rgb\(([^)]+)\)/)[1]).split(/ *, */).map(Number);return{r:e[0],g:e[1],b:e[2],a:1}}},_rgbaColorToRGBA:function(t){if(0===t.indexOf("rgba(")){var e=(t=t.match(/rgba\(([^)]+)\)/)[1]).split(/ *, */).map(Number);return{r:e[0],g:e[1],b:e[2],a:e[3]}}},_hex6ColorToRGBA:function(t){if("#"===t[0]&&7===t.length)return{r:parseInt(t.slice(1,3),16),g:parseInt(t.slice(3,5),16),b:parseInt(t.slice(5,7),16),a:1}},_hex3ColorToRGBA:function(t){if("#"===t[0]&&4===t.length)return{r:parseInt(t[1]+t[1],16),g:parseInt(t[2]+t[2],16),b:parseInt(t[3]+t[3],16),a:1}},_hslColorToRGBA:function(t){if(/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.test(t)){var e=/hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(t),i=(e[0],e.slice(1)),n=Number(i[0])/360,r=Number(i[1])/100,o=Number(i[2])/100,a=void 0,s=void 0,h=void 0;if(0==r)return h=255*o,{r:Math.round(h),g:Math.round(h),b:Math.round(h),a:1};for(var d=2*o-(a=o<.5?o*(1+r):o+r-o*r),l=[0,0,0],c=0;c<3;c++)(s=n+1/3*-(c-1))<0&&s++,1t.x+t.width||e.x+e.widtht.y+t.height||e.y+e.heighte.length){var a=e;e=t,t=a}for(n=0;n=this.parent.children.length)&&E.warn("Unexpected value "+t+" for zIndex property. zIndex is just index of a node in children of its parent. Expected value is from 0 to "+(this.parent.children.length-1)+".");var e=this.index;return this.parent.children.splice(e,1),this.parent.children.splice(t,0,this),this.parent._setChildrenIndices(),this},pt.prototype.getAbsoluteOpacity=function(){return this._getCache(J,this._getAbsoluteOpacity)},pt.prototype._getAbsoluteOpacity=function(){var t=this.opacity(),e=this.getParent();return e&&!e._isUnderCache&&(t*=e.getAbsoluteOpacity()),t},pt.prototype.moveTo=function(t){return this.getParent()!==t&&(this._remove(),t.add(this)),this},pt.prototype.toObject=function(){var t,e,i,n={},r=this.getAttrs();for(t in n.attrs={},r)e=r[t],E.isObject(e)&&!E._isPlainObject(e)&&!E._isArray(e)||(i="function"==typeof this[t]&&this[t],delete r[t],(i?i.call(this):null)!==(r[t]=e)&&(n.attrs[t]=e));return n.className=this.getClassName(),E._prepareToStringify(n)},pt.prototype.toJSON=function(){return JSON.stringify(this.toObject())},pt.prototype.getParent=function(){return this.parent},pt.prototype.findAncestors=function(t,e,i){var n=[];e&&this._isMatch(t)&&n.push(this);for(var r=this.parent;r;){if(r===i)return n;r._isMatch(t)&&n.push(r),r=r.parent}return n},pt.prototype.isAncestorOf=function(t){return!1},pt.prototype.findAncestor=function(t,e,i){return this.findAncestors(t,e,i)[0]},pt.prototype._isMatch=function(t){if(!t)return!1;if("function"==typeof t)return t(this);var e,i,n=t.replace(/ /g,"").split(","),r=n.length;for(e=0;ethis.duration?this.yoyo?(this._time=this.duration,this.reverse()):this.finish():t<0?this.yoyo?(this._time=0,this.play()):this.reset():(this._time=t,this.update())},Re.prototype.getTime=function(){return this._time},Re.prototype.setPosition=function(t){this.prevPos=this._pos,this.propFunc(t),this._pos=t},Re.prototype.getPosition=function(t){return void 0===t&&(t=this._time),this.func(t,this.begin,this._change,this.duration)},Re.prototype.play=function(){this.state=2,this._startTime=this.getTimer()-this._time,this.onEnterFrame(),this.fire("onPlay")},Re.prototype.reverse=function(){this.state=3,this._time=this.duration-this._time,this._startTime=this.getTimer()-this._time,this.onEnterFrame(),this.fire("onReverse")},Re.prototype.seek=function(t){this.pause(),this._time=t,this.update(),this.fire("onSeek")},Re.prototype.reset=function(){this.pause(),this._time=0,this.update(),this.fire("onReset")},Re.prototype.finish=function(){this.pause(),this._time=this.duration,this.update(),this.fire("onFinish")},Re.prototype.update=function(){this.setPosition(this.getPosition(this._time))},Re.prototype.onEnterFrame=function(){var t=this.getTimer()-this._startTime;2===this.state?this.setTime(t):3===this.state&&this.setTime(this.duration-t)},Re.prototype.pause=function(){this.state=1,this.fire("onPause")},Re.prototype.getTimer=function(){return(new Date).getTime()},Re);function Re(t,e,i,n,r,o,a){this.prop=t,this.propFunc=e,this.begin=n,this._pos=n,this.duration=o,this._change=0,this.prevPos=0,this.yoyo=a,this._time=0,this._position=0,this._startTime=0,this._finish=0,this.func=i,this._change=r-this.begin,this.pause()}var Oe=(Le.prototype._addAttr=function(t,e){var i,n,r,o,a,s,h,d,l=this.node,c=l._id;if((r=Le.tweens[c][t])&&delete Le.attrs[c][r][t],i=l.getAttr(t),E._isArray(e))if(n=[],a=Math.max(e.length,i.length),"points"===t&&e.length!==i.length&&(e.length>i.length?(h=i,i=E._prepareArrayForTween(i,e,l.closed())):(s=e,e=E._prepareArrayForTween(e,i,l.closed()))),0===t.indexOf("fill"))for(o=0;othis.dataArray[i].pathLength;)t-=this.dataArray[i].pathLength,++i;if(i===n)return{x:(e=this.dataArray[i-1].points.slice(-2))[0],y:e[1]};if(t<.01)return{x:(e=this.dataArray[i].points.slice(0,2))[0],y:e[1]};var r=this.dataArray[i],o=r.points;switch(r.command){case"L":return ui.getPointOnLine(t,r.start.x,r.start.y,o[0],o[1]);case"C":return ui.getPointOnCubicBezier(t/r.pathLength,r.start.x,r.start.y,o[0],o[1],o[2],o[3],o[4],o[5]);case"Q":return ui.getPointOnQuadraticBezier(t/r.pathLength,r.start.x,r.start.y,o[0],o[1],o[2],o[3]);case"A":var a=o[0],s=o[1],h=o[2],d=o[3],l=o[4],c=o[5],p=o[6];return l+=c*t/r.pathLength,ui.getPointOnEllipticalArc(a,s,h,d,l,p)}return null},ui.getLineLength=function(t,e,i,n){return Math.sqrt((i-t)*(i-t)+(n-e)*(n-e))},ui.getPointOnLine=function(t,e,i,n,r,o,a){void 0===o&&(o=e),void 0===a&&(a=i);var s=(r-i)/(n-e+1e-8),h=Math.sqrt(t*t/(1+s*s));n>>1,k=_.slice(0,1+P),T=this._getTextWidth(k)+v;T<=d?(b=1+P,w=k+(g?"…":""),C=T):x=P}if(!w)break;if(f){var A,M=_[w.length];0<(A=(" "===M||"-"===M)&&C<=d?w.length:Math.max(w.lastIndexOf(" "),w.lastIndexOf("-"))+1)&&(b=A,w=w.slice(0,b),C=this._getTextWidth(w))}if(w=w.trimRight(),this._addTextLine(w),i=Math.max(i,C),c+=n,!u||s&&le?g=pi.getPointOnLine(e,f.x,f.y,v.points[0],v.points[1],f.x,f.y):v=void 0;break;case"A":var a=v.points[4],s=v.points[5],h=v.points[4]+s;0===m?m=a+1e-8:iv.pathLength?1e-8:e/v.pathLength:ithis.findOne(".bottom-right").x()?-1:1,p=this.findOne(".top-left").y()>this.findOne(".bottom-right").y()?-1:1;e=n*this.cos*c,i=n*this.sin*p,this.findOne(".top-left").x(this.findOne(".bottom-right").x()-e-2*l),this.findOne(".top-left").y(this.findOne(".bottom-right").y()-i-2*l)}}else if("top-center"===this._movingAnchorName)this.findOne(".top-left").y(r.y());else if("top-right"===this._movingAnchorName){d&&(n=Math.sqrt(Math.pow(r.x()-this.findOne(".bottom-left").x()-2*l,2)+Math.pow(this.findOne(".bottom-left").y()-r.y()-2*l,2)),c=this.findOne(".top-right").x()this.findOne(".bottom-left").y()?-1:1,e=n*this.cos*c,i=n*this.sin*p,this.findOne(".top-right").x(e+l),this.findOne(".top-right").y(this.findOne(".bottom-left").y()-i-2*l));var u=r.position();this.findOne(".top-left").y(u.y),this.findOne(".bottom-right").x(u.x)}else if("middle-left"===this._movingAnchorName)this.findOne(".top-left").x(r.x());else if("middle-right"===this._movingAnchorName)this.findOne(".bottom-right").x(r.x());else if("bottom-left"===this._movingAnchorName)d&&(n=Math.sqrt(Math.pow(this.findOne(".top-right").x()-r.x()-2*l,2)+Math.pow(r.y()-this.findOne(".top-right").y()-2*l,2)),c=this.findOne(".top-right").x()this.findOne(".bottom-right").x()?-1:1,p=this.findOne(".top-left").y()>this.findOne(".bottom-right").y()?-1:1,e=n*this.cos*c,i=n*this.sin*p,this.findOne(".bottom-right").x(e+l),this.findOne(".bottom-right").y(i+l));else if("rotater"===this._movingAnchorName){var f=this._getNodeRect();e=r.x()-f.width/2,i=-r.y()+f.height/2;var g=Math.atan2(-i,e)+Math.PI/2;f.height<0&&(g-=Math.PI);for(var v=I.getAngle(this.rotation()),y=E._radToDeg(v)+E._radToDeg(g),m=I.getAngle(this.getNode().rotation()),_=E._degToRad(y),S=this.rotationSnaps(),b=0;b>N,0!==C?(C=255/C,k[s]=(d*z>>N)*C,k[s+1]=(l*z>>N)*C,k[s+2]=(c*z>>N)*C):k[s]=k[s+1]=k[s+2]=0,d-=u,l-=f,c-=g,p-=v,u-=F.r,f-=F.g,g-=F.b,v-=F.a,o=h+((o=i+e+1)>N,0>N)*C,k[o+1]=(l*z>>N)*C,k[o+2]=(c*z>>N)*C):k[o]=k[o+1]=k[o+2]=0,d-=u,l-=f,c-=g,p-=v,u-=F.r,f-=F.g,g-=F.b,v-=F.a,o=i+((o=n+O) { getSelfRect() { var points = []; this.dataArray.forEach(function(data) { - points = points.concat(data.points); + if (data.command === 'A') { + points = points.concat([ + data.points[0] - data.points[2], + data.points[1] - data.points[3], + data.points[0] + data.points[2], + data.points[1] + data.points[3] + ]) + } else { + points = points.concat(data.points); + } }); var minX = points[0]; var maxX = points[0]; diff --git a/test/unit/shapes/Path-test.js b/test/unit/shapes/Path-test.js index 3412cc2b..5e4f0783 100644 --- a/test/unit/shapes/Path-test.js +++ b/test/unit/shapes/Path-test.js @@ -1169,6 +1169,56 @@ suite('Path', function() { layer.add(path); var rect = path.getClientRect(); - assert.deepEqual(rect, { x: -4, y: -3, width: 107, height: 124 }); + var back = new Konva.Rect({ + x: rect.x, + y: rect.y, + width: rect.width, + height: rect.height, + stroke: 'red' + }); + layer.add(back); + layer.draw(); + + assert.deepEqual(rect, {x: 8, y: 65, width: 95, height: 56}); + }); + + test('getClientRect for arc', function() { + var stage = addStage(); + var layer = new Konva.Layer(); + stage.add(layer); + + var path = new Konva.Path({ + data: + 'M -12274.95703125 17975.16015625 C -12271.4072265625 17975.16015625 -12268.017578125 17974.345703125 -12264.8837890625 17972.740234375 C -12261.892578125 17971.208984375 -12259.24609375 17968.97265625 -12257.2314453125 17966.2734375 L -12256.775390625 17965.662109375 L -12256.0654296875 17965.939453125 C -12253.494140625 17966.947265625 -12250.7783203125 17967.45703125 -12247.9921875 17967.45703125 C -12245.01171875 17967.45703125 -12242.1201171875 17966.873046875 -12239.396484375 17965.720703125 C -12236.765625 17964.607421875 -12234.4013671875 17963.013671875 -12232.3701171875 17960.982421875 C -12230.3388671875 17958.953125 -12228.7431640625 17956.587890625 -12227.62890625 17953.95703125 C -12226.4755859375 17951.232421875 -12225.890625 17948.337890625 -12225.890625 17945.35546875 C -12225.890625 17941.30859375 -12226.99609375 17937.349609375 -12229.0888671875 17933.90625 C -12231.12109375 17930.5625 -12234.01171875 17927.802734375 -12237.447265625 17925.927734375 L -12237.849609375 17925.708984375 L -12237.9462890625 17925.26171875 C -12238.99609375 17920.408203125 -12241.708984375 17915.994140625 -12245.5830078125 17912.83203125 C -12247.5146484375 17911.2578125 -12249.6748046875 17910.029296875 -12252.0068359375 17909.18359375 C -12254.41796875 17908.306640625 -12256.9541015625 17907.86328125 -12259.54296875 17907.86328125 C -12263.171875 17907.86328125 -12266.7568359375 17908.75390625 -12269.9111328125 17910.44140625 L -12270.556640625 17910.78515625 L -12271.0810546875 17910.275390625 C -12275.2353515625 17906.2265625 -12280.7138671875 17903.99609375 -12286.5078125 17903.99609375 C -12288.9462890625 17903.99609375 -12291.34375 17904.390625 -12293.630859375 17905.171875 C -12295.84375 17905.92578125 -12297.916015625 17907.0234375 -12299.791015625 17908.4375 C -12301.646484375 17909.8359375 -12303.2646484375 17911.509765625 -12304.599609375 17913.41015625 C -12305.9541015625 17915.3359375 -12306.984375 17917.44921875 -12307.6640625 17919.69140625 L -12307.8193359375 17920.203125 L -12308.3310546875 17920.359375 C -12310.5712890625 17921.0390625 -12312.6826171875 17922.068359375 -12314.6044921875 17923.421875 C -12316.501953125 17924.755859375 -12318.1708984375 17926.37109375 -12319.56640625 17928.224609375 C -12322.466796875 17932.078125 -12324 17936.671875 -12324 17941.51171875 C -12324 17944.498046875 -12323.416015625 17947.392578125 -12322.2646484375 17950.1171875 C -12321.15234375 17952.75 -12319.55859375 17955.11328125 -12317.529296875 17957.142578125 C -12315.5 17959.171875 -12313.1376953125 17960.765625 -12310.505859375 17961.876953125 C -12307.7822265625 17963.029296875 -12304.8876953125 17963.61328125 -12301.90234375 17963.61328125 C -12299.7900390625 17963.61328125 -12297.71875 17963.322265625 -12295.744140625 17962.74609375 L -12294.95703125 17962.517578125 L -12294.578125 17963.24609375 C -12292.7373046875 17966.78125 -12289.9716796875 17969.759765625 -12286.580078125 17971.861328125 C -12283.095703125 17974.01953125 -12279.076171875 17975.16015625 -12274.95703125 17975.16015625 M -12274.95703125 17976.16015625 C -12283.8671875 17976.16015625 -12291.609375 17971.11328125 -12295.46484375 17963.70703125 C -12297.50390625 17964.30078125 -12299.66796875 17964.61328125 -12301.90234375 17964.61328125 C -12314.6640625 17964.61328125 -12325 17954.27734375 -12325 17941.51171875 C -12325 17931.08203125 -12318.1015625 17922.27734375 -12308.62109375 17919.40234375 C -12305.74609375 17909.91015625 -12296.92578125 17902.99609375 -12286.5078125 17902.99609375 C -12280.234375 17902.99609375 -12274.54296875 17905.50390625 -12270.3828125 17909.55859375 C -12267.15234375 17907.83203125 -12263.46484375 17906.86328125 -12259.54296875 17906.86328125 C -12248.48046875 17906.86328125 -12239.21875 17914.65234375 -12236.96875 17925.05078125 C -12229.78125 17928.97265625 -12224.890625 17936.58984375 -12224.890625 17945.35546875 C -12224.890625 17958.11328125 -12235.25 17968.45703125 -12247.9921875 17968.45703125 C -12250.96875 17968.45703125 -12253.81640625 17967.89453125 -12256.4296875 17966.87109375 C -12260.640625 17972.51171875 -12267.37109375 17976.16015625 -12274.95703125 17976.16015625 Z', + fill: 'black', + stroke: 'blue', + strokeWidth: 10 + }); + layer.add(path); + var rect = path.getClientRect(); + + var scale = stage.height() / rect.height / 2; + + + path.x(-rect.x * scale); + path.y(-rect.y * scale); + path.scaleX(scale); + path.scaleY(scale); + + + + rect = path.getClientRect(); + + var back = new Konva.Rect({ + x: rect.x, + y: rect.y, + width: rect.width, + height: rect.height, + stroke: 'red' + }); + layer.add(back); + layer.draw(); + + assert.deepEqual(rect, {x: 0, y: 0, width: 132.53012048192795, height: 100}); }); });