Effect with js and svg only works in the first class in the second effect does not apply

Asked

Viewed 30 times

1

I have a problem with my effect on svg added a triangles effect with svg and js in one of my sessions website as you can see works normally but needed to put this effect again in another session I’m calling the effect via class and not by id but it doesn’t work it only works in the first session follows the photos First session running: inserir a descrição da imagem aqui

Second session does not work: inserir a descrição da imagem aqui

Notice that even the triangles do not appear because? so I understand javascript is only getting the first class and the second it does not find so it does not activate the effect follows my codes:

HTML:

JS:

/*SVG Background*/
        var refreshDuration = 6000;
        var refreshTimeout;
        var numPointsX;
        var numPointsY;
        var unitWidth;
        var unitHeight;
        var points;

        function onLoad()
        {
            var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
            svg.setAttribute('width',window.innerWidth);
            svg.setAttribute('height',window.innerHeight);
            document.querySelector(".bg").appendChild(svg);

            var unitSize = (window.innerWidth+window.innerHeight)/20;
            numPointsX = Math.ceil(window.innerWidth/unitSize)+1;
            numPointsY = Math.ceil(window.innerHeight/unitSize)+1;
            unitWidth = Math.ceil(window.innerWidth/(numPointsX-1));
            unitHeight = Math.ceil(window.innerHeight/(numPointsY-1));

            points = [];

            for(var y = 0; y < numPointsY; y++) {
                for(var x = 0; x < numPointsX; x++) {
                    points.push({x:unitWidth*x, y:unitHeight*y, originX:unitWidth*x, originY:unitHeight*y});
                }
            }

            randomize();

            for(var i = 0; i < points.length; i++) {
                if(points[i].originX != unitWidth*(numPointsX-1) && points[i].originY != unitHeight*(numPointsY-1)) {
                    var topLeftX = points[i].x;
                    var topLeftY = points[i].y;
                    var topRightX = points[i+1].x;
                    var topRightY = points[i+1].y;
                    var bottomLeftX = points[i+numPointsX].x;
                    var bottomLeftY = points[i+numPointsX].y;
                    var bottomRightX = points[i+numPointsX+1].x;
                    var bottomRightY = points[i+numPointsX+1].y;

                    var rando = Math.floor(Math.random()*2);

                    for(var n = 0; n < 2; n++) {
                        var polygon = document.createElementNS(svg.namespaceURI, 'polygon');

                        if(rando==0) {
                            if(n==0) {
                                polygon.point1 = i;
                                polygon.point2 = i+numPointsX;
                                polygon.point3 = i+numPointsX+1;
                                polygon.setAttribute('points',topLeftX+','+topLeftY+' '+bottomLeftX+','+bottomLeftY+' '+bottomRightX+','+bottomRightY);
                            } else if(n==1) {
                                polygon.point1 = i;
                                polygon.point2 = i+1;
                                polygon.point3 = i+numPointsX+1;
                                polygon.setAttribute('points',topLeftX+','+topLeftY+' '+topRightX+','+topRightY+' '+bottomRightX+','+bottomRightY);
                            }
                        } else if(rando==1) {
                            if(n==0) {
                                polygon.point1 = i;
                                polygon.point2 = i+numPointsX;
                                polygon.point3 = i+1;
                                polygon.setAttribute('points',topLeftX+','+topLeftY+' '+bottomLeftX+','+bottomLeftY+' '+topRightX+','+topRightY);
                            } else if(n==1) {
                                polygon.point1 = i+numPointsX;
                                polygon.point2 = i+1;
                                polygon.point3 = i+numPointsX+1;
                                polygon.setAttribute('points',bottomLeftX+','+bottomLeftY+' '+topRightX+','+topRightY+' '+bottomRightX+','+bottomRightY);
                            }
                        }
                        polygon.setAttribute('fill','rgba(0,0,0,'+(Math.random()/3)+')');
                        var animate = document.createElementNS('http://www.w3.org/2000/svg','animate');
                        animate.setAttribute('fill','freeze');
                        animate.setAttribute('attributeName','points');
                        animate.setAttribute('dur',refreshDuration+'ms');
                        animate.setAttribute('calcMode','linear');
                        polygon.appendChild(animate);
                        svg.appendChild(polygon);
                    }
                }
            }

            refresh();

        }

        function randomize() {
            for(var i = 0; i < points.length; i++) {
                if(points[i].originX != 0 && points[i].originX != unitWidth*(numPointsX-1)) {
                    points[i].x = points[i].originX + Math.random()*unitWidth-unitWidth/2;
                }
                if(points[i].originY != 0 && points[i].originY != unitHeight*(numPointsY-1)) {
                    points[i].y = points[i].originY + Math.random()*unitHeight-unitHeight/2;
                }
            }
        }

        function refresh() {
            randomize();
            for(var i = 0; i < document.querySelector(".bg svg").childNodes.length; i++) {
                var polygon = document.querySelector(".bg svg").childNodes[i];
                var animate = polygon.childNodes[0];
                if(animate.getAttribute('to')) {
                    animate.setAttribute('from',animate.getAttribute('to'));
                }
                animate.setAttribute('to',points[polygon.point1].x+','+points[polygon.point1].y+' '+points[polygon.point2].x+','+points[polygon.point2].y+' '+points[polygon.point3].x+','+points[polygon.point3].y);
                animate.beginElement();
            }
            refreshTimeout = setTimeout(function() {refresh();}, refreshDuration);
        }

        function onResize() {
            document.querySelector(".bg svg").remove();
            clearTimeout(refreshTimeout);
            onLoad();
        }

        window.onload = onLoad;
        window.onresize = onResize;

Note: Code taken from this site here

1 answer

1


One way to solve this is by putting at the end of your function onLoad():

var bg_svg = $("div.bg").first().html();
$("div.bg").each(function(){
   if($(this).find("svg").length == 0) $(this).html(bg_svg);
});

That will clone the contents of the first session and play inside the second that is empty (or all that are empty).

Or, if there are only 2, you can use:

$("div.bg:eq(1)").html($("div.bg").first().html());
  • Thank you very much worked but it happens a problem like I did here it works in the first and in the second but the second works a little later and does not come back to work I will add this with this your answer in the link ai you give a look http://www.deepocean.com.br/2018/

  • the first session works normal and not already there in the last session works for a while after the triangles stop moving up the server of a look please

  • 1

    @Kirito I’ll take a look...

  • beauty sent you the link there in the comments

  • only reinforcing any of the two codes you made if I put the two triangles work but the last one moves for a while and soon the top one is working normally

  • 1

    @Kirito Locate setTimeout within "refresh()"

  • opa I found it what I do now? refreshTimeout = setTimeout(Function() {refresh();}, refreshDuration);

  • @Kirito Replace with this: refreshTimeout = setTimeout(Function() { refresh(); var bg_svg = $("div.bg"). first(). html(); $("div.bg"). each(Function(e){ if(e != 0) $(this). html(bg_svg); }); }, refreshDuration);

  • You’re the guy it worked out ahahha vlw even

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.