【问题标题】:Using Raphael JS to float shapes around the screen使用 Raphael JS 在屏幕上浮动形状
【发布时间】:2010-11-23 13:41:45
【问题描述】:

我正在努力让一些形状随机漂浮在屏幕上。最终它们将相互交互,并具有 onclick 功能。我可以让圆圈在屏幕上呈现,但我无法让它们运动。看来 .animate() 函数不是我这里需要的。

有人知道我会怎么做吗?

这是我所拥有的:

jQuery(function ($) {

    // Global Vars
    var items = 30,
    width = window.innerWidth,
    height = window.innerHeight,
    paper = Raphael("galaxy", width, height),
    frameRate = 100,
    i,
    galaxy = [],
    star = [],
    stars = [];

    // Do some variants for each item
    for (i = 0; i<items; i++ ) {
        stars[i] = {
            x : Math.random() * width,
            y : Math.random() * height,
            r : Math.random()*255,
            g : Math.random()*255,
            b : Math.random()*255,
            size :  Math.random() * 20
        }
    }

    // Creates canvas 320 × 200 at 10, 50
    // Creates circle at x = 50, y = 40, with radius 10
    for (i = 0; i<items; i++ ) {
        star[i] = paper.circle(stars[i].x, stars[i].y, stars[i].size);
        star[i].attr("fill", "rgb("+stars[i].r+", "+stars[i].g+", "+stars[i].b+")");
        star[i].attr("stroke", "none"); 

    }//end for

});

【问题讨论】:

    标签: vector canvas svg raphael shapes


    【解决方案1】:

    它可以与 animate() 一起使用。如果你例如将其添加到上述函数中:

    (function anim() {
        for (i = 0; i<items; i++ ) {
             newX = Math.random() * width;
             newY = Math.random() * height;
             star[i].animate({cx: newX, cy: newY}, 1000);
        }
        setTimeout(anim, 1000);
    })();
    

    你的圈子飞来飞去。当然,要让它们真正浮动甚至互动还有很长的路要走,但这可能是一个起点。

    【讨论】:

    • 哇,这太完美了。我尝试过使用 setTimeout 的不同组合,但我永远无法做到正确。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    相关资源
    最近更新 更多