Core API
classList.toggle()
DOM animations usually work best when JavaScript toggles state and CSS handles motion.
panel.classList.toggle("is-open");
/* CSS owns the transition. */
Graphics, Media & UI
DOM animations usually work best when JavaScript toggles state and CSS handles motion.
Visual JavaScript should make state easier to understand. The code must stay connected to real data, user intent and accessibility.
A polished interface is not only animation. It is timing, feedback, labels, fallback states and predictable interaction.
classList.toggle()
Use motion or graphics to explain state, not to distract from it.
Keep text, labels and keyboard alternatives available.
Avoid layout-heavy loops and unnecessary repaints.
Examples
panel.classList.toggle("is-open");
/* CSS owns the transition. */
panel.style.height = "0px"; panel.style.height = "400px"; // Layout calculations become fragile quickly.
Code patterns
Use these examples when turning data, state and interaction into something visible.
A direct example of the topic.
panel.classList.toggle("is-open");
/* CSS owns the transition. */
A shortcut that makes the interface fragile.
panel.style.height = "0px"; panel.style.height = "400px"; // Layout calculations become fragile quickly.
Users may ask the browser for less animation.
const prefersReducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
if (!prefersReducedMotion) {
element.animate(keyframes, options);
}
Visuals should not be the only source of meaning.
<figure> <canvas aria-label="Monthly revenue chart"></canvas> <figcaption>Revenue increased over four months.</figcaption> </figure>
Rules that matter
Motion, canvas and drag/drop can improve a product, but only when they stay usable and understandable.
Do not hard-code charts or status when data should drive them.
Canvas, charts and drag/drop need labels or fallback content.
JavaScript should orchestrate, CSS can animate state.
Repeated measurements and writes can hurt performance.
Reduced motion and keyboard use are part of production design.
Mouse, keyboard and touch can behave differently.
Production thinking
Graphics and interaction are where users feel quality immediately. Small implementation choices decide whether the UI feels smooth or chaotic.
Visual UI needs semantic labels, focus management, reduced motion support and alternatives for pointer-only behavior.
Production visual code should be measured, tested on real devices and kept separate from business logic where possible.
Important text should not exist only inside canvas pixels or animation frames.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these before moving to the next graphics and UI lesson.
Senior audit upgrade