j0hnstew
j0hnstew

Reputation: 709

javascript raphael object function passing

I apologize for asking this question but I am just looking for a little guidance on this morning. I simply want to create a function so that way I can make a Raphael element glow by just passing in that element. Below is the code I have. Why does this not work?

var paper = Raphael("playarea", 500, 500);

var rectangle = paper.rect(100, 100, 200, 200, 4);

function elemHover(var el)
{

    el.hover(
    // When the mouse comes over the object //
    // Stock the created "glow" object in myCircle.g
   function() {
    this.g = this.glow({
        color: "#0000EE",
         width: 10,
         opacity: 0.8
     });
    },
    // When the mouse goes away //
    // this.g was already created. Destroy it!
    function() {
     this.g.remove();
    });
}

elemHover(rectangle);

here is the fiddle http://jsfiddle.net/aZG6C/15/

Upvotes: 1

Views: 305

Answers (1)

kiranvj
kiranvj

Reputation: 34147

You should fill the element( rectangle in our case) to trigger the hover.

rectangle.attr("fill", "red");

Try this fiddle http://jsfiddle.net/aZG6C/17/

The full code will look like

<div id="playarea"></div>

​<script type="text/javascript">
var paper = Raphael("playarea", 500, 500);

var rectangle = paper.rect(100, 100, 200, 200, 4);

function elemHover(el)
{
    el.hover(
    // When the mouse comes over the object //
    // Stock the created "glow" object in myCircle.g
    function() {
     this.g = this.glow({
         color: "#0000EE",
         width: 10,
         opacity: 0.8
     });
    },
    // When the mouse goes away //
    // this.g was already created. Destroy it!
    function() {
     this.g.remove();
    });
}

rectangle.attr("fill", "red");

elemHover(rectangle);

</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Update

Hover event is triggered only if the element is filled with something. If you want to have a transparent element you can try

rectangle.attr("fill", "transparent");

Check the fiddle here http://jsfiddle.net/aZG6C/20/

Upvotes: 2

Related Questions