What is the impact on the browser when there is infinite and direct interaction in HTML?

Asked

Viewed 69 times

6

I have several polygons that change their points infinitely (while on the page), this interaction is done by a js plugin and in my code I have these calls:

var config = {
    targets: '',
    points: [],
    easing: 'easeOutQuad',
    duration: 10000,
    loop: true,
    direction: 'alternate'
}

// Tenho 11 chamadas como esta
config.targets = '.class';
config.points = [{value: 'pontos'}, {value: 'pontos'}, {value: 'pontos'}]
anime(config);

So there are 11 elements being modified 3 times, I will have problems with this practice? If yes, how can I improve?

The plugin I’m using: https://animejs.com/

  • 3

    Resource consumption, memory failure, browser crash. If it’s on mobile it’s even worse.... Chrome Devtools has tools for you to measure this, use them, will help you get an idea

1 answer

7


Look I used Chrome Devtools to check resource consumption. But keep in mind that I tested right on the page of the plugin, there it is practically running insolently, but think of a real environment with more things on the page and other software consuming machine resources.

inserir a descrição da imagem aqui

See that you have several scales of memory consumption every time the animation starts, as soon as the animation for consumption goes down. I tried to demonstrate with the mouse arrow, stay tuned to it and the memory consumption curve

inserir a descrição da imagem aqui

Now see what is happening "underneath the scenes" see how many calculations the browser is doing. Certainly not very sustainable for the system...

inserir a descrição da imagem aqui

The effects of this as commented are high consumption of system resources, memory failure, browser crash. If it is on mobile devices it is even worse because they have less processing capacity....

inserir a descrição da imagem aqui

This animation of the link you pass is not even the worst, why she works in the do composite of the page rendering, it means that it animates the element after it has already been rendered, because the animation uses transform, and not margim. To better understand look at these two animations, see how the animation from the top of the "grabbing" because uses margin-left, the one below uses transform, see how it’s more fluid.

.box {
	width: 50px;
	height: 50px;
	background-color: red;
	animation: nome 3s linear infinite;
}

@keyframes nome {
	50% {
		margin-left: 500px;
	}
}

.box.x {
	animation: nomex 3s linear infinite;
}

@keyframes nomex {
	50% {
		transform: translateX(500px) translateZ(0);
	}
}
<p>com margim (vai apresentar pequenos travamentos)</p>
<div class="box"></div>
<p>com transform: translate (não tem travamentos é mais fluida)</p>
<div class="box x"></div>

Another point is that the translate active the use of GPU in the rendering of the animation, which relieves the processing of the CPU who is usually busy with other things.

TIP: The SVG tb has this more fluid behavior in the animations, and I recommend using it in place of this animation that you mentioned, mainly for not consuming so much resource, because the animation happens all in composite, in addition, it needs less nodes in the DOM which ends up weighing a lot in the case of your animation.

I recommend that you read also about the will-change that can help you optimize web animations. CSS will-change property: when to use?

  • Yes, I had noticed in the calculations in the elements itself and in fact in the cell eh much worse, it is locking a lot. I already made a function to activate the effects only when the user is on the screen, now I have to optimize this code better, maybe decrease the value and the amount of elements. Thank you!

  • @Kamilepimenta esta animação nem eh das pior Pq ela usa Transform, o que normalmente deixa o processamento para GPU e não para o CPU, so I will make an issue there to include some more observations and I’ll tell you. But I already tell you that putting a translateZ and will-change may improve a little the performance, and for 10 or 15 elements the browser should give quiet account

  • Okay, thank you Hugo! Here is the link of the real project if you want to check: https://tmw.com.br/ . The effects are at the home of the site even, if you have any suggestions for improvement can inform me. :)

  • 1

    @Kamilepimenta cara edited the answer with a few more hints, I think you might be interested, although there’s not much else you can do but try to deploy will-change, or migrate to a SVG animation... But reducing the number of animated nodes in the DOM will already improve a lot!

  • 1

    Siiim, very top! Thank you :)

Browser other questions tagged

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