|
| 1 | +import { |
| 2 | + balloonDefaultSize, |
| 3 | + createBallonElement, |
| 4 | + svgFiltersHtml, |
| 5 | +} from "./balloonSvg"; |
| 6 | +export { textBalloons } from "./textBalloons"; |
| 7 | + |
| 8 | +const easings = [ |
| 9 | + // easeOutQuint |
| 10 | + "cubic-bezier(0.22, 1, 0.36, 1)", |
| 11 | + // easeOutCubic |
| 12 | + "cubic-bezier(0.33, 1, 0.68, 1)", |
| 13 | +]; |
| 14 | +const colorPairs = [ |
| 15 | + // yellow |
| 16 | + ["#ffec37ee", "#f8b13dff"], |
| 17 | + // red |
| 18 | + ["#f89640ee", "#c03940ff"], |
| 19 | + //blue |
| 20 | + ["#3bc0f0ee", "#0075bcff"], |
| 21 | + // green |
| 22 | + ["#b0cb47ee", "#3d954bff"], |
| 23 | + // purple |
| 24 | + ["#cf85b8ee", "#a3509dff"], |
| 25 | +]; |
| 26 | + |
| 27 | +function createBalloonAnimation({ |
| 28 | + balloon, |
| 29 | + x, |
| 30 | + y, |
| 31 | + z, |
| 32 | + targetX, |
| 33 | + targetY, |
| 34 | + targetZ, |
| 35 | + zIndex, |
| 36 | +}: { |
| 37 | + balloon: HTMLElement; |
| 38 | + x: number; |
| 39 | + y: number; |
| 40 | + z: number; |
| 41 | + targetX: number; |
| 42 | + targetY: number; |
| 43 | + targetZ: number; |
| 44 | + zIndex: number; |
| 45 | +}) { |
| 46 | + balloon.style.zIndex = zIndex.toString(); |
| 47 | + // Add blur to the closes ballons for bokeh effect |
| 48 | + balloon.style.filter = `blur(${zIndex > 7 ? 8 : 0}px)`; |
| 49 | + const getAnimation = () => { |
| 50 | + const tiltAngle = Math.random() * (15 - 8) + 8; // Random tilt angle between 8 and 15 degrees |
| 51 | + const tiltDirection = Math.random() < 0.5 ? 1 : -1; // Random tilt direction |
| 52 | + return balloon.animate( |
| 53 | + [ |
| 54 | + { |
| 55 | + transform: `translate(-50%, 0%) translate3d(${x}px, ${y}px, ${z}px) rotate3d(0, 0, 1, ${ |
| 56 | + tiltDirection * -tiltAngle |
| 57 | + }deg)`, |
| 58 | + opacity: 1, |
| 59 | + }, |
| 60 | + { |
| 61 | + transform: `translate(-50%, 0%) translate3d(${ |
| 62 | + x + (targetX - x) / 2 |
| 63 | + }px, ${y + (y + targetY * 5 - y) / 2}px, ${ |
| 64 | + z + (targetZ - z) / 2 |
| 65 | + }px) rotate3d(0, 0, 1, ${tiltDirection * tiltAngle}deg)`, |
| 66 | + opacity: 1, |
| 67 | + offset: 0.5, |
| 68 | + }, |
| 69 | + { |
| 70 | + transform: `translate(-50%, 0%) translate3d(${targetX}px, ${ |
| 71 | + y + targetY * 5 |
| 72 | + }px, ${targetZ}px) rotate3d(0, 0, 1, ${ |
| 73 | + tiltDirection * -tiltAngle |
| 74 | + }deg)`, |
| 75 | + opacity: 1, |
| 76 | + }, |
| 77 | + ], |
| 78 | + { |
| 79 | + duration: (Math.random() * 1000 + 5000) * 5, |
| 80 | + easing: easings[Math.floor(Math.random() * easings.length)], |
| 81 | + delay: zIndex * 200, |
| 82 | + } |
| 83 | + ); |
| 84 | + }; |
| 85 | + return { balloon, getAnimation }; |
| 86 | +} |
| 87 | + |
| 88 | +export function balloons(): Promise<void> { |
| 89 | + return new Promise((resolve) => { |
| 90 | + const balloonsContainer = document.createElement("balloons"); |
| 91 | + |
| 92 | + Object.assign(balloonsContainer.style, { |
| 93 | + overflow: "hidden", |
| 94 | + position: "fixed", |
| 95 | + inset: "0", |
| 96 | + zIndex: "999", |
| 97 | + display: "inline-block", |
| 98 | + pointerEvents: "none", |
| 99 | + perspective: "1500px", |
| 100 | + perspectiveOrigin: "50vw 100vh", |
| 101 | + contain: "style, layout, paint", |
| 102 | + }); |
| 103 | + |
| 104 | + document.documentElement.appendChild(balloonsContainer); |
| 105 | + |
| 106 | + const sceneSize = { width: window.innerWidth, height: window.innerHeight }; |
| 107 | + // make balloon height relative to screen size for this nice bokeh/perspective effect |
| 108 | + const balloonHeight = Math.floor( |
| 109 | + Math.min(sceneSize.width, sceneSize.height) * 1 |
| 110 | + ); |
| 111 | + |
| 112 | + const balloonWidth = |
| 113 | + (balloonDefaultSize.width / balloonDefaultSize.height) * balloonHeight; |
| 114 | + let amount = Math.max( |
| 115 | + 7, |
| 116 | + Math.round(window.innerWidth / (balloonWidth / 2)) |
| 117 | + ); |
| 118 | + // make max dist depend on number of balloons and their size for realistic effect |
| 119 | + // we dont want them to be too separated or too squeezed together |
| 120 | + const maxDist = Math.max( |
| 121 | + (amount * balloonWidth) / 2, |
| 122 | + (balloonWidth / 2) * 10 |
| 123 | + ); |
| 124 | + |
| 125 | + type BallonPosition = { |
| 126 | + x: number; |
| 127 | + y: number; |
| 128 | + z: number; |
| 129 | + targetX: number; |
| 130 | + targetY: number; |
| 131 | + targetZ: number; |
| 132 | + }; |
| 133 | + |
| 134 | + let balloonPositions: BallonPosition[] = []; |
| 135 | + |
| 136 | + for (let i = 0; i < amount; i++) { |
| 137 | + const x = Math.round(sceneSize.width * Math.random()); |
| 138 | + // make sure balloons first render below the bottom of the screen |
| 139 | + const y = window.innerHeight; |
| 140 | + const z = Math.round(-1 * (Math.random() * maxDist)); |
| 141 | + |
| 142 | + const targetX = Math.round( |
| 143 | + x + Math.random() * balloonWidth * 6 * (Math.random() > 0.5 ? 1 : -1) |
| 144 | + ); |
| 145 | + const targetY = -window.innerHeight; |
| 146 | + // balloons don't move in the Z direction |
| 147 | + const targetZ = z; |
| 148 | + balloonPositions.push({ |
| 149 | + x, |
| 150 | + y, |
| 151 | + z, |
| 152 | + targetX, |
| 153 | + targetY, |
| 154 | + targetZ, |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + balloonPositions = balloonPositions.sort((a, b) => a.z - b.z); |
| 159 | + const closestBallonPosition = balloonPositions[balloonPositions.length - 1]; |
| 160 | + const farthestBallonPosition = balloonPositions[0]; |
| 161 | + // console.log({ closestBallonPosition, farthestBallonPosition }); |
| 162 | + balloonPositions = balloonPositions.map((pos) => ({ |
| 163 | + ...pos, |
| 164 | + z: pos.z - closestBallonPosition.z, |
| 165 | + targetZ: pos.z - closestBallonPosition.z, |
| 166 | + })); |
| 167 | + |
| 168 | + const filtersElement = document.createElement("div"); |
| 169 | + filtersElement.innerHTML = svgFiltersHtml; |
| 170 | + balloonsContainer.appendChild(filtersElement); |
| 171 | + |
| 172 | + let currentZIndex = 1; |
| 173 | + |
| 174 | + const animations = balloonPositions.map((pos, index) => { |
| 175 | + const colorPair = colorPairs[index % colorPairs.length]; |
| 176 | + |
| 177 | + const balloon = createBallonElement({ |
| 178 | + balloonColor: colorPair[1], |
| 179 | + lightColor: colorPair[0], |
| 180 | + width: balloonWidth, |
| 181 | + }); |
| 182 | + balloonsContainer.appendChild(balloon); |
| 183 | + |
| 184 | + return createBalloonAnimation({ |
| 185 | + balloon, |
| 186 | + ...pos, |
| 187 | + zIndex: currentZIndex++, |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + // Wait a bit for the balloon prerender |
| 192 | + requestAnimationFrame(() => { |
| 193 | + const animationPromises = animations.map(({ balloon, getAnimation }) => { |
| 194 | + const a = getAnimation(); |
| 195 | + return a.finished.then(() => { |
| 196 | + balloon.remove(); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + Promise.all(animationPromises).then(() => { |
| 201 | + balloonsContainer.remove(); |
| 202 | + resolve(); |
| 203 | + }); |
| 204 | + }); |
| 205 | + }); |
| 206 | +} |
0 commit comments