Add Film Grain & Noise Textures to Images and Backgrounds with Pixlated Web Components
The library currently comes with 2 custom elements, <pixlated-image> and <pixlated-bg>, to handle images and container backgrounds.
<pixlated-image> for image grain effects and <pixlated-bg> for background noise textures.1. Install & import pixlated with NPM.
# NPM $ npm install pixlated import 'pixlated';
2. For static sites or quick demos, use the CDN link. You can load both components or just the one you need.
<script src="https://unpkg.com/pixlated/src/pixlated.js"></script> <script src="https://unpkg.com/pixlated/src/pixlated-bg.js"></script>
3. Apply a grain effect directly to an image using the <pixlated-image> component. It works just like a standard <img> tag but renders the image on a canvas.
Available Props:
| Attribute | Description | Default |
|---|---|---|
src | (Required) The URL of the image to display. | null |
intensity | Noise intensity from 0 (none) to 1 (max). | 0.1 |
width | The width of the canvas in pixels. | 400 |
height | The height of the canvas in pixels. | 400 |
alt | Alternative text for accessibility, just like <img>. | '' |
<pixlated-image src="path/to/your/photo.jpg" intensity="0.2" width="600" height="400" alt="A photo with a subtle grain effect." > </pixlated-image>
4. Add a noisy texture to the background of any container element.
Available Props:
| Attribute | Description | Default |
|---|---|---|
intensity | Noise intensity from 0 (none) to 1 (max). | 0.1 |
color | The background color in any valid CSS format. | #09090b |
width | A fixed width in pixels. If omitted, it’s responsive. | auto |
height | A fixed height in pixels. If omitted, it’s responsive. | auto |
<pixlated-bg intensity="0.15" color="#1a1a1a"> <h1>Your Hero Title</h1> <p>This container has a nice, subtle texture.</p> </pixlated-bg>
5. Style the web components with CSS. They behave like standard block-level or inline-block elements.
pixlated-image {
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
display: block;
margin: 2rem auto;
}
pixlated-bg {
padding: 2rem;
border-radius: 8px;
min-height: 300px;
} 6. API methods.
// reload()
// Redraws the grain effect with current attributes.
// Useful when you need to regenerate the random noise pattern.
const image = document.querySelector('pixlated-image');
image.reload();
// getConfig()
// Returns an object with the current configuration values:
const config = image.getConfig();
// Returns: { src, intensity, width, height, alt } 7. Event handlers.
// pixlated:loaded
// Fires when the source image successfully loads.
// The event detail includes the image source URL and natural dimensions.
image.addEventListener('pixlated:loaded', (e) => {
console.log('Image loaded:', e.detail);
// { src: 'photo.jpg', width: 1920, height: 1080 }
});
// pixlated:error
// Fires when the image fails to load.
// The event detail includes the attempted source URL and error information.
image.addEventListener('pixlated:error', (e) => {
console.error('Load failed:', e.detail);
// { src: 'photo.jpg', error: 'error', message: 'Failed to load image' }
}); Q: The grain effect looks different on retina displays compared to standard screens. How do I ensure consistency?
A: Pixlated automatically handles device pixel ratio scaling by multiplying the canvas dimensions by window.devicePixelRatio.If you’re seeing differences, check that you haven’t overridden the canvas styles or scaled the element with CSS transforms.
Q: My images are on a different domain and not rendering. What’s causing this?
A: The component sets crossOrigin="anonymous" on the image element to allow canvas manipulation of cross-origin images. Your image server needs to send the appropriate CORS headers (Access-Control-Allow-Origin) for this to work.
Q: Does changing the intensity attribute dynamically update the effect immediately?
A: Yes, both components observe attribute changes and redraw automatically. You can update intensity, color, dimensions, or any other attribute and see the changes immediately. The component uses attributeChangedCallback to detect changes and triggers a redraw.
Q: How does this perform compared to using CSS filters for a similar effect?
A: Canvas-based grain generally performs better for static effects because the rendering happens once rather than continuously. CSS filters run on every frame and can cause repaints during scrolling or animations. The tradeoff is that canvas requires more initial setup and JavaScript execution.
The post Add Film Grain & Noise Textures to Images and Backgrounds with Pixlated Web Components appeared first on CSS Script.
The DJI Romo robot vacuums. | Image: DJI On Valentine's Day, I brought you a…
Magic: The Gathering’s crossovers get harder to predict, and the second set of the year…
Pixar's Hoppers, about a young animal rights activist (Piper Curda) who transfers her mind into…
After more than five years since the last book in the series was published, Sarah…
A giant power line was knocked down Friday evening after a truck crashed into it.
Students at Caledonia Elementary School were sent home Friday morning after heavy rainfall caused flooding…
This website uses cookies.