0

I have two overlayed rectangles:

enter image description here

I'm trying to fade them out uniformly as if they were one object.

The problem: When I animate their opacity from 1 to 0, the top rectangle becomes transparent and reveals the edges of the rectangle beneath it.

Here is my code:

    var paper = Raphael(50,50,250,350)
    var rect = paper.rect (20,40,200,200).attr({"fill":"red","stroke":"black"})
    var rect2 = paper.rect (100,140,200,200).attr({"fill":"red","stroke":"black"})
    var set=paper.set()
    set.push(rect)
    set.push(rect2)
    set.click(function () {fadeOut()})
    function fadeOut() {
            rect.animate({"opacity":0},3000)
            rect2.animate({"opacity":0},3000)
            setTimeout(function () {
                rect.attr({"opacity":1})
                rect2.attr({"opacity":1})
            },3100)

    }

When the set is clicked, the rectangles fade out in 3 seconds. (look at the red rectangles in my fiddle, it will clarify my problem)

https://jsfiddle.net/apoL5rfp/1/

In my fiddle I also create a similar looking green path that performs the fade out CORRECTLY.

I can I achieve the same type of fadeout with multiple objects?

1 Answer 1

0

I think this is quite difficult in Raphael alone.

Few options spring to mind. Don't use Raphael, use something like Snap, put them in a group and change opacity in the group.

var g = paper.g(rect, rect2);

g.click(function () { fadeOut( this )} )

function fadeOut( el ) {

        el.animate({"opacity":0},3000)
        setTimeout(function () {
            el.attr({"opacity":1})
        },3100)

}

jsfiddle

However, you may be tied to Raphael, which makes things a bit tricky, as it doesn't support groups. You could place an 'blank' object over it (which matches same as background) and animate its opacity in the opposite way, like this..(note the disabling of clicks on top object in css)

var rectBlank = paper.rect(18,20,250,330).attr({ fill: 'white', stroke: "white", opacity: 0 });
var set=paper.set()
....
rectBlank.animate({"opacity":1},3000)

jsfiddle

Otherwise I think you may need to use a filter, which may help a bit. SO question

4
  • Interesting!! For your first option: is there anyway to incorporate the snap elements onto the same canvas as the Raphael (I'm working in full screen) without problems? The second solution doesn't really work since my two rectangles are eventually gonna be replaced by two images Any other ideas or directions I can attempt? Commented Jun 19, 2015 at 14:54
  • The second solution should still work with images, if you place a rect above the images to cover them fully. The first solution, Snap can access existing SVG objects fine, but I would probably expect some bugs if trying to repeatedly access the respective elements from each library, as they may overwrite the others settings. So it depends why Raphael is being uses really (Snap is the successor to Raphael, same author, so there's a lot of compatibility, but Snap doesn't play well with older browsers like IE8 etc)
    – Ian
    Commented Jun 19, 2015 at 15:06
  • When I said images, I meant, that two overlayed images are fadedout to reveal a third image. Would the second solution work? I'm not worried about the snap and raphael using the same settings because it's a very local process this particular problem that I have. But how can I use them simultaneously on the same canvas? Commented Jun 19, 2015 at 15:10
  • Just incredible! Thank you! Commented Jun 19, 2015 at 18:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.