JavaScript Procedural Sky Texture

Easy Procedural Clouds with Canvas API

Live stream set for 2025-06-20 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.

Creating a Simple Blue Sky with Procedural Clouds Using Vanilla JavaScript

Procedural textures can bring life and realism to web graphics without relying on external images or libraries. In this tutorial, we’ll create a simple yet visually appealing blue sky with soft, fluffy clouds entirely in vanilla JavaScript – no dependencies needed.

You’ll learn how to use value noise to generate natural-looking clouds and add subtle color variation to the sky to avoid flatness. Plus, you’ll get handy buttons to generate new cloud patterns and download your creation.

What Is Value Noise?

Value noise is a type of gradient noise used to create smooth, natural random patterns. It works by generating a grid of random values and smoothly interpolating between them based on the pixel’s position.

Compared to simpler random noise (which can look grainy and harsh), value noise produces softer, more organic patterns – perfect for clouds, terrain, water ripples, and more.

A CAPTCHA presents a challenge that is relatively easy for humans to complete but difficult for bots to replicate.

The Core Concepts in Our Code

1. The Noise Grid

We create a grid of random values – in this case, a 17×17 grid (gridSize = 16 means 16 cells between 17 points). These values are the “seed” for our noise.

2. Interpolating Noise

For each pixel, we calculate where it lies between four surrounding grid points and smoothly interpolate the random values of those points. This interpolation creates smooth transitions and avoids hard edges.

3. Generating Clouds

Clouds are generated by taking the noise value to a power greater than 1 (here, Math.pow(valueNoise, 2.5)), which makes bright areas puffier and darker areas smaller. We then threshold the noise to keep only denser parts as clouds, blending those with the sky.

4. Adding Sky Variation

To avoid a flat, boring sky, we add a second layer of low-frequency noise that subtly modulates the blue and green channels, simulating the natural variations you might see in a real sky.

The User Interface

Two buttons allow you to:

  • Generate Clouds: Creates a fresh noise grid and redraws the sky with new clouds.
  • Download Texture: Saves the current canvas as a PNG image for you to use or share.

The Complete Code Breakdown

HTML Canvas And Buttons

   <!-- Canvas element for drawing -->
   <canvas id="canvas" width="512" height="256"></canvas>

   <!-- Buttons -->
   <button id="generateBtn">Generate Clouds</button>
   <button id="downloadBtn">Download Texture</button>
   
The canvas displays the generated sky texture, while the buttons control regeneration and downloading.

   // Create a grid of random values
   function generateGrid() {
     // ...
   }
   
Generates the noise grid used to calculate smooth noise values.

   // Interpolates noise values for smoothness
   function valueNoise(x, y, scale = 1.0) {
     // ...
   }
   
Calculates interpolated noise based on the grid for any pixel.

   function render() {
     // For each pixel:
     // - Calculate cloud noise and threshold it
     // - Calculate low-frequency sky noise for color variation
     // - Blend sky color and cloud white based on noise
   }
   
The core rendering loop where the pixels get their colors based on noise and thresholds.

   // Event handlers for buttons
   document.querySelector('#generateBtn').addEventListener('click', generate);
   document.querySelector('#downloadBtn').addEventListener('click', download);
   
Hooks the buttons to the functions that regenerate and download the image.

Why Vanilla JavaScript?

Using plain JavaScript and the Canvas API gives you full control and flexibility, without extra dependencies. You can easily integrate this into any web project, customize it, or extend it for animations or interactive skies.

Explanation

  1. Generate a HTML form to hold elements for the sky texture.
  2. The HTML elements will be styled using CSS rules.
  3. The generate button generates a new sky texture.
  4. The download button generates a PNG file.

Desktop Web Browser Unstyled Sky Texture
Desktop Web Browser Displaying Unstyled Procedural Sky Texture

Desktop Web Browser Styled Sky Texture
Desktop Web Browser Displaying Styled Procedural Sky Texture

Desktop Web Browser Downloaded Sky Texture
Desktop Web Browser Displaying Downloaded Sky Texture

Video Displaying The Creation Of A Procedural Sky Texture

Usage

  • You may use the any IDE or text editor including the Learning JavaScript Course Web IDE in a new browser window.
  • Open your web browser.
  • Navigate to the URL or drag and drop the HTML file into the web browser.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. The permissive license requires the preservation of the copyright notice and disclaimer. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

CSS specifications are maintained by the World Wide Web Consortium (W3C) and is licensed under the W3C Software License by web browser vendors. The permissive license requires the preservation of the copyright notice and disclaimer. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

Conclusion:

In this tutorial, we explored how to create a dynamic blue sky with soft procedural clouds using vanilla JavaScript and the HTML5 Canvas API – no libraries required. By generating smooth value noise, we created realistic cloud patterns and subtly varied the sky color to avoid a flat appearance. The project includes interactive buttons to regenerate the sky and download the final texture as a PNG image. This simple yet powerful technique is a great introduction to procedural graphics in the browser..

If you enjoy this article, consider supporting me by purchasing one of my OjamboShop.com Online Programming Courses or publications at Edward Ojambo Programming Books or simply donate here Ojambo.com Donate

References:

Leave a Reply

Your email address will not be published. Required fields are marked *