📘 CodingMarble Learn

Creative Coding: Making Art with Programs

Creative coding means writing programs to make pictures, animation, sound or interactive art. You draw on a canvas with x and y coordinates, where (0, 0) is the top-left corner and y grows downwards. Colours are three numbers (red, green, blue). Loops repeat shapes into patterns, random numbers make every run different (generative art), recursion draws fractals, and events such as a mouse click or a sensor reading let the viewer change the work. Tools like p5.js, Processing, Scratch and Python turtle are made for this.

🎬 Step-by-step story

  1. The canvas is like graph paper, but upside down. (0, 0) is the top-left corner. x grows to the right and y grows downwards. Watch the dot travel to (100, 50).
  2. A shape needs a position, a size and a colour. The colour is three numbers from 0 to 255: red, green and blue. Here, (255, 120, 0) makes orange.
  3. A loop repeats an instruction. One loop draws a row. A loop inside a loop draws a whole grid. Change n: one line of code, many squares.
  4. Now add random numbers for size, place and colour. Every run makes a new picture. Simple rules plus chance is called generative art.
  5. Recursion: a function that calls a smaller copy of itself. Each branch grows two smaller branches. Depth 6 gives 63 branches: a fractal tree.
  6. Your turn. Each slider is an event: when you move it, the program redraws. Press the dice for a new random seed and a new mode.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why is y upside down compared with maths?

Screens draw from the top line downwards, so programs count rows from the top. Step 1 shows y growing downwards.

How can three numbers make every colour?

Screens mix red, green and blue light. Different strengths of the three give millions of colours.

Why use a loop instead of writing 100 circle() lines?

The loop is shorter, has fewer mistakes, and you change one number to get 10 or 1,000 shapes.

If the computer chooses randomly, is the art still mine?

Yes. You design the rules, the range of randomness and the colours. The computer only explores your design.

Why doesn't the recursive tree grow forever?

The depth goes down by 1 each call. When it reaches 0 (the base case) the function stops.

How does the sketch know I moved a slider?

The slider fires an input event, and a function connected to it redraws the picture.

What is creative coding?

Creative coding uses programming to make art: images, animation, sound, games or interactive installations. The aim is expression, not a business app.

Popular tools: p5.js (JavaScript in the browser), Processing (Java), Python with turtle or pygame, and block tools like Scratch.

Most sketches have two parts: setup() runs once (make the canvas) and draw() runs again and again, about 60 times a second, which makes animation.

Generative art: the artist writes rules, and the computer makes the picture. Change one number and you get a new work.

Canvas, coordinates, colours and shapes

The canvas is a grid of pixels. createCanvas(400, 300) makes it 400 wide and 300 tall. The origin (0, 0) is the top-left; y grows downwards, unlike maths graphs.

Variables store numbers that can change: x = x + 2 in draw() moves a ball. A test (if (x > width) x = 0) wraps it back.

Loops, randomness and patterns

A for loop repeats with a counter: for (i = 0; i < 10; i++) circle(i*40, 100, 30) draws a row of 10. A loop inside a loop (nested) fills a grid: n × n shapes.

random(a, b) gives a number between a and b. Use it for position, size or colour. A seed fixes the random sequence so you can make the same picture again.

Noise (Perlin noise) is smooth randomness, good for clouds, hills and natural motion.

Patterns come from repeating with small changes: rotate a little each time, change colour by i, or skip every second shape.

Functions, recursion and fractals

A function packs instructions under a name, with parameters: flower(x, y, size) can be drawn many times.

Iteration repeats with a loop. Recursion repeats by a function calling itself on a smaller problem. It needs a base case to stop, or it never ends.

function branch(len, depth) {
  line(0, 0, 0, -len); translate(0, -len)
  if (depth > 0) {
    rotate(a);  branch(len*0.7, depth-1)
    rotate(-2a); branch(len*0.7, depth-1)
  }
}

A fractal looks similar at every scale: trees, ferns, the Koch snowflake, the Sierpiński triangle. A binary tree of depth d has 2d − 1 branches.

Events, objects, data and installations

Events

An event is something that happens: mousePressed(), keyPressed(), a slider moving, a sound getting loud. The program reacts with a function. This makes art interactive.

Arrays and objects

An array stores many values: 100 x-positions for 100 raindrops. A class is a blueprint (Drop with x, y, speed and a fall() method); each drop is an object. This keeps big sketches tidy.

Images and video as data

An image is an array of pixels, each with RGB values. Change every pixel to make filters: grey = (r + g + b) / 3; invert = 255 − value. Webcam video is a new image 30 times a second.

Data visualisation

Turn numbers into shapes: rainfall per month as bar heights, or city temperatures as colours.

Sensors, APIs, GUIs and installations

A microcontroller (Arduino, micro:bit) can send light or distance readings to a sketch. An API brings live data from the internet, such as weather. A GUI gives sliders and buttons. Put it all in a room with a projector and you have an interactive installation.

Try it

Open a p5.js web editor and draw a 10 × 10 grid of circles whose colour depends on mouseX.

Key formulas and definitions

Worked examples

1. On a 400 × 300 canvas, where is the point (400, 300)?

The bottom-right corner, because x grows right and y grows down from the top-left (0, 0).

2. Write the colour for pure yellow in RGB.

Yellow is red + green light: fill(255, 255, 0).

3. How many circles does this draw? for (i = 0; i < 8; i++) for (j = 0; j < 5; j++) circle(i*50, j*50, 40)

The outer loop runs 8 times and the inner 5 times each: 8 × 5 = 40 circles.

4. A recursive tree starts with one trunk and each branch splits in two, down to depth 5. How many branches?

1 + 2 + 4 + 8 + 16 = 31 = 2⁵ − 1.

5. A pixel is (200, 100, 30). Find its greyscale value and its inverted colour.

Grey = (200 + 100 + 30) / 3 = 110. Invert = (55, 155, 225).

6. Plan a raindrop sketch with objects.

Class Drop { x, y, speed; fall() { y += speed; if (y > height) y = 0 } show() { line(x, y, x, y+10) } }. Make an array of 200 drops; in draw(), call fall() and show() for each.

Common mistakes

Practice quiz

1. Where is (0, 0) on a p5.js or Processing canvas?
2. fill(0, 0, 255) gives:
3. Which makes every run of a sketch look different?
4. Recursion means:
5. mousePressed() is an example of:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is creative coding?

Creative coding is programming to make art, animation, sound or interactive experiences, using tools like p5.js, Processing or Scratch.

Which language is best for creative coding beginners?

p5.js (JavaScript) runs in a browser with nothing to install. Processing and Python turtle are also good starters.

What is generative art?

Art made by a set of rules that a computer follows, often with randomness, so each run produces a new image.

Where this is taught

Ukraine10 класElective: creative programming (35 h)
Ukraine11 класElective: creative programming (35 h)
FrancePremièreDigital tools and languages

Learn first

Learn next

Related lessons

All Computer Science lessons