/* ===================================================================== playerfig.js — shared basketball player renderer for the Sport on Earth Games arcade. Adapted from the Family Football Times Interactive Lab's playerfig.js approach (one helper every game uses, so players are drawn as real figures, never plain circles), rebuilt for basketball: jerseys with numbers, baggy shorts, sneakers, hair, and shooting/dribbling poses. Exposes window.HoopsFig: .draw(ctx, o) side-view player; o.x/o.y = feet center .top(ctx, o) top-down player (for full-court games) .appearance(seed) deterministic {skin, hair, hairColor} from a seed .SKINS / .HAIR_STYLES / .HAIR_COLORS draw opts: { x, y, s=1, flip=false, pose='idle'|'dribble'|'shoot'| 'run'|'defend'|'celebrate', t=0 (animation phase, seconds), lift=0 (px the figure is airborne), jersey='#14418f', trim='#fff', shorts='#111', num='0', numColor, skin (hex or index), hair (style name or index), hairColor (hex or index), ball=false, label, labelColor } top opts: { x, y, r=13, dir=0 (radians facing), jersey, trim, num, skin, hair, hairColor, ball=false, label, labelColor, ring } ===================================================================== */ (function (global) { "use strict"; var SKINS = ["#f8d0a8", "#f0bd8e", "#e0a173", "#c68642", "#9c6642", "#6f4423"]; var HAIR_COLORS = ["#17130e", "#2e1d10", "#5b3a1a", "#8a5a24", "#c9a227", "#b8b8b8", "#7a1f1f", "#20303c"]; var HAIR_STYLES = ["short", "fade", "fro", "curly", "mohawk", "braids", "long", "bald"]; function pick(arr, v) { if (typeof v === "number") return arr[((v % arr.length) + arr.length) % arr.length]; return v || arr[0]; } function hashSeed(seed) { var s = String(seed == null ? "0" : seed), h = 2166136261; for (var i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = (h * 16777619) >>> 0; } return h; } function appearance(seed) { var h = hashSeed(seed); return { skin: SKINS[h % SKINS.length], hair: HAIR_STYLES[Math.floor(h / 7) % HAIR_STYLES.length], hairColor: HAIR_COLORS[Math.floor(h / 49) % HAIR_COLORS.length], build: Math.floor(h / 343) % 3 /* 0 = guard frame, 1 = wing, 2 = big */ }; } function rr(ctx, x, y, w, h, r) { ctx.beginPath(); ctx.moveTo(x + r, y); ctx.arcTo(x + w, y, x + w, y + h, r); ctx.arcTo(x + w, y + h, x, y + h, r); ctx.arcTo(x, y + h, x, y, r); ctx.arcTo(x, y, x + w, y, r); ctx.closePath(); } function ballAt(ctx, x, y, r) { ctx.save(); ctx.fillStyle = "#e25822"; ctx.beginPath(); ctx.arc(x, y, r, 0, 7); ctx.fill(); ctx.strokeStyle = "rgba(90,30,5,.65)"; ctx.lineWidth = Math.max(1, r * 0.12); ctx.beginPath(); ctx.arc(x, y, r, 0, 7); ctx.stroke(); ctx.beginPath(); ctx.moveTo(x - r, y); ctx.lineTo(x + r, y); ctx.stroke(); ctx.beginPath(); ctx.moveTo(x, y - r); ctx.lineTo(x, y + r); ctx.stroke(); ctx.beginPath(); ctx.arc(x - r * 1.5, y, r * 1.18, -0.5, 0.5); ctx.stroke(); ctx.beginPath(); ctx.arc(x + r * 1.5, y, r * 1.18, Math.PI - 0.5, Math.PI + 0.5); ctx.stroke(); ctx.restore(); } function hairDraw(ctx, style, color, hx, hy, hr) { ctx.fillStyle = color; if (style === "bald") return; if (style === "fro") { ctx.beginPath(); ctx.arc(hx, hy - hr * 0.35, hr * 1.28, 0, 7); ctx.fill(); return; } if (style === "curly") { var offs = [[-0.55, -0.62], [0, -0.85], [0.55, -0.62], [-0.3, -0.35], [0.3, -0.35]]; for (var i = 0; i < offs.length; i++) { ctx.beginPath(); ctx.arc(hx + offs[i][0] * hr, hy + offs[i][1] * hr, hr * 0.52, 0, 7); ctx.fill(); } return; } /* cap styles: short / fade / braids / long share a top cap */ ctx.beginPath(); ctx.arc(hx, hy - hr * 0.14, hr * 1.02, Math.PI * 1.02, Math.PI * 1.98); ctx.closePath(); ctx.fill(); if (style === "fade") { ctx.fillRect(hx - hr * 0.98, hy - hr * 0.32, hr * 1.96, hr * 0.20); } if (style === "mohawk") { rr(ctx, hx - hr * 0.22, hy - hr * 1.75, hr * 0.44, hr * 1.05, hr * 0.2); ctx.fill(); } if (style === "braids") { ctx.lineWidth = hr * 0.30; ctx.strokeStyle = color; ctx.lineCap = "round"; for (var b = -1; b <= 1; b++) { ctx.beginPath(); ctx.moveTo(hx - hr * 0.85, hy - hr * 0.25); ctx.lineTo(hx - hr * 0.95, hy + hr * (0.75 + Math.abs(b) * 0.28)); ctx.stroke(); } } if (style === "long") { rr(ctx, hx - hr * 1.02, hy - hr * 0.30, hr * 0.5, hr * 2.1, hr * 0.22); ctx.fill(); } } /* ---------------- side view ---------------- */ function draw(ctx, o) { o = o || {}; var s = o.s || 1, f = o.flip ? -1 : 1; var pose = o.pose || "idle"; var t = o.t || 0; var skin = pick(SKINS, o.skin), hairColor = pick(HAIR_COLORS, o.hairColor); var hair = typeof o.hair === "number" ? HAIR_STYLES[((o.hair % 8) + 8) % 8] : (o.hair || "short"); var jersey = o.jersey || "#14418f", trim = o.trim || "#ffffff", shortsC = o.shorts || "#14161c"; var crouch = pose === "dribble" ? 4 : pose === "defend" ? 6 : 0; var run = pose === "run"; var ph = t * 9; /* run/dribble phase */ var bob = run ? Math.abs(Math.sin(ph)) * 2.2 : pose === "defend" ? Math.sin(t * 2.4) * 0.8 : 0; ctx.save(); ctx.translate(o.x || 0, (o.y || 0) - (o.lift || 0)); /* shadow stays on the floor even when the figure jumps */ ctx.fillStyle = "rgba(0,0,0,0.22)"; ctx.beginPath(); ctx.ellipse(0, 2 + (o.lift || 0), 15, 3.6, 0, 0, Math.PI * 2); ctx.fill(); ctx.translate(0, -bob + crouch * 0); /* crouch handled in joints below */ ctx.scale(f * s, s); ctx.lineCap = "round"; /* ---- legs + sneakers ---- */ var hipY = -36 + crouch, kneeDrop = 20; var feet; if (run) { var sw = Math.sin(ph); feet = [ { x: -3 + sw * 11, y: -Math.max(0, Math.cos(ph)) * 5, knee: -3 + sw * 6 }, { x: 4 - sw * 11, y: -Math.max(0, -Math.cos(ph)) * 5, knee: 4 - sw * 6 } ]; } else if (pose === "defend") feet = [{ x: -14, y: 0, knee: -9 }, { x: 15, y: 0, knee: 10 }]; else if (pose === "shoot") feet = [{ x: -5, y: 0, knee: -3 }, { x: 6, y: -1, knee: 4 }]; else if (pose === "dribble") feet = [{ x: -9, y: 0, knee: -6 }, { x: 10, y: 0, knee: 7 }]; else feet = [{ x: -6, y: 0, knee: -4 }, { x: 7, y: 0, knee: 5 }]; /* skin lower legs */ ctx.strokeStyle = skin; ctx.lineWidth = 5; for (var i = 0; i < 2; i++) { var ft = feet[i]; ctx.beginPath(); ctx.moveTo(ft.knee, hipY + kneeDrop); ctx.lineTo(ft.x, ft.y - 3); ctx.stroke(); } /* sneakers */ for (var j = 0; j < 2; j++) { var fq = feet[j]; ctx.fillStyle = "#f4f4f6"; rr(ctx, fq.x - 4.5, fq.y - 3.4, 10, 3.8, 1.6); ctx.fill(); ctx.fillStyle = trim; ctx.fillRect(fq.x - 4.5, fq.y - 1.1, 10, 1.1); } /* shorts (baggy, to the knee) */ ctx.fillStyle = shortsC; rr(ctx, -10.5, hipY - 1, 22, 22, 4); ctx.fill(); ctx.fillStyle = trim; ctx.fillRect(-10.5, hipY + 17.5, 22, 2.6); ctx.fillRect(-1.2, hipY - 1, 2.4, 21); /* ---- back arm ---- */ var shY = -70 + crouch, shX = -6; var backHand; if (pose === "shoot" || pose === "celebrate") backHand = { x: -3, y: -93 + crouch }; else if (pose === "defend") backHand = { x: -21, y: -56 + crouch + Math.sin(t * 3) * 2 }; else if (run) backHand = { x: -11 - Math.sin(ph) * 4, y: -50 + crouch }; else if (pose === "dribble") backHand = { x: -14, y: -50 + crouch }; else backHand = { x: -11, y: -45 + crouch }; ctx.strokeStyle = skin; ctx.lineWidth = 4.6; ctx.beginPath(); ctx.moveTo(shX, shY); ctx.lineTo(backHand.x, backHand.y); ctx.stroke(); /* sleeve */ ctx.strokeStyle = jersey; ctx.lineWidth = 6.4; ctx.beginPath(); ctx.moveTo(shX, shY); ctx.lineTo(shX + (backHand.x - shX) * 0.34, shY + (backHand.y - shY) * 0.34); ctx.stroke(); /* ---- torso / jersey ---- */ ctx.fillStyle = jersey; rr(ctx, -9, -78 + crouch, 19, 30, 5); ctx.fill(); ctx.strokeStyle = trim; ctx.lineWidth = 1.6; ctx.beginPath(); ctx.moveTo(-9, -78 + crouch); ctx.lineTo(0.5, -68 + crouch); ctx.lineTo(10, -78 + crouch); ctx.stroke(); ctx.fillStyle = trim; ctx.fillRect(-9, -52 + crouch, 19, 2.2); if (o.num !== undefined && o.num !== null && o.num !== "") { ctx.fillStyle = o.numColor || "#ffffff"; ctx.font = "bold 11px ui-monospace,monospace"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText(String(o.num), 0.5, -61 + crouch); } /* ---- head + hair ---- */ var hx = 1.5, hy = -86 + crouch, hr = 8.6; ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx, hy, hr, 0, 7); ctx.fill(); hairDraw(ctx, hair, hairColor, hx, hy, hr); /* simple face hint: eye away from heavy detail */ ctx.fillStyle = "rgba(0,0,0,0.5)"; ctx.beginPath(); ctx.arc(hx + 4.6, hy + 0.6, 1.05, 0, 7); ctx.fill(); /* ---- front arm ---- */ var fShX = 7, fShY = shY, frontHand, ballPos = null; if (pose === "shoot") { frontHand = { x: 11, y: -94 + crouch }; ballPos = { x: 10, y: -100 + crouch, r: 7.4 }; } else if (pose === "celebrate") frontHand = { x: 13, y: -95 + crouch }; else if (pose === "defend") frontHand = { x: 21, y: -58 + crouch - Math.sin(t * 3) * 2 }; else if (run) frontHand = { x: 11 + Math.sin(ph) * 4, y: -52 + crouch }; else if (pose === "dribble") { frontHand = { x: 14, y: -34 + crouch }; ballPos = { x: 15, y: -16 - Math.abs(Math.sin(t * 5.2)) * 11, r: 7 }; } else frontHand = { x: 11, y: -46 + crouch }; if (pose === "idle" && o.ball) ballPos = { x: 12, y: -30 + crouch, r: 7 }; ctx.strokeStyle = skin; ctx.lineWidth = 4.6; ctx.beginPath(); ctx.moveTo(fShX, fShY); ctx.lineTo(frontHand.x, frontHand.y); ctx.stroke(); ctx.strokeStyle = jersey; ctx.lineWidth = 6.4; ctx.beginPath(); ctx.moveTo(fShX, fShY); ctx.lineTo(fShX + (frontHand.x - fShX) * 0.34, fShY + (frontHand.y - fShY) * 0.34); ctx.stroke(); if (o.ball && ballPos) ballAt(ctx, ballPos.x, ballPos.y, ballPos.r); ctx.restore(); if (o.label) { ctx.save(); ctx.font = "bold " + Math.max(9, Math.round(10.5 * s)) + "px ui-monospace,monospace"; ctx.textAlign = "center"; ctx.textBaseline = "alphabetic"; ctx.lineWidth = 3.4; ctx.strokeStyle = "rgba(0,0,0,0.6)"; var ly = (o.y || 0) - (o.lift || 0) - 104 * s; ctx.strokeText(o.label, o.x || 0, ly); ctx.fillStyle = o.labelColor || "#ffffff"; ctx.fillText(o.label, o.x || 0, ly); ctx.restore(); } } /* ---------------- top-down view ---------------- */ function top(ctx, o) { o = o || {}; var r = o.r || 13, dir = o.dir || 0; var build = (o.build == null ? 1 : o.build); var bw = [0.90, 1, 1.12][build] || 1; /* shoulder width */ var bl = [0.95, 1, 1.07][build] || 1; /* torso length */ var skin = pick(SKINS, o.skin), hairColor = pick(HAIR_COLORS, o.hairColor); var hair = typeof o.hair === "number" ? HAIR_STYLES[((o.hair % 8) + 8) % 8] : (o.hair || "short"); var jersey = o.jersey || "#14418f", trim = o.trim || "#ffffff"; var shortsC = o.shorts || "#14161c"; var dx = Math.cos(dir), dy = Math.sin(dir), px = -dy, py = dx; var rot = Math.atan2(dy, dx); ctx.save(); ctx.translate(o.x || 0, o.y || 0); ctx.fillStyle = "rgba(0,0,0,0.25)"; ctx.beginPath(); ctx.ellipse(1.5, 2.5, r * bw * 1.08, r * 0.95, 0, 0, Math.PI * 2); ctx.fill(); if (o.ring) { ctx.strokeStyle = o.ring; ctx.lineWidth = 2.4; ctx.beginPath(); ctx.arc(0, 0, r * bw + 5, 0, 7); ctx.stroke(); } /* shorts: two dark blocks at the back of the torso */ for (var q = -1; q <= 1; q += 2) { ctx.fillStyle = shortsC; ctx.beginPath(); ctx.arc(-dx * r * 0.74 + px * q * r * bw * 0.40, -dy * r * 0.74 + py * q * r * bw * 0.40, r * 0.34, 0, 7); ctx.fill(); } /* torso: ellipse aligned to facing, wider for bigs */ ctx.fillStyle = jersey; ctx.beginPath(); ctx.ellipse(0, 0, r * bl, r * bw, rot, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = trim; ctx.lineWidth = 2; ctx.beginPath(); ctx.ellipse(0, 0, r * bl - 1, r * bw - 1, rot, 0, Math.PI * 2); ctx.stroke(); /* chest trim stripe across the shoulders */ ctx.strokeStyle = trim; ctx.lineWidth = 2.2; ctx.beginPath(); ctx.moveTo(dx * r * 0.34 - px * r * bw * 0.66, dy * r * 0.34 - py * r * bw * 0.66); ctx.lineTo(dx * r * 0.34 + px * r * bw * 0.66, dy * r * 0.34 + py * r * bw * 0.66); ctx.stroke(); /* arms: jersey sleeves at the shoulders + skin hands */ for (var sd = -1; sd <= 1; sd += 2) { var sx = px * sd * r * bw * 0.99 + dx * r * 0.16, sy = py * sd * r * bw * 0.99 + dy * r * 0.16; ctx.fillStyle = jersey; ctx.beginPath(); ctx.arc(sx, sy, r * 0.42, 0, 7); ctx.fill(); ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(sx + px * sd * r * 0.32 + dx * r * 0.18, sy + py * sd * r * 0.32 + dy * r * 0.18, r * 0.26, 0, 7); ctx.fill(); } /* head: skin base, then the player's actual hair style on top */ var hx = dx * r * 0.34, hy = dy * r * 0.34, hr = r * 0.52; ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx, hy, hr, 0, 7); ctx.fill(); ctx.fillStyle = hairColor; if (hair === "bald") { ctx.fillStyle = "rgba(255,255,255,0.20)"; ctx.beginPath(); ctx.arc(hx - dx * hr * 0.2, hy - dy * hr * 0.2, hr * 0.42, 0, 7); ctx.fill(); } else if (hair === "fro") { ctx.beginPath(); ctx.arc(hx - dx * hr * 0.18, hy - dy * hr * 0.18, hr * 1.22, 0, 7); ctx.fill(); ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx + dx * hr * 0.55, hy + dy * hr * 0.55, hr * 0.52, 0, 7); ctx.fill(); } else if (hair === "braids" || hair === "long") { ctx.beginPath(); ctx.ellipse(hx - dx * hr * 0.34, hy - dy * hr * 0.34, hr * 1.02, hr * 0.86, rot, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.arc(hx, hy, hr * 0.94, 0, 7); ctx.fill(); ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx + dx * hr * 0.52, hy + dy * hr * 0.52, hr * 0.55, 0, 7); ctx.fill(); } else if (hair === "mohawk") { ctx.beginPath(); ctx.arc(hx, hy, hr * 0.92, 0, 7); ctx.fill(); ctx.save(); ctx.translate(hx, hy); ctx.rotate(rot); rr(ctx, -hr * 1.05, -hr * 0.20, hr * 1.5, hr * 0.40, hr * 0.16); ctx.fill(); ctx.restore(); ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx + dx * hr * 0.5, hy + dy * hr * 0.5, hr * 0.5, 0, 7); ctx.fill(); } else if (hair === "curly") { var offs = [[0, 0], [-0.5, -0.3], [0.5, -0.3], [-0.3, 0.35], [0.3, 0.35], [0, -0.55]]; for (var ci = 0; ci < offs.length; ci++) { ctx.beginPath(); ctx.arc(hx + px * offs[ci][0] * hr - dx * hr * 0.15, hy + py * offs[ci][0] * hr - dy * hr * 0.15 + (offs[ci][1] * hr * 0), hr * 0.52, 0, 7); ctx.fill(); } ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx + dx * hr * 0.55, hy + dy * hr * 0.55, hr * 0.48, 0, 7); ctx.fill(); } else { /* short / fade: cap swept back from the face */ ctx.beginPath(); ctx.arc(hx - dx * hr * 0.22, hy - dy * hr * 0.22, hr * 0.98, 0, 7); ctx.fill(); ctx.fillStyle = skin; ctx.beginPath(); ctx.arc(hx + dx * hr * 0.48, hy + dy * hr * 0.48, hr * 0.58, 0, 7); ctx.fill(); } /* number on the back */ if (o.num !== undefined && o.num !== null && o.num !== "") { ctx.fillStyle = o.numColor || "#ffffff"; ctx.font = "bold " + Math.round(r * 0.9) + "px ui-monospace,monospace"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText(String(o.num), -dx * r * 0.38, -dy * r * 0.38); } if (o.ball) { var bx = dx * (r * bl + 7), by = dy * (r * bl + 7) - ((o.t || 0) % 0.5) * 2; ballAt(ctx, bx, by, 6.2); } ctx.restore(); if (o.label) { ctx.save(); ctx.font = "bold 9.5px ui-monospace,monospace"; ctx.textAlign = "center"; ctx.textBaseline = "alphabetic"; ctx.lineWidth = 3.2; ctx.strokeStyle = "rgba(0,0,0,0.65)"; ctx.strokeText(o.label, o.x || 0, (o.y || 0) - r * bw - 8); ctx.fillStyle = o.labelColor || "#ffffff"; ctx.fillText(o.label, o.x || 0, (o.y || 0) - r * bw - 8); ctx.restore(); } } global.HoopsFig = { draw: draw, top: top, appearance: appearance, ball: ballAt, SKINS: SKINS, HAIR_STYLES: HAIR_STYLES, HAIR_COLORS: HAIR_COLORS }; })(window);