Skip to content

A react component that draws a starfield on a canvas element.

License

Notifications You must be signed in to change notification settings

impaler/starfield-react

Repository files navigation

Starfield

A retro Star Field Space effect in a html canvas element through a React Component and or Hook.

See a live editable demo here:

Edit starfield-react example

Install from npm:

`npm install starfield-react`
`yarn add starfield-react`
  1. Use the React Component StarField directly in your React app:
import { StarField } from 'starfield-react'
import { render } from 'react-dom'

render(<StarField/>, document.querySelector('body'))

Or use the React Hook useStarField to have more control:

import { useStarField } from 'starfield-react'

const StarFieldCustom = (props) => {
  const canvasRef = useRef<HTMLCanvasElement>(null)
  const stateReference = useRef<StarFieldState>(createStarsState({
    count,
    height,
    width,
  }))

  // The same hook used in the `StarField` component above:
  useStarField(canvasRef, options, stateReference)

  return <canvas
    ref={canvasRef}
    {...{ width, height }}
    {...restProps}
  />
}

Documentation on the StarField Component props are on the Props interface type in src/StarField.tsx.

Examples of different props in action: