|
| 1 | +const MUSIC_URL = |
| 2 | + 'https://shotstack-assets.s3.amazonaws.com/music/unminus/ambition.mp3'; |
| 3 | +const SECONDS_PER_PHOTO = 3.2; |
| 4 | +const MIN_LENGTH = 6; |
| 5 | +const EFFECTS = ['zoomIn', 'slideUp', 'zoomOut', 'slideDown']; |
| 6 | + |
| 7 | +const escapeHtml = value => |
| 8 | + String(value).replace( |
| 9 | + /[&<>"']/g, |
| 10 | + character => |
| 11 | + ({ |
| 12 | + '&': '&', |
| 13 | + '<': '<', |
| 14 | + '>': '>', |
| 15 | + '"': '"', |
| 16 | + "'": ''' |
| 17 | + })[character] |
| 18 | + ); |
| 19 | + |
| 20 | +// The specification card is an html5 asset. The vehicle data is written into |
| 21 | +// the markup here, so the card can change shape per dealer without a template. |
| 22 | +function specCard(vehicle, length) { |
| 23 | + const title = escapeHtml(`${vehicle.year} ${vehicle.make} ${vehicle.model}`); |
| 24 | + const chips = [vehicle.odometer, vehicle.transmission, vehicle.fuel] |
| 25 | + .map( |
| 26 | + (text, index) => |
| 27 | + `<span class="chip" id="chip${index}">${escapeHtml(text)}</span>` |
| 28 | + ) |
| 29 | + .join(''); |
| 30 | + |
| 31 | + return { |
| 32 | + asset: { |
| 33 | + type: 'html5', |
| 34 | + html: `<div class="card" id="card"><div class="title">${title}</div><div class="trim">${escapeHtml(vehicle.trim)}</div><div class="price">${escapeHtml(vehicle.price)}</div><div class="row">${chips}</div></div>`, |
| 35 | + css: 'html,body{margin:0;padding:0;width:960px;height:420px;overflow:hidden;background:transparent;font-family:system-ui,sans-serif}.card{box-sizing:border-box;width:960px;height:420px;padding:40px 48px;background:rgba(10,15,30,0.86);border-radius:32px;color:#fff;opacity:0;transform:translateY(24px)}.title{font-size:60px;font-weight:800;letter-spacing:-1px;line-height:1.05}.trim{margin-top:10px;font-size:34px;color:#cbd5e1}.price{margin-top:22px;font-size:72px;font-weight:800;color:#facc15;line-height:1}.row{display:flex;gap:14px;margin-top:26px}.chip{font-size:28px;padding:10px 20px;border-radius:999px;background:rgba(255,255,255,0.14);color:#e2e8f0;white-space:nowrap;opacity:0;transform:translateY(12px)}', |
| 36 | + js: "const tl=gsap.timeline();tl.to('#card',{opacity:1,y:0,duration:0.6,ease:'power3.out'},0).to(['#chip0','#chip1','#chip2'],{opacity:1,y:0,duration:0.6,ease:'power3.out',stagger:0.13},0.3);" |
| 37 | + }, |
| 38 | + start: 0.5, |
| 39 | + length: length - 0.5, |
| 40 | + width: 960, |
| 41 | + height: 420, |
| 42 | + position: 'bottom', |
| 43 | + offset: { y: 0.05 } |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function dealerBadge(vehicle, length) { |
| 48 | + return { |
| 49 | + asset: { |
| 50 | + type: 'html5', |
| 51 | + html: `<div class="badge">${escapeHtml(vehicle.dealer)}</div>`, |
| 52 | + css: 'html,body{margin:0;padding:0;width:960px;height:110px;overflow:hidden;background:transparent;font-family:system-ui,sans-serif}.badge{box-sizing:border-box;display:inline-block;height:110px;line-height:110px;padding:0 44px;border-radius:55px;background:rgba(10,15,30,0.7);color:#fff;font-size:42px;font-weight:700}' |
| 53 | + }, |
| 54 | + start: 0, |
| 55 | + length, |
| 56 | + width: 960, |
| 57 | + height: 110, |
| 58 | + position: 'top', |
| 59 | + offset: { y: -0.04 } |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +// One clip per photo, alternating between two tracks so each photo |
| 64 | +// dissolves into the next instead of dipping to black. An odd photo sits on |
| 65 | +// the upper track: it fades in over the photo below, then fades out to reveal |
| 66 | +// the next photo, which already plays underneath. OVERLAP matches the length |
| 67 | +// of the fade transition. |
| 68 | +const OVERLAP = 1; |
| 69 | + |
| 70 | +function photoClips(photos, secondsPerPhoto) { |
| 71 | + const upper = []; |
| 72 | + const lower = []; |
| 73 | + |
| 74 | + photos.forEach((src, index) => { |
| 75 | + const isLast = index === photos.length - 1; |
| 76 | + const clip = { |
| 77 | + asset: { type: 'image', src }, |
| 78 | + start: index * secondsPerPhoto, |
| 79 | + length: secondsPerPhoto + (isLast ? 0 : OVERLAP), |
| 80 | + fit: 'crop', |
| 81 | + effect: EFFECTS[index % EFFECTS.length] |
| 82 | + }; |
| 83 | + |
| 84 | + if (index % 2 === 1) { |
| 85 | + clip.transition = isLast ? { in: 'fade' } : { in: 'fade', out: 'fade' }; |
| 86 | + upper.push(clip); |
| 87 | + } else { |
| 88 | + if (index === 0) clip.transition = { in: 'fade' }; |
| 89 | + lower.push(clip); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + return { upper, lower }; |
| 94 | +} |
| 95 | + |
| 96 | +export function buildEdit(vehicle) { |
| 97 | + // A vehicle with one photo still gets a video long enough to read the card. |
| 98 | + const secondsPerPhoto = Math.max( |
| 99 | + SECONDS_PER_PHOTO, |
| 100 | + MIN_LENGTH / vehicle.photos.length |
| 101 | + ); |
| 102 | + const length = secondsPerPhoto * vehicle.photos.length; |
| 103 | + const photos = photoClips(vehicle.photos, secondsPerPhoto); |
| 104 | + |
| 105 | + return { |
| 106 | + timeline: { |
| 107 | + background: '#000000', |
| 108 | + tracks: [ |
| 109 | + { clips: [specCard(vehicle, length)] }, |
| 110 | + { clips: [dealerBadge(vehicle, length)] }, |
| 111 | + { clips: photos.upper }, |
| 112 | + { clips: photos.lower }, |
| 113 | + { |
| 114 | + clips: [ |
| 115 | + { |
| 116 | + asset: { |
| 117 | + type: 'audio', |
| 118 | + src: MUSIC_URL, |
| 119 | + volume: 0.3, |
| 120 | + effect: 'fadeOut' |
| 121 | + }, |
| 122 | + start: 0, |
| 123 | + length: 'end' |
| 124 | + } |
| 125 | + ] |
| 126 | + } |
| 127 | + ] |
| 128 | + }, |
| 129 | + output: { format: 'mp4', size: { width: 1080, height: 1920 } } |
| 130 | + }; |
| 131 | +} |
0 commit comments