useAdaptiveItem

Hook to extract color from any DOM element including videos, images, canvas, or React components.

Import

tsx
import { useAdaptiveItem } from "react-cinematic-lighting";

Basic Usage

tsx
import { useAdaptiveItem } from "react-cinematic-lighting";
import { useRef } from "react";

function VideoPlayer() {
  const videoRef = useRef<HTMLVideoElement>(null);
  const { color, start, stop } = useAdaptiveItem(videoRef, {
    colorExtraction: true,
    fps: 10,
    sampling: 10,
  });

  return (
    <div
      style={{
        background: color 
          ? `radial-gradient(circle, rgba(${color.r}, ${color.g}, ${color.b}, 0.3) 0%, transparent 70%)`
          : "#000",
      }}
    >
      <video ref={videoRef} src="video.mp4" controls />
    </div>
  );
}

Advanced Usage

tsx
function CustomComponent() {
  const elementRef = useRef<HTMLDivElement>(null);
  const { color, start, stop } = useAdaptiveItem(elementRef, {
    extractMode: "computed",
    fps: 8,
    sampling: 15,
    onColorChange: (color) => {
      console.log('Color changed:', color);
      // Update theme, send analytics, etc.
    },
  });

  return (
    <div ref={elementRef}>
      {/* Your content */}
    </div>
  );
}

Parameters

refrequired

Type: React.RefObject<HTMLElement | null>

Reference to the DOM element to extract color from.

optionsrequired

Type: AdaptiveItemOptions

Configuration options for color extraction.

id?: string

Unique identifier (auto-generated if not provided)

colorExtraction?: boolean

Enable/disable extraction. Default: true

fps?: number

Limit updates per second. Default: 10

sampling?: number

Pixel sampling step, higher = faster. Default: 10

extractMode?: 'auto' | 'video' | 'canvas' | 'computed'

Extraction method. Default: 'auto'

onColorChange?: (color: ColorRGB) => void

Callback when color changes

disabled?: boolean

Disable extraction temporarily

Return Value

color

Type: ColorRGB | null

Current extracted color with r, g, b values (0-255), or null if no color extracted yet.

start

Type: () => void

Manually start color extraction.

stop

Type: () => void

Manually stop color extraction.

💡 Tip

Use the extractMode option to force a specific extraction method. The 'auto' mode automatically detects the element type and uses the appropriate extractor.

Built with v0