Pixel Swap HTML
Create a bounded grid of animated pixel windows that reveals one complete HTML layer over another. Preserve the target page's layout and typography; the transition should clarify a state change, not obscure essential interaction.
Build the effect
- Copy
assets/pixel-swap.jsbeside the target HTML, or adapt thePixelSwapclass into the existing JavaScript bundle. - Place exactly two direct content layers inside a host with an explicit size or aspect ratio:
html<div id="swap-card"> <article data-pixel-swap-layer="first">First state</article> <article data-pixel-swap-layer="second">Second state</article> </div> <script src="./pixel-swap.js"></script> <script> const swap = new window.PixelSwap('#swap-card', { trigger: 'hover', pattern: 'random', pixelSize: 64, duration: 1400 }); </script>
- Start from the source-matched defaults. Read
references/options.mdonly when tuning the pattern, timing, geometry, imperative API, or data-attribute initializer. - Use
assets/demo.htmlas the behavior and integration reference, not as required page styling.
Preserve these mechanics
- Each pixel is an overflow-hidden window onto a full clone of the incoming layer. Offset every clone by the pixel's grid position so text, images, and backgrounds remain locked to one shared origin.
- Animate the pixel window and its content with exact inverse scale and rotation keyframes. This prevents the revealed content from shrinking, spinning, or drifting inside each pixel.
- Keep the grid bounded. The bundled implementation caps it at 220 pixels and increases the requested pixel size when necessary.
- Center grid overhang so edge cells remain square. For gaps or rounded pixels, expand the final window scale enough to close coverage without visible seams.
- Freeze grid geometry and animation settings for a transition already in flight. Apply option or resize changes to the next transition.
- Keep the outgoing complete layer visible below the pixel grid until the transition finishes, then reveal the incoming complete layer and remove all cloned pixels.
Interaction and content constraints
- Keep the animated grid
aria-hiddenand inert. Only the currently shown complete layer belongs in the accessibility tree. - Hover mode must also respond to focus and blur. Click mode must support Enter and Space and expose button semantics when the host has no stronger existing role.
- Honor
prefers-reduced-motionby swapping layers immediately with no pixel animation. - Avoid cloning live video, canvas state, iframes, form controls, or other interactive widgets into hundreds of pixels. Use stable text, images, SVG, and decorative HTML; place controls outside the swapped layers when possible.
- Retain
destroy()cleanup for timers, animations, observers, attributes, and event listeners when integrating into a client-routed page.
Verify before delivery
- Test forward and reverse transitions, including leaving hover before the forward transition completes.
- Test every requested pattern at both wide and narrow sizes; confirm edge pixels are not clipped or rectangular.
- Check gap plus rounded-corner combinations for seams at the end of the reveal.
- Confirm manual mode, keyboard activation, reduced motion, resize behavior, and repeated rapid toggles.
Scope and provenance
This skill implements only the Pixel Swap interaction studied at https://reactbits.dev/animations/pixel-swap. The bundled module is an original plain-JavaScript adaptation and does not require React or code from other React Bits effects. Do not present it as the upstream React component.


