🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

react-noise

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-noise

A light-weight react component for creating noise textures

npmnpm
Version
0.1.1
Version published
Weekly downloads
299
124.81%
Maintainers
1
Weekly downloads
 
Created
Source

react-noise

react-noise is a light-weight react component that helps achieve a high-def 'noisy' texture effect. It supports both static and animated rendering as well as themeing.

https://github.com/Moniet/react-noise/assets/20152320/6f46e060-3715-46b8-8a70-85ffa3a9f383

Installation

// with pnpm :
pnpm i react-noise

//with npm :
npm i react-noise

// with yarn
with yarn : yarn add react-noise`

Usage

Without config


import { Noise } from "react-noise"
import "react-noise/index.css" // 🚧 required for base styling and animation

const cardStyle = {
  width: '100%',
  height: '100%',
  borderRadius: '20px'
  backgroundImage: "var(--bg-image)"
}

const labelStyle = {
  fontSize: '4rem',
  color: 'white'
}


const NoisyImageCard = ({  bgImage, label, animate }) => {

  return (
    <Noise
      isAnimated={animate} // ✨
      style={{
        ...cardStyle,
        '--bg-image': bgImage
      }}
    >
      <div style={labelStyle}>
        {label}
      </div>
    </Noise>
  )
}

With config

...

useNoiseConfig({ // should be set at top level, e.g App.tsx
  key: "img-noise",  // 🚧 needed as a cache key, and when referencing in the <Noise /> component
  color: "50 50 50", // rgb pattern
  opacity: 0.25      // 0-1 range
}, [deps_array])

const NoisyImageCard = ({  bgImage, label, animate }) => {
  return (
    <Noise
      isAnimated={animate} // ✨
      style={{
        ...cardStyle,
        '--bg-image': bgImage
      }}
      noiseKey="img-noise" // this refers to the cached noise config, see "Noise Config" to learn more
    >
      <div style={labelStyle}>
        {label}
      </div>
    </Noise>
  )
}

Noise Component Props

PropTypeDefault Value
opacitynumber1
classNamestring""
noiseKeystring"default"
isAnimatedbooleanfalse
styleCSSProperties{}

Noise Config

The noise config enables :

  • Dark/Light mode theming
  • Controlling noise resolution (can help save memory)
  • Setting noise opacity
  • Setting the noise color

Usage

  const isDarkMode = useDarkMode()

  useConfig({
    key: 'custom',
    color: isDarkMode ? "255 255 255" : "0 0 0",
    opacity: isDarkMode ? 0.2 : 0.5 // from 0-1
  }, [isDarkMode])  // taking the dependency arr to re-render

    useConfig({
    key: 'custom-2',
    color: isDarkMode ? "50 50 50" : "10 10 10",
    opacity: isDarkMode ? 0.25 : 0.45
  }, [isDarkMode])

  <Noise noiseKey="custom"  />
  <Noise noiseKey="custom-2" />

useNoiseConfig props

PropertyTypeDefault ValueDescription
keystring'default'Cache key to track configs
colorstring'50 50 50'RGB values representing the color
opacitynumber0.1Opacity value, ranging from 0 to 1
resolutionobject1920x1080pxDefinition of the Noise

🚧 Config Tip

To save memory & computation, it may be advisable to reduce the resolution if you don't require default 1920x1080 size.

How it works

The noise is created using the canvas api. To improve performance the noise is only rendered off-screen, once per noise config. This rendering (kind-of) happens in the useNoiseConfig. This is why we pass the noiseKey to the Noise component so we can retrieve the cached canvas value and then render the noise based on those settings.

🏗️ Coming Soon

The SSR only version of the component is in the works. Ideally we should be able to generate the noise image on the server-side to reap the benefits RSC.

This can be potentially achieved through the node canvas library. The downside might be the large img file we would need to generate per component.

References

Keywords

texture

FAQs

Package last updated on 10 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts