2014-09-02 37 views
0

我想要動畫形狀繼續從X到Y距離,再次使用動力學js動畫函數從Y到X返回形狀。例如。將形狀從0px移動到200px,並再次將形狀200px返回到0px。kineticjs ::從X到Y的距離繼續動畫形狀

謝謝。

+0

你嘗試了什麼? – lavrton 2014-09-02 12:35:12

+0

Hi lavrton,| 我想循環兩個用戶定義的點X到Y之間的動畫。我看到這個例子:[KineticJS Animate Position Tutorial](http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-animate-position-教程/),但我不知道如何控制開始和位置,因爲X和Y點是用戶定義的。 – Divyesh 2014-09-04 07:13:54

回答

1

您可能正在尋找一個補間,而不是一個動畫。退房的動能文檔上吐溫更多的信息,但是你在找什麼做的可能會是這個樣子:

var tween = new Kinetic.Tween({ 
    node: myShape, 
    x: moveToX, 
    y: moveToY, 
    duration: 1 
}); 

現在,你有一個補間,你可以玩,倒帶,暫停,將其反轉(再次查看文檔以獲取有關Tween的所有最新信息)。

所以,你現在可以做的:

tween.play() 

tween.reverse() 

來完成你在找什麼。

參考:http://www.html5canvastutorials.com/kineticjs/html5-canvas-stop-and-resume-transitions-with-kineticjs/

更新(按照下面的評論):如果你想有一個循環的影響,在X方向,Y方向,或兩者兼而有之。你可以這樣做:

var yPos = myShape.getAttr('y'), 
    xPos = myShape.getAttr('x'), 
    maxX = 1000, 
    maxY = 1000, 
    yIncreasing = true, 
    xIncreasing = true; //lets assume myShape resides somewhere below the max 

var anim = new Kinetic.Animation(function(frame) { 
    /* move the shape back and fourth in a y direction */ 
    if (yPos < maxY && yIncreasing) { 
     hexagon.setY(yPos++); 
    } else { 
     if (yPos < 1) { 
      yIncreasing = true; 
      hexagon.setY(yPos++); 
     } else { 
      hexagon.setY(yPos--); 
      yIncreasing = false; 
     } 
    } 

    /* move the shape back and fourth in a x direction */ 
    if (xPos < maxX && xIncreasing) { 
     hexagon.setX(xPos++); 
    } else { 
     if (xPos < 1) { 
      xIncreasing = true; 
      hexagon.setX(xPos++); 
     } else { 
      hexagon.setX(xPos--); 
      xIncreasing = false; 
     } 
    } 
}, layer); 

注意:我還沒有運行此代碼,它應該工作。使用兩者都會導致形狀沿對角線移動,但希望此snipplet能夠爲您的問題提供解決方案。

+0

感謝hippofluff。但使用補間功能,我無法循環動畫。任何其他解決方案將動畫從X到Y的距離循環。我看到一個例子[KineticJS Animate Position Tutorial](http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-animate-position-tutorial/),但我不知道如何在兩個特定點X之間循環形狀到Y,其中X和Y是用戶定義的值。 – Divyesh 2014-09-04 06:56:46

+0

你發給我的例子有什麼問題?我看到一個形狀從x和y向後移動和四分之一。 – hippofluff 2014-09-04 13:47:43

+0

是從x和y向後移動和向後移動的形狀,但是第x和Y位置用戶已定義。如果用戶指定將形狀從X = 0移動到Y = 250,那麼我如何實現這一點? – Divyesh 2014-09-05 05:38:59