使用 Raphael JS 在屏幕上浮动形状

发布于 2024-08-03 03:00:47 字数 1096 浏览 5 评论 0原文

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

});

I am working on having some shapes floating around the screen at random. Eventually they will interact with each other, as well as have onclick functions. I can get the circles to render on screen, but I cannot get them to have motion. It seems that the .animate() function is not what I need here.

Does anyone know how I would go about this?

Here is what I have:

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

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-08-10 03:00:48

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

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

it could work with animate(). If you e.g. add this to the above function:

(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);
})();

your circles fly around. Of course it's still a long way to go to have them really floating or even interaction, but it's maybe a starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文