Spaces:
Running
Running
File size: 3,658 Bytes
7a1a88f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 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 |