blob: ce3d06f601ac15c3f964da4002fd4b25fdf0bda3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
---
title: Window.requestAnimationFrame()
slug: Web/API/Window/requestAnimationFrame
translation_of: Web/API/window/requestAnimationFrame
---
<div>{{APIRef}}</div>
<div>El método <strong><code>window.requestAnimationFrame</code></strong> informa al navegador que quieres realizar una animación y solicita que el navegador programe el repintado de la ventana para el próximo ciclo de animación. El método acepta como argumento una función a la que llamar antes de efectuar el repintado.</div>
<div class="note"><strong>Nota:</strong> Si no quieres que tu animación se detenga, debes asegurarte de llamar a su vez a <code>requestAnimationFrame() </code>desde tu callback.</div>
<p>Debes llamar a este método cuando estés preparado para actualizar tu animación en la pantalla para pedir que se programe el repintado. Ésto puede suceder hasta 60 veces por segundo en pestañas en primer plano, pero se puede ver reducido a velocidades inferiores en pestañas en segundo plano.</p>
<p>El método indicado como callback recibe un único argumento que indica el tiempo en el que está programado que se ejecute el ciclo de animación.</p>
<h2 id="Syntax" name="Syntax">Sintaxis</h2>
<pre class="syntaxbox notranslate"><em>requestID</em> = window.mozRequestAnimationFrame(<em>callback</em>); // Firefox
window.msRequestAnimationFrame(<em><code>callback</code></em>); // IE 10 PP2+
window.webkitRequestAnimationFrame(callback<em>[, element]</em>); // Chrome/Webkit
</pre>
<h3 id="Parameters" name="Parameters">Parámetros</h3>
<dl>
<dt><code>callback</code></dt>
<dd>Parámetro que especifica la función a la cual llamar llegado el momento de actualizar tu animación para el próximo repintado.</dd>
</dl>
<h3 id="Valor_devuelto">Valor devuelto</h3>
<p>Un valor entero <code>long</code>, es un entero de tipo long que identifica de manera exclusiva la entrada en la lista de callbacks. Es siempre un distinto de cero, pero no debes realizar ninguna otra suposición acerca de su valor. Puedes pasar este valor a {{ domxref("window.cancelAnimationFrame()") }} para cancelar la petición de actualización del callback.</p>
<h2 id="Notes" name="Notes">Ejemplo</h2>
<pre class="brush: js notranslate">(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
<code>var start = null;
var element = document.getElementById('SomeElementYouWantToAnimate');
function step(timestamp) {
if (!start) start = timestamp;
var progress = timestamp - start;
element.style.transform = 'translateX(' + Math.min(progress / 10, 200) + 'px)';
if (progress < 2000) {
window.requestAnimationFrame(step);
}
}
window.requestAnimationFrame(step);</code></pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">Compatibilidad entre Navegadores</h2>
{{Compat("api.Window.requestAnimationFrame")}}
<h2 id="Specification" name="Specification">Especificaciones</h2>
<p>{{ spec("http://www.w3.org/TR/animation-timing/#requestAnimationFrame", "Timing control for script-based animations: requestAnimationFrame", "WD") }}</p>
<h2 id="Véase_también">Véase también</h2>
<ul>
<li>{{ domxref("window.mozAnimationStartTime") }}</li>
<li>{{ domxref("window.cancelAnimationFrame()") }}</li>
<li><a class="external" href="http://weblogs.mozillazine.org/roc/archives/2010/08/mozrequestanima.html">mozRequestAnimationFrame</a> - Blog post</li>
<li><a class="external" href="http://paulirish.com/2011/requestanimationframe-for-smart-animating/">requestAnimationFrame for smart animating</a> - Blog post</li>
<li><a class="external" href="http://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/">Animating with javascript: from setInterval to requestAnimationFrame</a> - Blog post</li>
<li><a class="external" href="http://blogs.msdn.com/b/ie/archive/2011/07/05/using-pc-hardware-more-efficiently-in-html5-new-web-performance-apis-part-1.aspx">Using PC Hardware more efficiently in HTML5: New Web Performance APIs, Part 1</a> - Blog post</li>
</ul>
|