From 8937741c31d37e9903824c5eda650220d8821be6 Mon Sep 17 00:00:00 2001 From: Anton Lavrenov Date: Fri, 10 Apr 2020 16:00:25 -0500 Subject: [PATCH] fix some transformer bugs --- konva.js | 50 ++++-------- konva.min.js | 4 +- src/Node.ts | 10 ++- src/shapes/Transformer.ts | 39 ++------- test/unit/shapes/Transformer-test.js | 116 ++++++++++++++------------- 5 files changed, 91 insertions(+), 128 deletions(-) diff --git a/konva.js b/konva.js index d92bbf90..55631f8b 100644 --- a/konva.js +++ b/konva.js @@ -8,7 +8,7 @@ * Konva JavaScript Framework v4.2.2 * http://konvajs.org/ * Licensed under the MIT - * Date: Wed Apr 08 2020 + * Date: Fri Apr 10 2020 * * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS) * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva) @@ -2682,10 +2682,12 @@ * when the logic for a cached result depends on ancestor propagation, use this * method to clear self and children cache */ - Node.prototype._clearSelfAndDescendantCache = function (attr) { + Node.prototype._clearSelfAndDescendantCache = function (attr, forceEvent) { this._clearCache(attr); // trigger clear cache, so transformer can use it - this.fire('clearCache'); + if (forceEvent) { + this.fire('clearCache'); + } // skip clearing if node is cached with canvas // for performance reasons !!! if (this.isCached()) { @@ -2693,7 +2695,7 @@ } if (this.children) { this.children.each(function (node) { - node._clearSelfAndDescendantCache(attr); + node._clearSelfAndDescendantCache(attr, true); }); } }; @@ -3474,8 +3476,8 @@ x: this.attrs.x + it.getTranslation().x, y: this.attrs.y + it.getTranslation().y }; - this.setPosition({ x: pos.x, y: pos.y }); this._setTransform(origTrans); + this.setPosition({ x: pos.x, y: pos.y }); return this; }; Node.prototype._setTransform = function (trans) { @@ -14683,9 +14685,7 @@ 'offsetXChange', 'offsetYChange', 'transformsEnabledChange', - 'strokeWidthChange', - // listen to cache changes - 'clearCache' + 'strokeWidthChange' ] .map(function (e) { return e + ("." + EVENTS_NAME); }) .join(' '); @@ -14966,9 +14966,6 @@ return this._nodes && this._nodes[0]; }; Transformer.prototype.drawScene = function (can, top, caching) { - if (!this._cache.get(NODES_RECT)) { - this.update(); - } return _super.prototype.drawScene.call(this, can, top, caching); }; // _attachTo(node) => { @@ -14998,9 +14995,10 @@ }; node.on(additionalEvents, onChange); node.on(TRANSFORM_CHANGE_STR$1, onChange); - node.on("xChange." + EVENTS_NAME + " yChange." + EVENTS_NAME, function () { + node.on("clearCache." + EVENTS_NAME, function () { _this._resetTransformCache(); }); + node.on("xChange." + EVENTS_NAME + " yChange." + EVENTS_NAME, onChange); }); this._resetTransformCache(); // we may need it if we set node in initial props @@ -15340,7 +15338,7 @@ var reverseX = this.findOne('.top-right').x() < this.findOne('.bottom-left').x() ? -1 : 1; - var reverseY = this.findOne('.bottom-right').y() < this.findOne('.top-left').y() + var reverseY = this.findOne('.bottom-left').y() < this.findOne('.top-right').y() ? -1 : 1; x = newHypotenuse * this.cos * reverseX; @@ -15600,15 +15598,6 @@ var _this = this; var attrs = this._getNodeRect(); this.rotation(Util._getRotation(attrs.rotation)); - var node = this.getNode(); - var scale = { x: 1, y: 1 }; - // if (node && node.getParent()) { - // scale = node.getParent().getAbsoluteScale(); - // } - var invertedScale = { - x: 1 / scale.x, - y: 1 / scale.y - }; var width = attrs.width; var height = attrs.height; var enabledAnchors = this.enabledAnchors(); @@ -15632,14 +15621,12 @@ y: 0, offsetX: anchorSize / 2 + padding, offsetY: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0 }); this.findOne('.top-center').setAttrs({ x: width / 2, y: 0, offsetY: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0 }); this.findOne('.top-right').setAttrs({ @@ -15647,21 +15634,18 @@ y: 0, offsetX: anchorSize / 2 - padding, offsetY: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0 }); this.findOne('.middle-left').setAttrs({ x: 0, y: height / 2, offsetX: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0 }); this.findOne('.middle-right').setAttrs({ x: width, y: height / 2, offsetX: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0 }); this.findOne('.bottom-left').setAttrs({ @@ -15669,14 +15653,12 @@ y: height, offsetX: anchorSize / 2 + padding, offsetY: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0 }); this.findOne('.bottom-center').setAttrs({ x: width / 2, y: height, offsetY: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0 }); this.findOne('.bottom-right').setAttrs({ @@ -15684,20 +15666,16 @@ y: height, offsetX: anchorSize / 2 - padding, offsetY: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0 }); - var scaledRotateAnchorOffset = -this.rotateAnchorOffset() * Math.abs(invertedScale.y); this.findOne('.rotater').setAttrs({ x: width / 2, - y: scaledRotateAnchorOffset * Util._sign(height) - padding, - scale: invertedScale, + y: -this.rotateAnchorOffset() * Util._sign(height) - padding, visible: this.rotateEnabled() }); this.findOne('.back').setAttrs({ - width: width * scale.x, - height: height * scale.y, - scale: invertedScale, + width: width, + height: height, visible: this.borderEnabled(), stroke: this.borderStroke(), strokeWidth: this.borderStrokeWidth(), diff --git a/konva.min.js b/konva.min.js index 0365b8d4..23d23863 100644 --- a/konva.min.js +++ b/konva.min.js @@ -3,10 +3,10 @@ * Konva JavaScript Framework v4.2.2 * http://konvajs.org/ * Licensed under the MIT - * Date: Wed Apr 08 2020 + * Date: Fri Apr 10 2020 * * 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",L._namedColorToRBA(t)||L._hex3ColorToRGBA(t)||L._hex6ColorToRGBA(t)||L._rgbColorToRGBA(t)||L._rgbaColorToRGBA(t)||L._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 l=2*o-(a=o<.5?o*(1+r):o+r-o*r),d=[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)&&L.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(Z,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],L.isObject(e)&&!L._isPlainObject(e)&&!L._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(),L._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=(Ie.prototype._addAttr=function(t,e){var i,n,r,o,a,s,h,l,d=this.node,c=d._id;if((r=Ie.tweens[c][t])&&delete Ie.attrs[c][r][t],i=d.getAttr(t),L._isArray(e))if(n=[],a=Math.max(e.length,i.length),"points"===t&&e.length!==i.length&&(e.length>i.length?(h=i,i=L._prepareArrayForTween(i,e,d.closed())):(s=e,e=L._prepareArrayForTween(e,i,d.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],l=o[3],d=o[4],c=o[5],p=o[6];return d+=c*t/r.pathLength,ui.getPointOnEllipticalArc(a,s,h,l,d,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<=l?(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<=l?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&&de?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-0),this.findOne(".top-left").y(this.findOne(".bottom-right").y()-i-0)}}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()-0,2)+Math.pow(this.findOne(".bottom-left").y()-r.y()-0,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+0),this.findOne(".top-right").y(this.findOne(".bottom-left").y()-i-0));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()-0,2)+Math.pow(r.y()-this.findOne(".top-right").y()-0,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+0),this.findOne(".bottom-right").y(i+0));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=L._radToDeg(v)+L._radToDeg(g),m=(I.getAngle(this.getNode().rotation()),L._degToRad(y)),_=this.rotationSnaps(),S=I.getAngle(this.rotationSnapTolerance()),b=0;b<_.length;b++){var x=I.getAngle(_[b]);Math.abs(x-L._degToRad(y))%(2*Math.PI)>z,0!==C?(C=255/C,k[s]=(l*N>>z)*C,k[s+1]=(d*N>>z)*C,k[s+2]=(c*N>>z)*C):k[s]=k[s+1]=k[s+2]=0,l-=u,d-=f,c-=g,p-=v,u-=F.r,f-=F.g,g-=F.b,v-=F.a,o=h+((o=i+e+1)>z,0>z)*C,k[o+1]=(d*N>>z)*C,k[o+2]=(c*N>>z)*C):k[o]=k[o+1]=k[o+2]=0,l-=u,d-=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",L._namedColorToRBA(t)||L._hex3ColorToRGBA(t)||L._hex6ColorToRGBA(t)||L._rgbColorToRGBA(t)||L._rgbaColorToRGBA(t)||L._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)&&L.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(Z,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],L.isObject(e)&&!L._isPlainObject(e)&&!L._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(),L._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=(Ie.prototype._addAttr=function(t,e){var i,n,r,o,a,s,h,d,l=this.node,c=l._id;if((r=Ie.tweens[c][t])&&delete Ie.attrs[c][r][t],i=l.getAttr(t),L._isArray(e))if(n=[],a=Math.max(e.length,i.length),"points"===t&&e.length!==i.length&&(e.length>i.length?(h=i,i=L._prepareArrayForTween(i,e,l.closed())):(s=e,e=L._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-0),this.findOne(".top-left").y(this.findOne(".bottom-right").y()-i-0)}}else if("top-center"===this._movingAnchorName)this.findOne(".top-left").y(r.y());else if("top-right"===this._movingAnchorName){l&&(n=Math.sqrt(Math.pow(r.x()-this.findOne(".bottom-left").x()-0,2)+Math.pow(this.findOne(".bottom-left").y()-r.y()-0,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+0),this.findOne(".top-right").y(this.findOne(".bottom-left").y()-i-0));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)l&&(n=Math.sqrt(Math.pow(this.findOne(".top-right").x()-r.x()-0,2)+Math.pow(r.y()-this.findOne(".top-right").y()-0,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+0),this.findOne(".bottom-right").y(i+0));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=L._radToDeg(v)+L._radToDeg(g),m=(I.getAngle(this.getNode().rotation()),L._degToRad(y)),_=this.rotationSnaps(),S=I.getAngle(this.rotationSnapTolerance()),b=0;b<_.length;b++){var x=I.getAngle(_[b]);Math.abs(x-L._degToRad(y))%(2*Math.PI)>z,0!==C?(C=255/C,k[s]=(d*N>>z)*C,k[s+1]=(l*N>>z)*C,k[s+2]=(c*N>>z)*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)>z,0>z)*C,k[o+1]=(l*N>>z)*C,k[o+2]=(c*N>>z)*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) { * when the logic for a cached result depends on ancestor propagation, use this * method to clear self and children cache */ - _clearSelfAndDescendantCache(attr?: string) { + _clearSelfAndDescendantCache(attr?: string, forceEvent?: boolean) { this._clearCache(attr); // trigger clear cache, so transformer can use it - this.fire('clearCache'); + if (forceEvent) { + this.fire('clearCache'); + } // skip clearing if node is cached with canvas // for performance reasons !!! @@ -277,7 +279,7 @@ export abstract class Node { } if (this.children) { this.children.each(function(node) { - node._clearSelfAndDescendantCache(attr); + node._clearSelfAndDescendantCache(attr, true); }); } } @@ -1208,8 +1210,8 @@ export abstract class Node { y: this.attrs.y + it.getTranslation().y }; - this.setPosition({ x: pos.x, y: pos.y }); this._setTransform(origTrans); + this.setPosition({ x: pos.x, y: pos.y }); return this; } diff --git a/src/shapes/Transformer.ts b/src/shapes/Transformer.ts index 89195371..d8e52102 100644 --- a/src/shapes/Transformer.ts +++ b/src/shapes/Transformer.ts @@ -70,9 +70,7 @@ var TRANSFORM_CHANGE_STR = [ 'offsetXChange', 'offsetYChange', 'transformsEnabledChange', - 'strokeWidthChange', - // listen to cache changes - 'clearCache' + 'strokeWidthChange' ] .map(e => e + `.${EVENTS_NAME}`) .join(' '); @@ -401,9 +399,6 @@ export class Transformer extends Group { } drawScene(can?, top?, caching?) { - if (!this._cache.get(NODES_RECT)) { - this.update(); - } return super.drawScene(can, top, caching); } // _attachTo(node) => { @@ -432,9 +427,10 @@ export class Transformer extends Group { }; node.on(additionalEvents, onChange); node.on(TRANSFORM_CHANGE_STR, onChange); - node.on(`xChange.${EVENTS_NAME} yChange.${EVENTS_NAME}`, () => { + node.on(`clearCache.${EVENTS_NAME}`, () => { this._resetTransformCache(); }); + node.on(`xChange.${EVENTS_NAME} yChange.${EVENTS_NAME}`, onChange); }); this._resetTransformCache(); // we may need it if we set node in initial props @@ -869,7 +865,7 @@ export class Transformer extends Group { : 1; var reverseY = - this.findOne('.bottom-right').y() < this.findOne('.top-left').y() + this.findOne('.bottom-left').y() < this.findOne('.top-right').y() ? -1 : 1; @@ -1198,15 +1194,6 @@ export class Transformer extends Group { update() { var attrs = this._getNodeRect(); this.rotation(Util._getRotation(attrs.rotation)); - var node = this.getNode(); - var scale = { x: 1, y: 1 }; - // if (node && node.getParent()) { - // scale = node.getParent().getAbsoluteScale(); - // } - var invertedScale = { - x: 1 / scale.x, - y: 1 / scale.y - }; var width = attrs.width; var height = attrs.height; @@ -1233,14 +1220,12 @@ export class Transformer extends Group { y: 0, offsetX: anchorSize / 2 + padding, offsetY: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('top-left') >= 0 }); this.findOne('.top-center').setAttrs({ x: width / 2, y: 0, offsetY: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('top-center') >= 0 }); this.findOne('.top-right').setAttrs({ @@ -1248,21 +1233,18 @@ export class Transformer extends Group { y: 0, offsetX: anchorSize / 2 - padding, offsetY: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('top-right') >= 0 }); this.findOne('.middle-left').setAttrs({ x: 0, y: height / 2, offsetX: anchorSize / 2 + padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('middle-left') >= 0 }); this.findOne('.middle-right').setAttrs({ x: width, y: height / 2, offsetX: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('middle-right') >= 0 }); this.findOne('.bottom-left').setAttrs({ @@ -1270,14 +1252,12 @@ export class Transformer extends Group { y: height, offsetX: anchorSize / 2 + padding, offsetY: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('bottom-left') >= 0 }); this.findOne('.bottom-center').setAttrs({ x: width / 2, y: height, offsetY: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('bottom-center') >= 0 }); this.findOne('.bottom-right').setAttrs({ @@ -1285,23 +1265,18 @@ export class Transformer extends Group { y: height, offsetX: anchorSize / 2 - padding, offsetY: anchorSize / 2 - padding, - scale: invertedScale, visible: resizeEnabled && enabledAnchors.indexOf('bottom-right') >= 0 }); - var scaledRotateAnchorOffset = - -this.rotateAnchorOffset() * Math.abs(invertedScale.y); this.findOne('.rotater').setAttrs({ x: width / 2, - y: scaledRotateAnchorOffset * Util._sign(height) - padding, - scale: invertedScale, + y: -this.rotateAnchorOffset() * Util._sign(height) - padding, visible: this.rotateEnabled() }); this.findOne('.back').setAttrs({ - width: width * scale.x, - height: height * scale.y, - scale: invertedScale, + width: width, + height: height, visible: this.borderEnabled(), stroke: this.borderStroke(), strokeWidth: this.borderStrokeWidth(), diff --git a/test/unit/shapes/Transformer-test.js b/test/unit/shapes/Transformer-test.js index 43bdf81e..a37983c4 100644 --- a/test/unit/shapes/Transformer-test.js +++ b/test/unit/shapes/Transformer-test.js @@ -1319,8 +1319,7 @@ suite('Transformer', function() { tr.simulateMouseUp(); }); - // TODO: doesn't work!!! - test.skip('switch vertical scaling with (top-left anchor)', function() { + test('switch vertical scaling with (top-left anchor)', function() { var stage = addStage(); var layer = new Konva.Layer(); stage.add(layer); @@ -1368,12 +1367,12 @@ suite('Transformer', function() { tr.simulateMouseUp(); assert.equal(rect.x(), 0); - assert.equal(rect.y(), 100); + assert.equal(rect.y(), 0); assert.equal(rect.width(), 100); assert.equal(rect.scaleX(), 1); assert.equal(rect.height(), 100); assert.equal(rect.rotation(), 0); - assert.equal(rect.scaleY(), -1); + assert.equal(rect.scaleY(), 1); }); test('switch scaling with padding for rotated - x', function() { @@ -1705,38 +1704,7 @@ suite('Transformer', function() { }); }); - test.skip('on negative scaleY should move rotater', function() { - var stage = addStage(); - var layer = new Konva.Layer(); - stage.add(layer); - - var rect = new Konva.Rect({ - x: 50, - y: 160, - draggable: true, - width: 100, - height: 100, - fill: 'yellow', - scaleY: -1 - }); - layer.add(rect); - - var tr = new Konva.Transformer({ - node: rect - }); - layer.add(tr); - layer.draw(); - - var rotater = tr.findOne('.rotater'); - var pos = rotater.getAbsolutePosition(); - - assert.equal(pos.x, 100); - - assert.equal(pos.y, 210); - }); - - // TODO: why it doesn't work? - test.skip('try rotated scaled rect', function() { + test('try rotate scaled rect', function() { var stage = addStage(); var layer = new Konva.Layer(); stage.add(layer); @@ -1761,27 +1729,20 @@ suite('Transformer', function() { var rotater = tr.findOne('.rotater'); var pos = rotater.getAbsolutePosition(); - stage.simulateMouseDown({ + tr.simulateMouseDown({ x: pos.x, y: pos.y }); - var top = stage.content.getBoundingClientRect().top; - tr._handleMouseMove({ - clientX: pos.x + 100, - clientY: pos.y - 100 + top + tr.simulateMouseMove({ + x: pos.x + 100, + y: pos.y + 100 + }); + tr.simulateMouseUp({ + x: pos.x + 100, + y: pos.y + 100 }); - // here is duplicate, because transformer is listening window events - tr._handleMouseUp({ - clientX: pos.x + 100, - clientY: pos.y - 100 + top - }); - stage.simulateMouseUp({ - x: 100, - y: 100 - }); - - assert.equal(rect.rotation(), -90); + assert.equal(rect.rotation(), 90); }); test('check correct cursor on scaled shape', function() { @@ -1926,12 +1887,13 @@ suite('Transformer', function() { layer.draw(); layer.scaleX(2); - layer.draw(); assert.equal(tr.width(), 200); + + layer.draw(); }); - test.skip('check fit and correct cursor on rotated parent', function() { + test('check fit and correct cursor on rotated parent', function() { var stage = addStage(); var layer = new Konva.Layer({ x: 100, @@ -1977,6 +1939,51 @@ suite('Transformer', function() { assert.equal(stage.content.style.cursor, 'ew-resize'); }); + test('check drag with transformer', function() { + var stage = addStage(); + stage.draggable(true); + var layer = new Konva.Layer(); + stage.add(layer); + + var rect = new Konva.Rect({ + x: 0, + y: 0, + draggable: true, + width: 100, + height: 100, + fill: 'yellow' + }); + layer.add(rect); + + var tr = new Konva.Transformer({ + node: rect + }); + layer.add(tr); + layer.draw(); + + stage.simulateMouseDown({ + x: 50, + y: 50 + }); + + stage.simulateMouseMove({ + x: 55, + y: 50 + }); + stage.simulateMouseMove({ + x: 60, + y: 50 + }); + + stage.simulateMouseUp({ + x: 60, + y: 50 + }); + + assert.equal(rect.x(), 10); + assert.equal(rect.y(), 0); + }); + test('stopTransform method', function() { var stage = addStage(); var layer = new Konva.Layer(); @@ -2535,6 +2542,7 @@ suite('Transformer', function() { }); }); + // TODO: fix it!!! test.skip('centered scaling on flip + keep ratio', function() { var stage = addStage(); var layer = new Konva.Layer();