npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-star-field-canvas

v1.4.0

Published

Configurable fly-through star field effect for HTML canvas with ReactJS.

Downloads

11

Readme

react-star-field-canvas

Configurable fly-through star field effect for HTML canvas with ReactJS.

https://github.com/usernein/react-star-field-canvas/

Installing

npm install --save react-star-field-canvas

Usage

import { StarField } from "react-star-field-canvas";

export default function App() {
  return (
    <StarField
      followMouse={true}
      numStars={800}
      minSpeed={3}
      maxSpeed={5}
      className="starfield"
    />
  );
}

Options

These are the default props:

{
  followMouse = false,
  followContext = undefined,
  color = { r: 255, g: 255, b: 255 },
  glow = false,
  maxSpeed = 5,
  minSpeed = 2,
  numStars = 400,
  trails = false,
  trailColor = { r: 255, g: 255, b: 255 },
  longerTrails = false,
  pauseOnBlur = true,
  className = "",
}

The followContext option is the element or object to attach the mousemove listener to, if you enable followMouse but need it to respond to something other than the canvas, perhaps if the canvas is behind another fullscreen element. eg. you can use document.body as the context:

<StarField
  followMouse={true}
  followContext={document.body}
  className="starfield"
/>

Important: style the canvas yourself

In case you want to use it for a flying star field effect but with alternate colors (ie. not white stars on black space background), this script does not fill the canvas background at all. It only draws the stars.

You should style the canvas with CSS. There is no need to use height or width attributes on the canvas element since the CSS computed values are applied to the canvas in the script.

You can style the canvas element by passing through the prop className the class names you want to apply.

<StarField
  followMouse={true}
  followContext={document.body}
  className="starfield"
/>
.starfield {
  background-color: black;
  position: fixed;
  width: 100dvw;
  height: 100dvh;
  z-index: -10;
}

Events

If you want to control the star field or change its options without re-rendering the StarField component (which will happen if you change the props with a state, for example), you have three events which you can dispatch:

| Event.name | Description | Event.detail | |--------------------------|------------------------------------------| --- | | star-field-start | Start the star field | undefined | star-field-stop | Stop the star field | undefined | star-field-reset | Reset the star field | undefined | star-field-set-options | Update the star field options on-the-fly | Partial<StarFieldOptions>

Updating options on-the-fly

The following code will change the color of the stars every 2 seconds:

setInterval(() => {
  const changeColor = new CustomEvent("star-field-set-options", {
    detail: {
      color: {
        r: Math.floor((Math.sin(Date.now() / 1000) + 1) * 128),
        g: Math.floor((Math.sin(Date.now() / 1000 + 2) + 1) * 128),
        b: Math.floor((Math.sin(Date.now() / 1000 + 4) + 1) * 128),
      },
    } as StarFieldOptions,
  });

  window.dispatchEvent(changeColor);
}, 2000);

Demo

Play and experiment with the demo at CodeSandbox: https://codesandbox.io/s/react-star-field-canvas-q88fkh/