All Posts

Creating Digital Art with Math

One of the many ways we can use mathematics and algorithms to create unique patterns and designs.

I was exploring the idea of using some algorithm to create me a random covers based on a page hashed slug.

That’s exactly what i set out to do when I stumbled upon the concept of generative art is a form of digital art driven by mathematical functions.

Inspired by krzysckh's bitart, I decided to build my own tool, pattern, to bring these ideas to life. In this writing, I’ll walk you through how it works, break down some examples, and share tips for creating your own mathematical art.

What is Generative Art?

Generative art is artwork created with the use of an autonomous system, typically involving algorithms, computer code, or other procedural processes. The artist defines a set of rules, parameters, or instructions, and the system generates the artwork, often producing unique, unpredictable, or evolving results.

At its core, mathematical pattern art involves using formulas to determine the color of each pixel on a canvas. By defining a function that takes a pixel’s coordinates and the canvas dimensions as inputs, we can generate different designs, from smooth gradients to complex geometric patterns.

How It Works

It uses a mathematical function to calculate the color of each pixel on a canvas. The function exposes four optional arguments:

  1. x: number is horizontal coordinate of the pixel.
  2. y: number is vertical coordinate of the pixel.
  3. w: number is width of the canvas.
  4. h: number is height of the canvas.

The function returns a value in the range 0–255, which represents the grayscale intensity of the pixel (0 for black, 255 for white) in the RGB color space. You can use JavaScript’s Math object functions like sin, cos, or random to craft dynamic patterns.

Examples

For demonstration purposes and to not overload device, the examples are rendered in fixed 256x256 px resolution. It allows to change things on a fly.

Let’s start with something simple: a horizontal gradient.

Horizontal Gradient

The formula (x * 255 / w) creates a smooth transition from black to white across the canvas. Here’s how it works:

This is a great starting point because it shows how a simple formula can create a visually appealing effect.

Now, let’s try something with more structure: a pattern of diagonal lines.

Diagonal Lines

This formula, (x + y) % 50 === 0 ? 120 : 35, generates a pattern of 45-degree lines. Let’s break it down:

The result is a grid of diagonal lines, showcasing how conditional logic can create geometric patterns.

Let’s explore a more complex pattern: a hexagonal grid.

Hexagonal Grid

This formula creates a hexagonal grid by combining modular arithmetic and conditional logic:

The result is a honeycomb-like structure, demonstrating how layered math can produce intricate designs.

For something more organic, let’s try a topographic-inspired pattern with a touch of noise.

Topographic Waves

This formula creates a wavy, radial topographic-like pattern:

This produces a fluid, almost topographic effect, showing how trigonometric functions can create organic visuals.

Playground

Playground includes a gallery of pre-build patterns to start with. You can render, modify, or use it for your own designs.

Visit playground to explore the full collection and start experimenting.