SVG con CSS · 04

stroke-dasharray y pathLength

Tus donuts usan stroke-dasharray (44 veces) calculando la circunferencia con 2·π·r. El atributo pathLength elimina ese cálculo: le dice al navegador cuánto debe "medir" el trazo, y a partir de ahí los guiones se escriben en tus unidades. Con 100, en porcentajes.

La idea nueva: pathLength="100" + stroke-dasharray: 45 100 pinta el 45 % del círculo, sea cual sea su radio. Cada segmento siguiente se desplaza con un stroke-dashoffset negativo igual al acumulado. Y con pathLength="1" cualquier línea se puede "dibujar" animando el offset de 1 a 0, sin medirla con getTotalLength().
8 5 · 0 8 round · 14 4 2 4
pathLength="100"
pathLength="1" + dashoffset

El CSS



    

Ejercicios

  1. Rehaz donut-dasharray.html con pathLength="100" y comprueba cuántas líneas de JS desaparecen.
  2. Deja un hueco de 1 % entre segmentos restando 1 a cada dasharray.
  3. Anima los segmentos del donut desde 0 con @property --a (serie Variables, lección 08).