|
| 1 | +/* Brand playground: one owner for mood, discoveries and transient particles. */ |
| 2 | +/* global document, window, matchMedia, devicePixelRatio, innerWidth, innerHeight, requestAnimationFrame, cancelAnimationFrame, performance */ |
| 3 | +{ |
| 4 | + const world = document.querySelector('#playground'); |
| 5 | + const friend = document.querySelector('#little-friend'); |
| 6 | + const wrap = document.querySelector('#friend-wrap'); |
| 7 | + const bubble = document.querySelector('#hello-bubble'); |
| 8 | + const prop = document.querySelector('#held-prop'); |
| 9 | + const fountain = document.querySelector('#fountain'); |
| 10 | + const reduced = matchMedia('(prefers-reduced-motion: reduce)'); |
| 11 | + const moods = { |
| 12 | + hello: ['嗨,你来啦。', '不只眼前这件事,你在意的事我也想懂。'], |
| 13 | + party: ['这一步,值得庆祝!', '大事小事,都陪你认真完成。'], |
| 14 | + dream: ['你休息,我轻一点。', '好的搭档,也知道什么时候不打扰。'], |
| 15 | + bloom: ['这个想法,一起养大吧。', '先聊聊,再把它接上你的计划。'], |
| 16 | + }; |
| 17 | + const greetings = [ |
| 18 | + ['在呢。', '你的计划、烦恼、小灵感,都可以和我聊。'], |
| 19 | + ['戳到一个认真听你的我。', '一起想,也一起把事情做完。'], |
| 20 | + ['被你发现了,我有点好玩。', '做事也认真,会从经验里改进。'], |
| 21 | + ]; |
| 22 | + const discovered = new Set(); |
| 23 | + let greeting = 0; |
| 24 | + let surpriseFound = false; |
| 25 | + let bounce; |
| 26 | + let drawId = 0; |
| 27 | + let particles = []; |
| 28 | + let emissionUntil = 0; |
| 29 | + let lastTime = 0; |
| 30 | + const canvas = document.createElement('canvas'); |
| 31 | + canvas.className = 'spark-canvas'; |
| 32 | + canvas.setAttribute('aria-hidden', 'true'); |
| 33 | + document.body.append(canvas); |
| 34 | + const ctx = canvas.getContext('2d'); |
| 35 | + const colors = ['#c7a0e8', '#edb559', '#f29389', '#91bca3', '#e0c1ea']; |
| 36 | + |
| 37 | + function speak(lines) { |
| 38 | + const detail = document.createElement('span'); |
| 39 | + detail.textContent = lines[1]; |
| 40 | + bubble.replaceChildren(document.createTextNode(lines[0]), detail); |
| 41 | + } |
| 42 | + |
| 43 | + function hop() { |
| 44 | + bounce?.cancel(); |
| 45 | + if (reduced.matches) return; |
| 46 | + bounce = wrap.animate([ |
| 47 | + { translate: '0 0', rotate: '0deg' }, |
| 48 | + { translate: '0 -18px', rotate: '-5deg', offset: .42 }, |
| 49 | + { translate: '0 2px', rotate: '3deg', offset: .78 }, |
| 50 | + { translate: '0 0', rotate: '0deg' }, |
| 51 | + ], { duration: 620, easing: 'ease-out' }); |
| 52 | + } |
| 53 | + |
| 54 | + function resize() { |
| 55 | + const ratio = Math.min(devicePixelRatio || 1, 2); |
| 56 | + canvas.width = innerWidth * ratio; |
| 57 | + canvas.height = innerHeight * ratio; |
| 58 | + ctx?.setTransform(ratio, 0, 0, ratio, 0, 0); |
| 59 | + } |
| 60 | + |
| 61 | + function addSpark(x, y, burst = false) { |
| 62 | + if (particles.length >= 180) return; |
| 63 | + const angle = burst ? Math.random() * Math.PI * 2 : -Math.PI / 2 + .3 + (Math.random() - .5) * 1.05; |
| 64 | + const speed = burst ? 80 + Math.random() * 180 : 180 + Math.random() * 220; |
| 65 | + particles.push({ x, y, vx: Math.cos(angle) * speed, vy: Math.sin(angle) * speed, |
| 66 | + age: 0, life: .8 + Math.random() * .85, size: 2 + Math.random() * 4, |
| 67 | + angle: Math.random() * Math.PI, color: colors[Math.floor(Math.random() * colors.length)], star: Math.random() > .55 }); |
| 68 | + } |
| 69 | + |
| 70 | + function draw(time) { |
| 71 | + drawId = 0; |
| 72 | + if (!ctx || reduced.matches || document.hidden) { stopSparks(); return; } |
| 73 | + const delta = Math.min((time - (lastTime || time)) / 1000, .04); |
| 74 | + lastTime = time; |
| 75 | + ctx.clearRect(0, 0, innerWidth, innerHeight); |
| 76 | + if (time < emissionUntil) { |
| 77 | + const rect = prop.getBoundingClientRect(); |
| 78 | + for (let i = 0; i < 5; i++) addSpark(rect.left + rect.width * .63, rect.top + rect.height * .23); |
| 79 | + } |
| 80 | + particles = particles.filter(p => p.age < p.life); |
| 81 | + for (const p of particles) { |
| 82 | + p.age += delta; |
| 83 | + p.x += p.vx * delta; |
| 84 | + p.y += p.vy * delta; |
| 85 | + p.vy += 210 * delta; |
| 86 | + p.angle += delta * 2; |
| 87 | + paintSpark(p); |
| 88 | + } |
| 89 | + if (particles.length || time < emissionUntil) drawId = requestAnimationFrame(draw); |
| 90 | + else { ctx.clearRect(0, 0, innerWidth, innerHeight); lastTime = 0; } |
| 91 | + } |
| 92 | + |
| 93 | + function paintSpark(p) { |
| 94 | + ctx.save(); |
| 95 | + ctx.translate(p.x, p.y); |
| 96 | + ctx.rotate(p.angle); |
| 97 | + ctx.globalAlpha = Math.max(0, 1 - p.age / p.life); |
| 98 | + ctx.fillStyle = p.color; |
| 99 | + if (p.star) { |
| 100 | + ctx.beginPath(); |
| 101 | + for (let i = 0; i < 8; i++) { |
| 102 | + const radius = i % 2 ? p.size * .32 : p.size; |
| 103 | + const angle = i * Math.PI / 4; |
| 104 | + const x = Math.cos(angle) * radius, y = Math.sin(angle) * radius; |
| 105 | + if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); |
| 106 | + } |
| 107 | + ctx.closePath(); |
| 108 | + ctx.fill(); |
| 109 | + } else ctx.fillRect(-p.size / 2, -p.size / 2, p.size, p.size * .65); |
| 110 | + ctx.restore(); |
| 111 | + } |
| 112 | + |
| 113 | + function stopSparks() { |
| 114 | + cancelAnimationFrame(drawId); |
| 115 | + drawId = 0; |
| 116 | + particles = []; |
| 117 | + emissionUntil = 0; |
| 118 | + lastTime = 0; |
| 119 | + ctx?.clearRect(0, 0, innerWidth, innerHeight); |
| 120 | + } |
| 121 | + |
| 122 | + function spark(burst = false) { |
| 123 | + if (!ctx || reduced.matches || document.hidden) return; |
| 124 | + if (burst) { |
| 125 | + const rect = friend.getBoundingClientRect(); |
| 126 | + for (let i = 0; i < 70; i++) addSpark(rect.left + rect.width / 2, rect.top + rect.height / 3, true); |
| 127 | + } else emissionUntil = performance.now() + 1200; |
| 128 | + if (!drawId) drawId = requestAnimationFrame(draw); |
| 129 | + } |
| 130 | + |
| 131 | + function hold(button) { |
| 132 | + prop.replaceChildren(button.querySelector('svg').cloneNode(true)); |
| 133 | + world.dataset.holding = 'true'; |
| 134 | + world.dataset.prop = button.dataset.mood || 'fountain'; |
| 135 | + } |
| 136 | + |
| 137 | + for (const toy of document.querySelectorAll('.world-toy[data-mood]')) { |
| 138 | + toy.addEventListener('click', () => { |
| 139 | + stopSparks(); |
| 140 | + const mood = toy.dataset.mood; |
| 141 | + world.dataset.mood = mood; |
| 142 | + hold(toy); |
| 143 | + speak(moods[mood]); |
| 144 | + hop(); |
| 145 | + discovered.add(mood); |
| 146 | + if (mood === 'party') spark(true); |
| 147 | + if (discovered.size === 3 && !surpriseFound) { |
| 148 | + surpriseFound = true; |
| 149 | + speak(['彩蛋:有趣的灵魂,被你找到啦。', '谢谢你愿意了解我。现在,也让我了解你吧。']); |
| 150 | + spark(true); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + fountain.addEventListener('click', () => { |
| 155 | + world.dataset.mood = 'party'; |
| 156 | + hold(fountain); |
| 157 | + speak(['给平凡的一天,放一点花。', '完成一小步,也值得一起开心。']); |
| 158 | + hop(); |
| 159 | + spark(); |
| 160 | + }); |
| 161 | + friend.addEventListener('click', () => { |
| 162 | + speak(greetings[greeting++ % greetings.length]); |
| 163 | + hop(); |
| 164 | + if (world.dataset.mood === 'party') spark(prop.querySelector('.cone-stripe') === null); |
| 165 | + }); |
| 166 | + world.addEventListener('pointermove', event => { |
| 167 | + if (reduced.matches || event.pointerType !== 'mouse') return; |
| 168 | + const rect = friend.getBoundingClientRect(); |
| 169 | + friend.style.setProperty('--look-x', `${Math.max(-6, Math.min(6, (event.clientX - rect.left - rect.width / 2) / 30))}px`); |
| 170 | + friend.style.setProperty('--look-y', `${Math.max(-5, Math.min(5, (event.clientY - rect.top - rect.height / 2) / 30))}px`); |
| 171 | + }); |
| 172 | + world.addEventListener('pointerleave', () => { |
| 173 | + friend.style.removeProperty('--look-x'); |
| 174 | + friend.style.removeProperty('--look-y'); |
| 175 | + }); |
| 176 | + function settle() { stopSparks(); bounce?.cancel(); } |
| 177 | + document.addEventListener('visibilitychange', () => { if (document.hidden) settle(); }); |
| 178 | + reduced.addEventListener('change', () => { if (reduced.matches) settle(); }); |
| 179 | + window.addEventListener('pagehide', settle); |
| 180 | + window.addEventListener('resize', resize); |
| 181 | + resize(); |
| 182 | + hop(); |
| 183 | +} |
0 commit comments