vector - 使用 Raphael JS 在屏幕上 float 形状

标签 vector canvas svg raphael shapes

我正在努力让一些形状随机漂浮在屏幕上。最终它们将相互交互,并具有 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

});

最佳答案

它可以与 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);
})();

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

关于vector - 使用 Raphael JS 在屏幕上 float 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1344176/

相关文章:

apache-flex - 泛型(向量)如何在 AVM 中工作?

c++ - vector 是否必须存储两次大小?

css - Safari 和 Mobile Safari 中的内联 SVG 中断

html - svg 矩形边框未正确绘制

c++ - Boost 多数组的 std::vector

c++ - 对 vector 的引用在回退后是否会重新定位?如果是,如何?

android - Android中webview的截图方法

javascript - 更新一个 JavaScript 对象会更新所有对象

javascript - 如何将div层导出到 Canvas ?

javascript - 将内联 SVG 转换为 Base64 字符串