Skip to content

BjornMelin/dev-skills / archive/skills/native/native-skia

native-skia

Verified

Use this skill for React Native Skia canvas animations, drawing primitives, Reanimated integration, CanvasKit/web caveats, performance, memory, and Expo/native validation. Trigger on React Native Skia, @shopify/react-native-skia, Skia Canvas, CanvasKit, Skia animation. Do not use for near-miss tasks outside these boundaries; route to adjacent motion or platform skills when they own the implementation.

references/docs-software-mansion-canvas-atlas.md

View on GitHub
# Atlas: Batched Sprite and Tile Animation

The `Atlas` component renders many instances of the same texture in a single
draw call with per-instance transforms. Use it for tile maps, sprite
animations, and any scene with hundreds of similar objects. Keep sprite
rectangles, texture dimensions, and transform buffers stable; per-frame array
allocation can erase the draw-call win.

Combine with `useTexture` (creates the texture on the UI thread) and
`useRSXformBuffer` (efficiently animates per-sprite transforms in a worklet).
Use `useRectBuffer` when sprite rectangles are generated or animated on the UI
thread:

```tsx
import {
  Skia,
  Canvas,
  Atlas,
  Rect,
  Group,
  rect,
  useTexture,
  useRSXformBuffer,
} from '@shopify/react-native-skia';
import { useSharedValue } from 'react-native-reanimated';
import { GestureDetector, Gesture } from 'react-native-gesture-handler';
import { useMemo } from 'react';

const size = { width: 25, height: 11.25 };
const strokeWidth = 2;
const textureSize = {
  width: size.width + strokeWidth,
  height: size.height + strokeWidth,
};

export const SpriteGrid = () => {
  const pos = useSharedValue({ x: 0, y: 0 });
  const texture = useTexture(
    <Group>
      <Rect
        rect={rect(strokeWidth / 2, strokeWidth / 2, size.width, size.height)}
        color="cyan"
      />
      <Rect
        rect={rect(strokeWidth / 2, strokeWidth / 2, size.width, size.height)}
        color="blue"
        style="stroke"
        strokeWidth={strokeWidth}
      />
    </Group>,
    textureSize
  );

  const gesture = Gesture.Pan().onChange((e) => (pos.value = e));
  const count = 150;
  const gridWidth = 256;

  const sprites = useMemo(
    () =>
      new Array(count)
        .fill(0)
        .map(() => rect(0, 0, textureSize.width, textureSize.height)),
    []
  );

  const transforms = useRSXformBuffer(count, (val, i) => {
    'worklet';
    const tx = 5 + ((i * size.width) % gridWidth);
    const ty = 25 + Math.floor(i / (gridWidth / size.width)) * size.width;
    const r = Math.atan2(pos.value.y - ty, pos.value.x - tx);
    val.set(Math.cos(r), Math.sin(r), tx, ty);
  });

  return (
    <GestureDetector gesture={gesture}>
      <Canvas style={{ flex: 1 }}>
        <Atlas image={texture} sprites={sprites} transforms={transforms} />
      </Canvas>
    </GestureDetector>
  );
};
```

## RSXform

Atlas transforms use a compressed rotation-scale-translate matrix `[scos, ssin, tx, ty]`:

```tsx
// Identity (no transform)
Skia.RSXform(1, 0, 0, 0);

// Scale by 2, translate to (50, 100)
Skia.RSXform(2, 0, 50, 100);

// Rotate by PI/4, translate to (50, 100)
const r = Math.PI / 4;
Skia.RSXform(Math.cos(r), Math.sin(r), 50, 100);

// Scale by 2, rotate by PI/4 with pivot (25, 25)
Skia.RSXformFromRadians(2, r, 0, 0, 25, 25);
```

## Validation

- Confirm the texture is non-null before drawing and provide a stable fallback
  for loading/error states.
- Profile low-end Android when sprite count, texture size, or animated
  transforms are high.
- Verify route unmount/remount and background/foreground do not leak repeating
  animations or hold stale texture handles.
- Use RN overlays for controls, hit targets, and accessibility labels; the
  atlas pixels are not semantic UI.
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall