Spaces:
Running
Running
Download script.js from ngrandson/pixel-walker-party: direct link, hf CLI and curl.
- Browser
- Download file 3.66 kB
-
https://huggingface.co/spaces/ngrandson/pixel-walker-party/resolve/main/script.js
- Command line
-
hf download hf://spaces/ngrandson/pixel-walker-party/script.js
-
curl -L -o script.js https://huggingface.co/spaces/ngrandson/pixel-walker-party/resolve/main/script.js
3.66 kB
| const COLORS = { | |
| skin: '#EACB8A', | |
| shirt: '#E42D43', | |
| pants: '#5F3A32', | |
| eyes: '#4A4A4A', | |
| bg: '#201e1e' | |
| }; | |
| // Canvas setup | |
| const canvas = document.getElementById('characterCanvas'); | |
| const ctx = canvas.getContext('2d'); | |
| // Animation variables | |
| let isWalking = false; | |
| let blinkLevel = 0; | |
| let walkCycle = { progress: 0 }; | |
| let time = 0; | |
| let frameId = null; | |
| let walkTween = null; | |
| let blinkTimeout = null; | |
| // Resize canvas to fill window | |
| function resizeCanvas() { | |
| const dpr = window.devicePixelRatio || 1; | |
| canvas.width = window.innerWidth * dpr; | |
| canvas.height = window.innerHeight * dpr; | |
| canvas.style.width = `${window.innerWidth}px`; | |
| canvas.style.height = `${window.innerHeight}px`; | |
| ctx.scale(dpr, dpr); | |
| } | |
| // Draw functions | |
| function drawRect(x, y, w, h, color, opacity = 1) { | |
| ctx.save(); | |
| ctx.globalAlpha = opacity; | |
| ctx.fillStyle = color; | |
| ctx.fillRect(x, y, w, h); | |
| ctx.restore(); | |
| } | |
| function drawCharacter(time, blinkLevel) { | |
| const sway = Math.sin(time / 800) * 3; | |
| const leftLeg = Math.sin(walkCycle.progress * Math.PI * 2) * -8; | |
| const rightLeg = Math.sin(walkCycle.progress * Math.PI * 2 + Math.PI) * -8; | |
| ctx.save(); | |
| ctx.translate(window.innerWidth / 2 - 70, window.innerHeight / 2 - 100); | |
| ctx.translate(sway, 0); | |
| // Arms + shirt | |
| drawRect(0, 103, 20, 44, COLORS.skin); | |
| drawRect(120, 103, 20, 44, COLORS.skin); | |
| drawRect(0, 103, 140, 21, COLORS.shirt); | |
| // Body | |
| drawRect(17, 111, 111, 55, COLORS.shirt); | |
| // Legs | |
| drawRect(17, 166 + leftLeg, 47, 28, COLORS.pants); | |
| drawRect(84, 166 + rightLeg, 44, 28, COLORS.pants); | |
| // Head | |
| drawRect(12, -16, 120, 111, COLORS.skin); | |
| // Eyes (blinking) | |
| const eyeH = Math.max(2, 23 - blinkLevel * 23); | |
| drawRect(60, 45, 14, eyeH, COLORS.eyes); | |
| drawRect(100, 45, 14, eyeH, COLORS.eyes); | |
| ctx.restore(); | |
| } | |
| // Animation functions | |
| function blinkNTimes(count = 1) { | |
| const tl = gsap.timeline(); | |
| for (let i = 0; i < count; i++) { | |
| tl.to({ blinkLevel: 0 }, { blinkLevel: 1, duration: 0.1 }) | |
| .to({ blinkLevel: 0 }, { blinkLevel: 0, duration: 0.1 }, '+=0.05'); | |
| } | |
| return tl; | |
| } | |
| function scheduleBlink() { | |
| const delay = 2000 + Math.random() * 3000; | |
| const times = Math.random() < 0.3 ? 2 : 1; | |
| blinkTimeout = setTimeout(() => { | |
| blinkNTimes(times); | |
| scheduleBlink(); | |
| }, delay); | |
| } | |
| function updateWalkingAnimation(isActive) { | |
| if (isActive && (!walkTween || !walkTween.isActive())) { | |
| walkTween = gsap.to(walkCycle, { | |
| progress: 1, | |
| duration: 0.6, | |
| ease: 'none', | |
| repeat: -1 | |
| }); | |
| } else if (!isActive && walkTween) { | |
| walkTween.kill(); | |
| walkTween = null; | |
| walkCycle.progress = 0; | |
| } | |
| } | |
| // Main render loop | |
| function render(timestamp) { | |
| time = timestamp; | |
| ctx.clearRect(0, 0, canvas.width, canvas.height); | |
| drawCharacter(time, blinkLevel); | |
| frameId = requestAnimationFrame(render); | |
| } | |
| // Event listeners | |
| document.getElementById('walkBtn').addEventListener('click', () => { | |
| isWalking = !isWalking; | |
| document.getElementById('walkBtn').classList.toggle('bg-green-600', isWalking); | |
| updateWalkingAnimation(isWalking); | |
| }); | |
| document.getElementById('blinkBtn').addEventListener('click', () => { | |
| blinkNTimes(3); | |
| }); | |
| // Color pickers | |
| document.getElementById('skinColor').addEventListener('change', (e) => { | |
| COLORS.skin = e.target.value; | |
| }); | |
| document.getElementById('shirtColor').addEventListener('change', (e) => { | |
| COLORS.shirt = e.target.value; | |
| }); | |
| document.getElementById('pantsColor').addEventListener('change |