Files
fn_registry/frontend/functions/ui/use_animated_canvas.md
T
egutierrez 87e8f33b01 fix: corregir lang y file_path en frontmatter de funciones existentes
Normaliza lang: typescript → ts en funciones frontend y corrige file_path de functions/infra/ → functions/browser/ en funciones CDP. Actualiza referencias cn_typescript_core → cn_ts_core.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 03:23:20 +02:00

1.6 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path, props, emits, has_state, framework, variant
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports tested tests test_file_path file_path props emits has_state framework variant
use_animated_canvas component ts ui 1.0.0 impure useAnimatedCanvas(options: AnimatedCanvasOptions): AnimatedCanvasResult Hook React para canvas animado a N fps via requestAnimationFrame. Maneja DPR, resize, throttling, y contador de FPS real.
canvas
animation
fps
requestAnimationFrame
hook
realtime
component
ui
false
react
false
frontend/functions/ui/use_animated_canvas.tsx
name type required description
fps number false Target FPS (default 60)
name type required description
draw (ctx: CanvasRenderingContext2D, width: number, height: number, frameCount: number) => void true Callback de renderizado
true react
default

Ejemplo

const { canvasRef, renderFpsRef } = useAnimatedCanvas({
  fps: 100,
  draw: (ctx, w, h) => {
    // Dibujar lo que sea a 100fps
    ctx.fillStyle = '#3b82f6'
    ctx.fillRect(0, 0, w * Math.random(), h)
  },
})

return <canvas ref={canvasRef} style={{ width: '100%', height: 300 }} />

Notas

  • DPR automático: escala el canvas al devicePixelRatio de la pantalla
  • Resize automático: detecta cambios de tamaño via getBoundingClientRect
  • Throttle configurable: rAF corre a ~144fps nativo, el hook filtra a N fps
  • FPS real: renderFpsRef.current tiene los FPS medidos (no el target)
  • drawRef pattern: actualiza el callback sin re-crear el loop de animación