[Feature] Gradients: tween stops under `motion()`, and support `gradientUnits`

Abierto
#191 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
30/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
d3, typescript

Línea de trabajo

Start with the repro in the issue: the vitest case for stop offsets uses motion() and createChartScene, and the focus-guide case uses surface.paintFocus. The relevant code is motionAttributes and the update track in packages/charts-core/src/motion.ts, renderGradients in svg.ts, focusGuideRenderHooks in svg-renderer.ts, and svg-surface.ts. Done means the stop offset changes across frames and the guide stroke resolves to url(#a-fade). Gradient units are a separate change, and the issue's own pattern and design notes suggest splitting it up.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Version: @tanstack/charts 1.1.0 (57774e1).

Description

Gradients in definition.gradients have two gaps: stops snap instead of easing with motion() (A), and they cannot paint zero-width or zero-height strokes (B).

A. motion() does not tween gradient stops

Repro: save as packages/charts-core/src/repro.test.ts, run pnpm vitest run packages/charts-core/src/repro.test.ts.

import { expect, it, vi } from 'vitest'
import { scaleLinear } from 'd3-scale'
import { areaY } from './area'
import { crosshair } from './crosshair'
import { motion } from './motion'
import { createChartScene, defineChart } from './scene'

const make = (end: number, top: number) =>
  createChartScene(defineChart({
    marks: [
      areaY([{ x: 0, y: 1 }, { x: 1, y: top }, { x: 2, y: 2 }], { x: 'x', y: 'y', fill: 'url(#fade)' }),
      crosshair({ y: false, stroke: 'url(#fade)' }),
    ],
    scales: { x: { scale: scaleLinear().domain([0, 2]) }, y: { scale: scaleLinear().domain([0, 4]) } },
    gradients: [{ id: 'fade', stops: [
      { offset: 0, color: '#2563eb', opacity: 0.6 }, { offset: end, color: '#2563eb', opacity: 0 }] }],
  }), { width: 320, height: 200 })
const opts = { ariaLabel: 't', idPrefix: 'a' }

it('A: stop offset eases with the update transition', () => {
  const cbs: FrameRequestCallback[] = []
  vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cbs.push(cb))
  const el = document.createElement('div')
  const surface = motion({ initial: false, transition: { type: 'tween', duration: 500 } }).mount(el, () => {})
  surface.render(make(0.5, 3), opts)
  surface.render(make(1, 2), opts)
  const stop = el.querySelector('[data-ts-key="gradient:fade:stop:1"]')!
  const offsets = [stop.getAttribute('offset')]
  for (let t = 0; t <= 600; t += 100) {
    cbs.splice(0).forEach((cb) => cb(t))
    offsets.push(stop.getAttribute('offset'))
  }
  console.log('offsets:', offsets.join(' '))
  expect(new Set(offsets).size).toBeGreaterThan(2)
})

Actual output:

offsets: 100% 100% 100% 100% 100% 100% 100% 100%
AssertionError: expected 1 to be greater than 2

Expected: offset and stop-opacity (ideally stop-color) interpolate with the update transition.

Proposal: add them to the update track for stops, with a timing context that reuses the root transition (for example a gradient role in ChartMotionRole).

Downstream measurement (bklit migration, Chrome, data-driven area fade): the gradient end moved about 18 px per update and snapped in one frame while the area eased.

B. No gradientUnits
  • Gradients have no units field (types.ts:644-675) and renderGradients emits no gradientUnits (svg.ts:30-48), so SVG uses objectBoundingBox.
  • A zero-width or zero-height stroke (ruleX, ruleY, crosshair) has an empty bounding box, so the gradient paints nothing. The docs state objectBoundingBox (chart-spec.md:179-180).

Proposal: units?: 'objectBoundingBox' | 'userSpaceOnUse' on ChartGradientBase (default unchanged), emitted as gradientUnits.

C. Focus-guide nodes do not scope url(#id)

Moved to #211 (a bug, separate from these two features). The repro above already sets idPrefix: 'a' and a gradient crosshair for it.

Path clip (this comment)

Moved to #216, together with the <mask> ask from #128.

Related upstream

#129 (radial gradients, shipped in #147) and #128 / PR #166 (<pattern> resources) are different gaps.

Lenguaje dominante
TypeScript
Estrellas
870
Forks
57
Merge medio
11 h 58 min
PR fusionados (30 d)
18

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/charts

Todos los issues de TanStack/charts

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.