开发者

Making vector points blink using Raphael and Javascript

开发者 https://www.devze.com 2023-02-21 02:56 出处:网络
I\'m 开发者_运维技巧using the Raphael JS library, and I\'m trying to figure out how to make a point appear on screen, then disappear.

I'm 开发者_运维技巧using the Raphael JS library, and I'm trying to figure out how to make a point appear on screen, then disappear.

I use a for loop to create the points, and then I make them fade in. Is there a way that they can also fade out, and I can remove them?

I'm quite new to Javascript, so I don't know the best strategy for dealing with this. I could not see how to to this in the Raphael documentation.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>blink point</title>        
        <script src="js/raphael.js"></script> 
        <!--<script src="https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael-min.js"></script>-->
        <script type="text/javascript">
        window.onload = function () {

            //Point Array
            pointList = new Array ();

            // Create Canvas
            var r = Raphael(10, 50, 600, 600);            

            // Make a 'group' of points
            for( i=0; i<20; i++){   

                    //Create Point            
                    pointList[i] = r.circle(10*i, 10*i,5);
                    pointList[i].attr({stroke: "none", fill: "#999", opacity: 0});

                    //Fade in   
                    pointList[i].animate({opacity: 1}, 1000 );  

            }

            // Remove points, starting with the first
            for( i=0; i<20; i++){           

                    //Try fading them out
                    //pointList[i].animate({opacity: 0}, 1000 );
            }

        };
        </script>
    </head>

    <body>
        <div id="holder"></div>         
    </body>
</html>

I also was not able to get the online link to the Raphael library to work, so it might be necessary to download the library.


You can find working example here http://jsbin.com/uxege4/2/edit
In details:

Problem with your code - animation is done asynchronously and that means your program will be finished before fading in. So you need to set up callback function when animation will be ready. Here is quote from Raphael documentation:

animate

Changes an attribute from its current value to its specified value in the given amount of milliseconds.
Parameters

newAttrs object A parameters object of the animation results. (Not all attributes can be >animated.)
ms number The duration of the animation, given in milliseconds.
callback function [optional]

Last parametr is what we need. You just need assign function to call after animation finished.


I made you this: http://jsbin.com/uxege4/5/edit You can chain the all the stuff together and have a callback function in the .animate(). Like this:

r.circle(10*i, 10*i, 5).attr({stroke: "none", fill: "#999", opacity: 0}).animate({opacity: 1}, 1000, function(){
                this.animate({opacity: 0}, 1000, function(){
                  this.remove();
                });

          });

The Callback function gets called when the animation is over and passes over the raphäel object.


To clarify -

Raphael's animate() will start happening as soon as you make the function call and will keep happening while the rest of your JavaScript is executed.

I've modified Eldar's example code to demonstrate this. See: http://jsbin.com/uxege4/4/edit

Note that the yellow circles are drawn at the same time as the grey ones even though the call to animate() them occurs later in the code.

Hitting a callback function upon the completion of an asynchronous code path is a common pattern in JavaScript and it is essential to understand it in order to be productive with JS.

In Eldar's example, an anonymous function is attached to the first animate()'s callback handler. Upon completion of the initial fade-in animate(), the function gets called, and performs a fade-out.

I recommend Douglas Crockford's JavaScript: The Good Parts (which is, somewhat amusingly, the slimmest programming book I've ever read) and running through JavaScript Koans. Doing that should set you on the right track.


I have created a never ending blink , of if you choose after some time..with help of clousures!

Intro is some raphael object check it out ;D

Have fun with it ! , kind regards Less

var createBlink = function(i,that){     
    var fadeIn = function(i){   
        if(i == 100){
            console.log("end fadein");
            return;
        }else{
            console.log("fadein " + i);
            that.animate({ opacity: 0.8 }, 1000, "<" , function(){
                fadeOut(++i) ;
            });
        }
    }
    var fadeOut = function(i){  
        if(i == 100){
            console.log("end fadeout");
            return;
        }else{
            console.log("fadeout " + i);
            that.animate({ opacity: 0.0 }, 1000, "<" , function(){
                fadeIn(++i);
            });
        }
    }
    fadeIn(i);      
}

createBlink(0,intro);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号