Film source

The Canvas drawing code for the night drive. The animation follows the song's playback time.

music-video.mjs

// Deterministic Canvas 2D music film. Every shape and texture is drawn in code.
// Time comes from the song, so pausing, replaying and offline export agree.
const W=320,H=180;
const fract=x=>x-Math.floor(x);
const noise=(a,b=0)=>fract(Math.sin(a*127.1+b*311.7)*43758.5453);
const mix=(a,b,t)=>a+(b-a)*t;
const clamp=x=>Math.max(0,Math.min(1,x));
const smooth=x=>{x=clamp(x);return x*x*(3-2*x);};
const rgb=(a,b,t)=>`rgb(${a.map((v,i)=>Math.round(mix(v,b[i],t))).join(',')})`;
export function createMusicVideo(canvas) {
 const screen=document.createElement('canvas');screen.width=W;screen.height=H;
 const p=screen.getContext('2d'),out=canvas.getContext('2d');
 const rect=(x,y,w,h,c)=>{p.fillStyle=c;p.fillRect(Math.round(x),Math.round(y),Math.round(w),Math.round(h));};
 const poly=(points,c)=>{p.fillStyle=c;p.beginPath();points.forEach(([x,y],i)=>i?p.lineTo(Math.round(x),Math.round(y)):p.moveTo(Math.round(x),Math.round(y)));p.closePath();p.fill();};
 const ellipse=(x,y,rx,ry,c)=>{p.fillStyle=c;p.beginPath();p.ellipse(Math.round(x),Math.round(y),rx,ry,0,0,Math.PI*2);p.fill();};
 function sky(t,dawn,horizon=105) {
  const top=[mix(10,72,dawn),mix(17,87,dawn),mix(38,113,dawn)];
  const bottom=[mix(55,244,dawn),mix(58,161,dawn),mix(91,116,dawn)];
  for(let y=0;y<horizon;y++)rect(0,y,W,1,rgb(top,bottom,(y/horizon)**1.4));
  for(let i=0;i<105;i++) {
   if(noise(i,1)<dawn*1.8)continue;
   const x=noise(i,3)*W,y=10+noise(i,5)*(horizon-26);
   rect(x,y,1,1,noise(i,9)>.8?'#c1c6d0':'#717f9d');
   if(i%21===0&&dawn<.3){rect(x-1,y,3,1,'#9daac0');rect(x,y-1,1,3,'#9daac0');}
  }
  if(dawn<.7){ellipse(258,32,10,10,rgb([205,215,201],[229,193,155],dawn));ellipse(262,29,9,9,rgb(top,bottom,.27));}
  if(dawn>.25){const y=108-(dawn-.25)*61;ellipse(237,y,17,17,'#ffd09a');for(let i=0;i<5;i++)rect(218,y+3+i*4,39,1,rgb(top,bottom,.85));}
  for(let i=0;i<12;i++){
   const x=((noise(i,12)*380-t*(.2+i%3*.07))%380+380)%380-30,y=19+noise(i,13)*54;
   const c=rgb([37,43,69],[184,133,137],dawn);
   rect(x,y,19+noise(i,14)*35,2,c);rect(x+6,y-2,20+noise(i,15)*13,2,c);
  }
 }
 function mountains(t,dawn) {
  for(let layer=0;layer<3;layer++){
   const points=[[0,122]];const speed=[.3,.9,1.8][layer];
   for(let x=-4;x<=324;x+=4){const u=x+t*speed;const y=87+layer*10+Math.sin(u*.027+layer*2)*11+Math.sin(u*.072+layer)*4+Math.sin(u*.13)*1.8;points.push([x,y]);}
   points.push([W,130]);poly(points,rgb([[31,36,61],[24,35,52],[19,33,42]][layer],[[138,118,135],[104,113,123],[68,96,105]][layer],dawn));
  }
 }
 function pine(x,y,size,dawn) {
  rect(x-1,y-size*.8,2,size*.8,rgb([21,30,34],[61,71,65],dawn));
  for(let j=0;j<4;j++){const yy=y-size+j*size*.18,half=size*(.13+j*.05);poly([[x,yy],[x-half,yy+size*.42],[x+half,yy+size*.42]],rgb([12+j*2,26+j*2,30+j*2],[42+j*4,70+j*4,66+j*3],dawn));}
 }
 function wheel(x,y,t) {
  ellipse(x,y,7,7,'#090f1a');ellipse(x,y,4,4,'#57626c');ellipse(x,y,2,2,'#1b2834');
  const phase=t*18;
  for(let j=0;j<3;j++)rect(x+Math.cos(phase+j*2.09)*3,y+Math.sin(phase+j*2.09)*3,1,1,'#a9b3ba');
 }
 function car(x,y,t,dawn,scale=1) {
  p.save();p.translate(Math.round(x),Math.round(y));p.scale(scale,scale);
  const bounce=Math.sin(t*8)*.45+Math.exp(-((t-.12)% .48)*22)*.3;
  p.translate(0,Math.round(bounce));
  ellipse(0,10,52,6,'#101724');
  // Headlights spread over the road, tapering away from the front bumper.
  p.globalAlpha=1-dawn*.88;
  poly([[46,2],[109,14],[105,19],[46,7]],rgb([95,91,67],[153,134,95],dawn));
  poly([[46,3],[87,11],[87,13],[46,6]],'#c5b990');
  p.globalAlpha=1;
  poly([[-49,-2],[-43,-12],[-25,-15],[-13,-29],[20,-29],[33,-14],[47,-11],[53,-3],[51,7],[-48,7]],'#182333');
  poly([[-46,-3],[-40,-10],[-22,-13],[-11,-27],[18,-27],[31,-12],[45,-9],[49,-2],[48,5],[-46,5]],rgb([132,58,57],[174,80,58],dawn));
  poly([[-20,-13],[-10,-25],[1,-25],[1,-13]],'#293d50');
  poly([[4,-25],[17,-25],[28,-13],[4,-13]],'#324b60');
  poly([[-17,-14],[-9,-23],[-3,-23],[-10,-14]],rgb([68,81,98],[149,155,159],dawn));
  poly([[7,-24],[16,-24],[24,-16],[7,-16]],'#4b5e6a');
  // A driver silhouette, steering wheel and one warm dashboard reflection.
  ellipse(15,-20,3,4,'#172634');poly([[13,-17],[21,-13],[10,-13]],'#192b36');
  rect(23,-15,3,2,'#ba9870');
  rect(-44,-7,85,2,rgb([174,79,65],[228,135,81],dawn));
  rect(-43,2,87,2,'#593b3f');rect(-48,5,96,2,'#253341');
  rect(2,-12,1,15,'#6b3d3b');rect(-17,-11,1,13,'#6b3d3b');
  rect(-12,-8,5,1,'#b6a797');rect(9,-8,5,1,'#b6a797');
  rect(-47,-5,4,4,'#e87968');rect(45,-6,5,4,'#ffe5ac');
  rect(-50,1,4,2,'#b5b9af');rect(49,1,4,2,'#b5b9af');
  wheel(-29,7,t);wheel(31,7,t);
  // Roof rack and suitcase: a small continuing detail across the journey.
  rect(-13,-31,31,1,'#b5a990');rect(-9,-35,22,4,'#8c785c');rect(-6,-35,2,4,'#c5b38c');rect(8,-35,2,4,'#c5b38c');
  p.restore();
 }
 function roadSide(t,dawn,y=142) {
  rect(0,y-11,W,10,rgb([41,53,53],[96,112,86],dawn));
  rect(0,y-1,W,2,'#b5a893');rect(0,y+1,W,25,rgb([34,39,53],[68,69,76],dawn));
  rect(0,y+24,W,2,'#a8a69b');
  for(let j=-1;j<12;j++){const x=j*40-((t*80)%40);rect(x,y+12,23,1,'#c4b691');}
  for(let i=0;i<95;i++){const x=((noise(i,80)*340-t*90)%340+340)%340-10;rect(x,y+2+noise(i,81)*20,2,1,rgb([44,49,61],[78,79,84],dawn));}
  rect(0,y+26,W,30,rgb([21,36,36],[60,85,64],dawn));
  for(let j=-1;j<35;j++){const x=j*11-(t*120%11),h=2+noise(j,19)*6;rect(x,y+27,1,h,'#435549');}
 }
 function coast(t,dawn) {
  sky(t,dawn);mountains(t,dawn);
  rect(0,109,W,25,rgb([35,56,75],[119,153,153],dawn));
  for(let j=0;j<70;j++){
   const x=(noise(j,41)*W+t*(j%2?.9:-.6))%W,y=111+noise(j,42)*22;
   rect(x,y,2+noise(j,43)*13,1,rgb([62,81,96],[201,177,147],dawn));
  }
  if(dawn>.6)for(let j=0;j<10;j++)rect(228-j*2+Math.sin(t+j)*2,111+j*2,15+j*4,1,'#d8b58e');
  roadSide(t,dawn,144);
  car(147+Math.sin(t*.35)*3,140,t,dawn);
  for(let j=0;j<6;j++){
   const x=((j*83-t*39)%498+498)%498-70;
   rect(x,126,2,13,'#89938b');rect(x-4,128,13,2,'#6b827c');
  }
 }
 function forest(t,dawn) {
  sky(t,dawn);mountains(t,dawn);
  rect(0,116,W,30,rgb([23,42,42],[71,96,73],dawn));
  for(let j=-1;j<15;j++)pine(j*29-(t*8%29),135,28+noise(j,21)*21,dawn);
  roadSide(t,dawn,143);car(153,139,t,dawn);
  for(let j=-1;j<5;j++){
   const x=j*110-(t*37%110);pine(x,184,70+noise(j,27)*21,dawn);
  }
 }
 function town(t,dawn) {
  sky(t,dawn);mountains(t,dawn);
  const offset=t*23;
  for(let j=-1;j<10;j++){
   const index=j+Math.floor(offset/48),x=j*48-offset%48;
   const h=34+noise(index,61)*30,y=127-h;
   const colors=['#394351','#494652','#3a4c54','#53464e'];
   rect(x,y,45,h,colors[((index%4)+4)%4]);rect(x-1,y,47,3,'#242f3f');
   rect(x+2,y+4,41,1,'#697078');
   for(let row=0;row<3;row++)for(let col=0;col<4;col++){
    const lit=noise(index*19+row*4+col,62)>.43;
    rect(x+5+col*10,y+9+row*13,6,8,lit?'#d5a56e':'#223442');
    if(lit){rect(x+5+col*10,y+9+row*13,6,1,'#efd09a');rect(x+8+col*10,y+9+row*13,1,8,'#8d7b67');}
   }
   rect(x+4,114,37,13,'#26313d');rect(x+7,117,11,10,'#876954');rect(x+20,116,17,8,'#788579');
   if(index%3===0){rect(x+2,109,40,4,'#bc775d');rect(x+8,110,4,1,'#f6ca8f');rect(x+15,110,15,1,'#f6ca8f');}
   for(let brick=0;brick<9;brick++)rect(x+noise(brick,index)*40,y+6+noise(brick,3+index)*h,4,1,'#64606a');
  }
  rect(0,127,W,6,'#6c7073');rect(0,132,W,2,'#b3aba0');
  roadSide(t,dawn,145);
  // Broken reflections run through the wet asphalt below shop windows.
  for(let i=0;i<30;i++){const x=((noise(i,72)*400-t*23)%400+400)%400;rect(x,148+noise(i,73)*18,1+noise(i,75)*8,1,'#827563');}
  car(151,142,t,dawn);
  for(let j=-1;j<4;j++){
   const x=j*120-(t*32%120);rect(x,83,2,53,'#223241');rect(x-12,82,14,2,'#53616a');rect(x-13,84,6,2,'#f9d69a');
   poly([[x-14,87],[x-32,132],[x+10,132],[x-8,87]],'rgba(240,191,115,.045)');
  }
 }
 function windshield(t,dawn) {
  sky(t,dawn,102);mountains(t,dawn);
  const horizon=100,bend=Math.sin(t*.14)*20;
  rect(0,horizon,W,80,'#1d3036');
  for(let y=horizon;y<180;y++){
   const z=(y-horizon)/80,half=3+z*z*171,center=160+bend*(1-z)**2;
   rect(center-half-3,y,half*2+6,1,'#a8a18c');rect(center-half,y,half*2,1,'#303b49');
   const distance=1/(z+.055);
   if(Math.floor(distance+t*5)%3!==0)rect(center-1-z*2,y,2+z*4,1,'#c7b080');
   if(z>.2&&noise(y,Math.floor(t*8))>.55)rect(center-half*.8,y,half*1.6,1,'rgba(205,196,149,.035)');
  }
  for(let j=0;j<18;j++){
   const z=fract(j/18+t*.13),d=z*z;
   for(const side of [-1,1])pine(160+side*(10+d*199)+bend*(1-z)**2,100+d*78,4+d*62,dawn);
  }
  // Windshield frame, instrument panel and steering wheel in the foreground.
  poly([[0,0],[10,0],[40,155],[24,162]],'#111b28');poly([[310,0],[320,0],[320,167],[291,154]],'#111b28');
  poly([[0,163],[76,152],[252,153],[320,163],[320,180],[0,180]],'#172332');
  rect(0,162,W,2,'#47505b');
  ellipse(82,180,37,26,'#0a121e');ellipse(82,180,30,20,'#3c4550');ellipse(82,180,25,16,'#162434');
  rect(136,161,39,13,'#0b1929');rect(141,164,28,4,'#526958');
  for(let i=0;i<9;i++)rect(142+i*3,165,1,2,'#a2c39a');
  rect(188,162,49,10,'#0d1b28');for(let i=0;i<7;i++)rect(191+i*6,166,4,1,'#61737a');
  rect(136,9,51,12,'#121e2b');rect(139,11,45,7,'#415265');rect(141,12,40,1,'#6b6f7d');
 }
 return function render(time,lyrics=[]) {
  const t=Math.max(0,Math.min(95.36,time));
  const dawn=smooth((t-45)/47);
  if(t<15.48)forest(t,0);
  else if(t<30.84)coast(t,dawn);
  else if(t<46.2)windshield(t,dawn);
  else if(t<76.92)town(t,dawn*.85);
  else coast(t,dawn);
  // Foreground grain is fixed to the artwork, not random frame flicker.
  for(let i=0;i<180;i++)rect(noise(i,91)*W,noise(i,92)*H,1,1,'rgba(210,185,152,.025)');
  // Brief exposure dips make musical section cuts intentional.
  const cuts=[15.48,30.84,46.2,76.92];
  let dip=0;for(const cut of cuts)dip=Math.max(dip,clamp(1-Math.abs(t-cut)/.14)*.40);
  if(dip)rect(0,0,W,H,`rgba(8,14,25,${dip})`);
  const fade=smooth(t/.7)*(1-smooth((t-93.8)/1.5));
  out.imageSmoothingEnabled=false;out.fillStyle='#090e18';out.fillRect(0,0,canvas.width,canvas.height);
  out.globalAlpha=fade;out.drawImage(screen,0,0,canvas.width,canvas.height);out.globalAlpha=1;
  const sx=canvas.width/960,sy=canvas.height/540;
  out.save();out.scale(sx,sy);
  out.fillStyle='rgba(8,14,25,.86)';out.fillRect(0,0,960,23);out.fillRect(0,507,960,33);
  const line=lyrics.find(l=>t>=l.start&&t<l.end)?.text||'';
  out.textAlign='center';out.font='18px monospace';out.fillStyle='#eee2c8';
  if(line)out.fillText(line,480,530);
  if(t<9){
    out.globalAlpha=smooth((t-.4)/1)*(1-smooth((t-7)/2));
    out.textAlign='left';out.font='bold 29px monospace';out.fillStyle='#eed8ae';out.fillText('THE LONG WAY HOME',45,91);
    out.font='12px monospace';out.fillStyle='#b3b6b7';out.fillText('A NIGHT DRIVE / 125 BPM',47,113);out.globalAlpha=1;
  }
  if(t>92.5){out.globalAlpha=smooth((t-92.5)/.8);out.font='18px monospace';out.fillStyle='#e0c79b';out.fillText('THE LONG WAY HOME',480,285);out.globalAlpha=1;}
  out.restore();
 };
}