如何在Kineticjs中的曲线路径上的对象进行动画 [英] How to animate object on curve path in Kineticjs

查看:109
本文介绍了如何在Kineticjs中的曲线路径上的对象进行动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一条路径作为一些曲线,可能是四边形曲线。它可以类似于 http: //www.html5canvastutorials.com/labs/html5-canvas-modify-curves-with-anchor-points-using-kineticjs/

I would like to create a path as some curve, probably quad curve. It can be done similarly to http://www.html5canvastutorials.com/labs/html5-canvas-modify-curves-with-anchor-points-using-kineticjs/

然后我能够创建图像对象。但是,我想沿着创建的路径(从起点到曲线的终点移动它)动画。我可以使用Javascript + Canvas + KineticJS(v 4.7.1)。有什么办法,怎么办呢?我找不到任何解决此问题的示例。

Then I am able to create image object. However, I want to animate it along the created path (move it from start point to end point of the curve). I can use Javascript+Canvas+KineticJS(v 4.7.1). Is there any way, how to do it? I can't find any example which solves this.

推荐答案

演示: http://jsfiddle.net/m1erickson/nnU89/

您可以使用此公式计算沿二次曲线的点:

You can calculate points along a quadratic curve with this formula:

// Calc an XY along a quadratic curve at interval T
// T==0.00 at start of curve, T==1.00 at end of curve
function getQuadraticBezierXYatT(startPt,controlPt,endPt,T) {
    var x = Math.pow(1-T,2) * startPt.x + 2 * (1-T) * T * controlPt.x + Math.pow(T,2) * endPt.x; 
    var y = Math.pow(1-T,2) * startPt.y + 2 * (1-T) * T * controlPt.y + Math.pow(T,2) * endPt.y; 
    return( {x:x,y:y} );
}

您传入:


  • 曲线点(startPt,controlPt,endingPt)

  • 沿着曲线计算XY(T)的间隔

  • 注意:T == 0在曲线的开始,T == 1.00在曲线的结尾

然后你可以创建一个Kinetic.Animation,沿着曲线动画:

Then you can create a Kinetic.Animation that animates along the curve:

var animation = new Kinetic.Animation(function(frame) {

    // calc an XY along the curve at interval T

    var pos=getQuadraticBezierXYatT(qStart,qControl,qEnd,T/100);

    // set some Kinetic object to that position

    yourObject.setPosition(pos);    

    // change T for the next animation frame

    T+=TDirection;
    if(T<0 || T>100){ TDirection*=-1; T+=TDirection}

}, layer);

这篇关于如何在Kineticjs中的曲线路径上的对象进行动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆