0

所以我有一些绘制路径和圆圈的代码。每次启动该功能时,圆圈都会在路径上设置动画。我只想创建 10 条路径,每条路径都有自己的圆圈,在每条路径上都有动画。当我简单地执行该函数 10 次时,路径生成得很好,但是圆圈都沿着相同的单一路径进行动画处理。我在这里做错了什么?这里是创建 for(i=0) 循环的最佳方法吗?

您可以在jfiddle上查看我的代码,或在此处查看:

function commerce() {
Raphael("commercebounce", function () {
            var r = this;   
            function pathfade() {
            var pa = .1,
                pb = .4, 
                pc = [0, 2], 
                pd = [50, 300], 
                pe = [150, 800], 
                pf = [150, 350], 
                pg = pd[0] + Math.random() * (pd[1] - pd[0]), 
                ph = pe[0] + Math.random() * (pe[1] - pe[0]), 
                pi = pf[0] + Math.random() * (pf[1] - pf[0]),
                bd = .1,
                be = .9,
                pathspot = bd + Math.random() * (be - bd),
                colours = ["215,10,45", "115,115,115"],
                stroke = ["", "- "];
                opacity = pa + Math.random() * (pb - pa), colour = "rgb(" + colours[Math.round(Math.random())] + ")", strokeW = pc[Math.round(Math.random())];
                pj = r.path("M 0 " + pg + " C 0 " + pg + " " + (ph - 100) + " " + pg + " " + ph + " 400 M " + ph + " 400 C " + ph + " 400 " + (ph + 100) + " " + pg + " 960 " + pi).attr({stroke: colour,"stroke-dasharray": stroke[Math.round(Math.random())],"opacity": 0});
                bh = r.circle(0, 0, 7, 7).attr({"stroke-width": this.strokeW,stroke: colour,"stroke-opacity": this.opacity,fill: "none","fill-opacity": 0}).onAnimation(function() {
                var t = this.attr("transform")})
                leng = pj.getTotalLength();
                r.customAttributes.along1 = function (v) {
                    var point = pj.getPointAtLength(v * leng);
                    return {
                        transform: "t" + [point.x, point.y] + "r" + point.alpha
                       }
                };
                return bh.attr({along1:0}), bh.animate({along1:pathspot},300), pj.animate({opacity:opacity},300), pj, bh
             }
pathfade();//how do i repeat this function n times?               
    }); 
}
commerce();
4

3 回答 3

2

据我所知,一个简单的循环应该没问题。如果您有兴趣,这是在 JavaScript 中循环的最快方法:

var i = 10; while (i--) {
//Your code..
}
于 2012-04-30T01:48:23.283 回答
1
var n = 5; //or however much you want
for (var i = 0; i < n; i++){
    pathfade();
}

for循环方法是这里最简单的选择。

于 2012-04-30T01:48:03.383 回答
1

您需要将 pathfade() 函数分解为多个仅执行少数任务的简单函数。

有两个主要问题。首先,您在变量声明期间应放置逗号的位置放置一个分号。查看笔画变量。其次,当 javascript 只支持一个值时,pathfade 会返回多个值。请记住,一旦您从函数返回,其余的语句就不会被执行。

最后,使用 for、do-while 或 while 循环重复您的函数调用。

这是修复。对不起,我没有时间重构。

function commerce() {
    Raphael("commercebounce", function () {
        var r = this;
        function pathfade() {
            var pa = 0.5,
            pb = 0.9,
            pc = [0, 2],
            pd = [50, 300],
            pe = [150, 800],
            pf = [150, 350],
            pg = pd[0] + Math.random() * (pd[1] - pd[0]),
            ph = pe[0] + Math.random() * (pe[1] - pe[0]),
            pi = pf[0] + Math.random() * (pf[1] - pf[0]),
            bd = 0.1,
            be = 0.9,
            pathspot = bd + Math.random() * (be - bd),
            colours = ["215,10,45", "115,115,115"],
            stroke = ["", "- "],
            opacity = pa + Math.random() * (pb - pa),
            colour = "rgb(" + colours[Math.round(Math.random())] + ")",
            strokeW = pc[Math.round(Math.random())],
            pj = r.path("M 0 " + pg + " C 0 " + pg + " " + (ph - 100) + " " + pg + " " + ph + " 400 M " + ph + " 400 C " + ph + " 400 " + (ph + 100) + " " + pg + " 960 " + pi).attr({
                    stroke : colour,
                    "stroke-dasharray" : stroke[Math.round(Math.random())],
                    "opacity" : 0
                }),
            bh = r.circle(0, 0, 7, 7).attr({
                    "stroke-width" : this.strokeW,
                    stroke : colour,
                    "stroke-opacity" : this.opacity,
                    fill : "none",
                    "fill-opacity" : 0
                }).onAnimation(function () {
                    var t = this.attr("transform")
                }),
                leng = pj.getTotalLength();
            r.customAttributes.along1 = function (v) {
                var point = pj.getPointAtLength(v * leng);
                return {
                    transform : "t" + [point.x, point.y] + "r" + point.alpha
                }
            };
            bh.attr({
                along1 : 0
            });
            bh.animate({
                along1 : pathspot
            }, 300);
            pj.animate({
                opacity : opacity
            }, 300);
        }
        var i = 10;
        while( i-- ){
            pathfade();
        }
    });
}
commerce();

演示:http: //jsfiddle.net/VEdwG/6/

您应该阅读“C# 风格的元素”。

http://www.amazon.com/The-Elements-Style-Kenneth-Baldwin/dp/0521671590/ref=cm_cr-mr-title

于 2012-04-30T05:59:09.813 回答