Skip to content

Instantly share code, notes, and snippets.

@skeptrunedev
Created June 2, 2025 08:14
Show Gist options
  • Save skeptrunedev/e1f0cf00641fb26bbd0acf937f57c6a5 to your computer and use it in GitHub Desktop.
Save skeptrunedev/e1f0cf00641fb26bbd0acf937f57c6a5 to your computer and use it in GitHub Desktop.
Film grain filter using svg's and HTML

You first need to add a svg filter definition to your HTML file such that the CSS can reference it later to put the grain on top of the background image. The filter uses feTurbulence to create a fractal noise pattern, and feColorMatrix to adjust the opacity. You can experiment with values like baseFrequency in feTurbulence or the alpha channel (the 0.8 in the feColorMatrix) to finetune the grain's intensity and texture.

<!-- SVG noise filter definition - this goes in your HTML -->
<svg style="display: none">
  <filter id="noiseFilter">
    <!-- Creates the fractal noise pattern -->
    <feTurbulence
      type="fractalNoise"
      baseFrequency="0.5"
      numOctaves="3"
      stitchTiles="stitch"
    />
    <!-- Converts noise to semi-transparent overlay -->
    <feColorMatrix
      type="matrix"
      values="0 0 0 0 0
              0 0 0 0 0
              0 0 0 0 0
              0 0 0 0.8 0"
    />
  </filter>
</svg>

Once you have the filter defined, you can apply it to a grain overlay element in your CSS. This element will cover the background image and apply the noise effect.

/* The grain overlay element that applies the filter */
.grain-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2; /* Above background image, below content */
  pointer-events: none; /* Allows clicks to pass through to elements below */
  filter: url(#noiseFilter); /* Applies the SVG filter defined above */
}

/* Background image styling for context */
.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url("./media/16x9steppecommander.png");
  background-size: cover;
  background-position: center;
  z-index: 1; /* Below grain overlay */
}

Finally, you need to structure your HTML to ensure the layering works correctly. The grain overlay should be positioned above the background image but below any content you want to display.

<!-- HTML structure showing the layering -->
<div class="hero">
  <!-- Layer 1: Background image (z-index: 1) -->
  <div class="background-image"></div>

  <!-- Layer 2: Grain overlay (z-index: 2) -->
  <div class="grain-overlay" style="filter: url(#noiseFilter)"></div>

  <!-- Layer 3: Content (z-index: 3) -->
  <div class="content-center">
    <!-- Your content here -->
  </div>
</div>

Be Creative!

While there's ongoing debate about AI generated art, I see Midjourney as just another tool in the toolkit. The key is using it to bring your vision to life, not to replace your creativity.

Take inspiration from what you see, but make it your own. Use AI to bridge the gap between the style you have in your head and what actually shows up on screen. The techniques I've shared here are all about developing your unique voice and letting AI help you express it better.

The goal isn't to generate something generic. It's to create images that actually work for your projects and feel intentional, not obviously AI generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment