Skip to content
animorio
IT
Back to the library

Night train background

Backgrounds, HoverCanvas, GSAPMediumCompressed weight: Demo 10 KBGSAP 28 KBFreeDownload the zip

An animated background for the hero of a night train booking app: behind the heading the city seen from the window of a moving train, completely out of focus. Every light is a bokeh disc like through a real lens, with a brighter rim, faint rings inside, a warm fringe outside and a cool one inside, and toward the corners it squeezes into a cat's eye. Near lights are big and pass quickly, far ones are small and slow, the carriage sways a hair and now and then a catenary pole goes by. The pointer slowly moves the focus and the depth; booking the cabin the train brakes and the city comes into focus for a moment.

The code

A folder that runs on its own: index.html, style.css, script.js and the libraries from a CDN. The React and Vue tabs hold the same demo as a ready component, which switches its animations off when it leaves the page.

<!doctype html>
<!--
  "Night train background" from Animorio · https://animorio.com
  License: free on your own sites and your clients’ sites, paid work included.
  Not for resale, redistribution, templates or code collections · https://animorio.com/en/license/
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Couchette</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <!-- the hero of a night train booking app. Behind the heading a canvas draws the city at night seen through the
       window of a moving train, completely out of focus: every light is a bokeh disc with a bright rim, a faint
       onion-ring texture and a coloured fringe, squeezed into a cat's eye near the edges like through a real lens.
       Near lights pass faster than far ones, the carriage sways a hair, a catenary pole crosses now and then.
       The pointer moves the focus and the depth; booking a cabin slows the train and pulls the city into focus -->
  <section class="stage" data-stage>
    <canvas class="city" aria-hidden="true" data-canvas></canvas>
    <div class="glass" aria-hidden="true" data-glass></div>
    <div class="grain" aria-hidden="true" data-grain></div>

    <header class="bar" data-in>
      <span class="brand">
        <svg class="brand__mark" viewBox="0 0 24 24" aria-hidden="true">
          <path d="M15.6 4.2a8.2 8.2 0 1 0 4.2 11.4 6.6 6.6 0 0 1-4.2-11.4z"/>
          <path d="M4.5 20.4h15"/>
        </svg>
        Couchette
      </span>
      <a class="bar__link" href="#">Sign in</a>
    </header>

    <div class="hero">
      <a class="ticket" href="#" data-in>
        <span class="ticket__when">Tonight</span>
        <span class="ticket__leg">Paris <b>19:52</b></span>
        <svg class="ticket__arrow" viewBox="0 0 22 10" aria-hidden="true"><path d="M1 5h19M16.5 1.6L20 5l-3.5 3.4"/></svg>
        <span class="ticket__leg">Roma <b>09:30</b></span>
      </a>
      <h1 class="title" data-title><span class="title__line">Fall asleep in Paris.</span> <span class="title__line">Wake up in Rome.</span></h1>
      <p class="lede" data-in>Private cabins on 140 night routes across Europe. Fresh sheets, a real bed, breakfast at the window.</p>
      <div class="actions" data-in>
        <button class="btn" type="button" data-btn>
          <span class="btn__face">
            <span class="btn__words" data-words>Book a cabin</span>
            <svg class="btn__icon" viewBox="0 0 16 16" aria-hidden="true">
              <path class="btn__arrow" data-arrow d="M3 8h9M8.5 4.2L12.3 8 8.5 11.8"/>
              <path class="btn__check" data-check pathLength="1" d="M3.4 8.4l3 3 6.2-6.6"/>
            </svg>
          </span>
        </button>
        <a class="ghost" href="#" data-ghost><span class="ghost__shine" aria-hidden="true"></span><span class="ghost__words">See all routes</span></a>
      </div>
    </div>

    <p class="hint" data-in>Move up and down to pull the focus</p>
    <p class="sr" aria-live="polite" data-live></p>
  </section>

  <script src="https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

Playground

Change values, colours and text with the controls and watch the animation respond live. Or write in the code yourself. Then download the folder with your changes.

32 values to change

Or ask your AI assistant for it

With the Animorio connector, Claude Code, ChatGPT, Cursor or VS Code take it from here, guide included, and fit it into your project. Paste this request:

Take “Night train background” from Animorio and fit it into my site
Platform licenseFree on your sites and your clients’ sites. Want it inside a website builder, a template platform or a client dashboard? There’s a license for agencies and platforms.

What it does

On an almost black night blue the hero of Couchette appears, an app that books cabins on Europe's night trains: at the top the moon mark and ‘Sign in’, in the middle a strip of frosted glass with tonight's train (‘Tonight’, Paris 19:52, an arrow, Roma 09:30), the heading ‘Fall asleep in Paris. Wake up in Rome.’, a line that explains and the ‘Book a cabin’ and ‘See all routes’ buttons. Behind, the city seen from the window slides from right to left completely out of focus: amber street lamps, warm white windows, a few blue LEDs and a few red signals, each one a bokeh disc with a brighter rim, the faint rings of the lens and a colour fringe. Near lights are big, soft and pass quickly, far ones small, slow and twinkling a little; toward the corners the discs squeeze into a cat's eye, the carriage sways by a pixel, now and then a catenary pole passes like a dark shadow. The focus breathes slowly; then the button is pressed: it gives and comes back on a spring, the label turns into ‘Cabin 4B is yours’ with a checkmark that draws itself, the train brakes, the city comes into focus (the far lights become points with a small glow, the near ones shrink), the heading gains a little weight and the amber glow under the button lights up. Then everything melts again, the train picks up and the loop starts over. With the pointer high up the far city comes into focus, low down everything goes soft again, sideways the lights shift in layers; on the glass button a warm highlight follows the pointer.

How it works

One bokeh disc per colour

At start each colour becomes a disc drawn pixel by pixel on a small canvas: a body slightly brighter toward the edge, a bright ring on the rim, the faint ‘onion’ rings of the lens and a light halo outside. Red sees a slightly wider disc and blue a slightly narrower one: hence the warm fringe outside and the cool one inside.

Depth and focus

Each light has a depth z from 0 (the far city) to 1 (the lamps by the track). The disc's radius grows with the distance from the focus plane, and the bigger the disc the fainter it is. The focus plane turns slowly toward its goal, like a real focus ring.

The running train

The lights sit on a strip wider than the page that repeats: each slides at a speed that grows with z, so near ones pass quickly and far ones almost stand still. The discs add up with globalCompositeOperation lighter, like real light.

The cat's eye and the details

Toward the corners each disc is squeezed along the line to the middle, with a single setTransform per light. The carriage sways on two slow sines, far lights twinkle, a catenary pole passes every four to seven seconds as a dark band, the grain jumps 24 times a second.

The pointer and the button

The pointer high up brings the focus to the far city, sideways it shifts the layers in parallax, always with a slow chase. Pressing ‘Book a cabin’ a GSAP timeline brakes the train, brings the city into focus, lights the glow under the button and then melts it all again.

Values to change

NameFileDefaultWhat it changes
BLURscript.js1How out of focus the city is at rest: 0.4 almost sharp, 1.6 only big soft discs.
LIGHTSscript.js74How many lights pass behind the window.
SPEEDscript.js1How fast the train runs: 0 stopped in a station, 2 twice as fast.
CAT_EYEscript.js0.32How much the discs squeeze toward the corners, like through a real lens (0 all round).
BRIGHTNESSscript.js1How bright the lights are.
POLESscript.jstrueA catenary pole passes now and then.
GRAINscript.js0.07How strong the grain is.
LOOPscript.jstrueRepeats the booking with the focus pull on its own.

Try them in the Playground

When to use it

As the background of the first screen of a travel site, night trains, a city hotel, a festival or an evening venue, a late-night music app: wherever a soft night mood is needed behind a clear heading. The light colours are --sodium, --warm, --ice and --signal in the CSS.

On phones

The same animations at adapted sizes: the discs are measured on the shorter side, on tall screens the city rises higher up the window, the heading is measured on width and height. With a finger, dragging up and down moves the focus.

With reduced motion

The city stays still, out of focus, and fades in; no sway, no poles. Pressing the button the glow lights up softly and the label and checkmark change with a fade.

Pitfalls

  • With identical discs that only get bigger the blur looks like bubbles: the brighter rim, the faint rings and the colour fringe are what make them look like light through a lens.
  • If big discs stay as bright as small ones the page burns out: a wider disc must be fainter, like the same light spread over more space.
  • A focus plane that jumps to the pointer looks like a glitch: it chases slowly, like a ring turned by hand, and goes quickly only in the booking.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0.

Compressed with gzip, the three demo files weigh 10 KB; the libraries: GSAP 28 KB.

Testing

Tested on 2 November 2026

  • Desktop: Done
  • Phone: Done
  • Reduced motion: Done
  • Safari on iPhone: To do

License

Use it in your own projects and in your clients’ projects. Do not resell it or redistribute it as is. GSAP stays under its own Standard license. Full license.

All animations