10.0.10 ā€¢ Published 6 months ago

react-digital-rain v10.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

react-digital-rain šŸ’Š šŸ”“ šŸ”µ āš” šŸŸ¢ šŸŒ§ļø

āž”ļø npmjs

This component renders neon digital rain on a black background. It fits its container. It can be enabled to go fullscreen when clicked on. This is probably best used as a screensaver, in fullscreen mode.

npm install react-digital-rain

This component uses one gif to fit any screen with no loss of resolution or stretching.

Problem:

/* styles.css */
body {
  background-image: url("/images/digital_rain.gif");
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

This results in stretching for most screens and a blurry experience.

Solution:

This component uses a single gif, appending it over and over to fill the screen and timing it so that the rain looks continuous. This is possible with rain.

Positioning: We use one outer div and one inner. The inner is going to calculate the gifs for a little larger than the height/width of the container. The outer is the dimensions of the container. Turns out the browser has eccentricities when it comes to gifs. We will talk about that later. Gifs are positioned statically in columns and rows. Each row gets a 2450ms delay, which is the speed of the rain over 400 css pixels. The animation travels downward at roughly 166 css pixels per second. This achieves a seemless transition from tile to tile that fits on all screen sizes.

Browser Eccentricities - Caching: A word on caching and timing - A 500x400 gif "tile" is served to the browser with the \ tag. If we simply use the \ tag with src, all instances are given the same gif start time by the browser. We cannot start sequentially, even if they are appended to the DOM at a later point in time. We can break this behavior by adding a random query string to the end of the \ attribute. The downside is that this breaks the native browser caching and forces the browser to request the \ on each render. We now have timing but are left with multiple expensive and slow operations. The solution is the blob (binary large object). With the blob we have control over \ timing AND we have control over caching. The blob is our manual override to cache this gif ourselves.

Browser Eccentricities - gifs: the browser pauses gifs when they are out of view to conserve computing resources. When switching between tabs, resizing the window or resizing the parent element container, this component will simply restart the animation to regain synchronicity within the browser.

fullScreen?: boolean // enters fullscreen when clicked. Defaults to false.

animationSeconds?: number // the animation duration in seconds. If not provided, the animation duration will be calculated based on screen height
<DigitalRain />

//no animation and fullscreen

<DigitalRain fullScreen animationSeconds={0} />
//static import is discouraged (8mb will be included in the main bundle)
import DigitalRain from "react-digital-rain";

//code splitting is encouraged

const DigitalRain = React.lazy(() => import("react-digital-rain"));

const App = (props) => {
  return (
    <React.Suspense fallback={<div>Loading...</div>}>
      <DigitalRain />
    </React.Suspense>
  );
};

export default App;
//For added control of full screen feature
//I've exposed a hook called useFullScreen to know when to show/hide stuff,
//such a hiding a navbar when fullscreen.

/**
 * copy/paste or use similar
 * the following is a HOC to dynamically load everyting in one component
 * this is to satisfy the React rule of hooks
 * hook will be present on every render
 */
const withLazy = (
  WrappedComponent,
  importModule,
  propsExtractor,
  fallback = null
) => {
  return (props) => {
    const [loadedModule, setLoadedModule] = React.useState(null);

    React.useEffect(() => {
      importModule().then((module) => setLoadedModule(propsExtractor(module)));
    }, []);

    return loadedModule ? (
      <WrappedComponent {...loadedModule} {...props} />
    ) : (
      fallback
    );
  };
};

const LazyDigitalRain = withLazy(
  (props) => {
    const { DigitalRain, useFullScreen, setShow, ...rest } = props;
    const { isFullScreen, screenfull } = useFullScreen();

    React.useEffect(() => {
      setShow(!isFullScreen);
    }, [isFullScreen]);

    return <DigitalRain {...rest} />;
  },
  () => import("react-digital-rain"),
  (module) => ({
    DigitalRain: module.DigitalRain, //DigitalRain is both named and default, FYI
    useFullScreen: module.useFullScreen,
  }),
  <div>...Loading</div>
);

import NavBar from "@someNavBarPackage";

const App = () => {
  const [show, setShow] = React.UseState(true);

  return (
    <>
      {show && <NavBar />}
      <LazyDigitalRain fullScreen setShow={setShow} />
    </>
  );
};

export default App;
10.0.5

6 months ago

10.0.6

6 months ago

10.0.7

6 months ago

10.0.8

6 months ago

10.0.9

6 months ago

10.0.0

6 months ago

10.0.1

6 months ago

10.0.2

6 months ago

10.0.3

6 months ago

10.0.4

6 months ago

10.0.10

6 months ago

9.4.0

8 months ago

9.2.2

8 months ago

9.2.1

8 months ago

9.1.0

8 months ago

9.3.0

8 months ago

9.2.0

8 months ago

9.0.1

8 months ago

9.0.0

8 months ago

5.6.0

9 months ago

5.4.2

10 months ago

5.5.0

10 months ago

5.4.1

10 months ago

5.4.0

10 months ago

5.3.0

10 months ago

5.2.0

10 months ago

5.1.0

10 months ago

5.0.1

10 months ago

5.0.0

10 months ago

6.0.1

9 months ago

6.0.0

9 months ago

6.0.2

9 months ago

2.2.1

10 months ago

2.0.3

10 months ago

2.2.0

10 months ago

2.0.2

10 months ago

2.0.5

10 months ago

2.2.2

10 months ago

2.0.4

10 months ago

7.0.0

9 months ago

2.1.0

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

3.0.3

10 months ago

3.0.1

10 months ago

8.0.0

9 months ago

3.0.0

10 months ago

4.1.0

10 months ago

4.0.1

10 months ago

4.0.0

10 months ago

4.2.0

10 months ago

5.7.3

9 months ago

5.7.2

9 months ago

5.7.1

9 months ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago