import React, { useEffect, useRef } from "react"; import Color from 'color'; import { Player } from "../main/GameReader"; // @ts-ignore import alive from '../../static/alive.png'; // @ts-ignore import dead from '../../static/dead.png'; import { backLayerHats, hatOffsets, hats, skins } from "./cosmetics"; import Tooltip from "react-tooltip-lite"; export interface CanvasProps { src: string; hat: number; skin: number; color: string; shadow: string; isAlive: boolean; } export interface AvatarProps { talking: boolean; borderColor: string; isAlive: boolean; player: Player; size: number; deafened?: boolean; } const playerColors = [ ['#C51111', '#7A0838',], ['#132ED1', '#09158E',], ['#117F2D', '#0A4D2E',], ['#ED54BA', '#AB2BAD',], ['#EF7D0D', '#B33E15',], ['#F5F557', '#C38823',], ['#3F474E', '#1E1F26',], ['#D6E0F0', '#8394BF',], ['#6B2FBB', '#3B177C',], ['#71491E', '#5E2615',], ['#38FEDC', '#24A8BE',], ['#50EF39', '#15A742',] ]; export default function Avatar({ talking, deafened, borderColor, isAlive, player, size }: AvatarProps) { let color = playerColors[player.colorId]; if (!color) color = playerColors[0]; return (
{ deafened && }
); } function Canvas({ src, hat, skin, color, shadow, isAlive }: CanvasProps) { const canvas = useRef(null); const hatImg = useRef(null); const skinImg = useRef(null); const image = useRef(null); useEffect(() => { (async () => { if (!canvas.current || !image.current || !hatImg.current || !skinImg.current) return; const ctx = canvas.current.getContext('2d')!; if (!image.current.complete) { await new Promise(r => image!.current!.onload = r); } if (!hatImg.current.complete) { await new Promise(r => hatImg!.current!.onload = r); } if (!skinImg.current.complete) { await new Promise(r => skinImg!.current!.onload = r); } canvas.current.width = image.current.width; canvas.current.height = image.current.height; ctx.clearRect(0, 0, canvas.current.width, canvas.current.height); ctx.drawImage(image.current, 0, 0); function drawHat() { let hatY = 17 - hatOffsets[hat]; ctx.drawImage(hatImg.current!, 0, hatY > 0 ? 0 : -hatY, hatImg.current!.width, hatImg.current!.height, canvas.current!.width / 2 - hatImg.current!.width / 2 + 2, Math.max(hatY, 0), hatImg.current!.width, hatImg.current!.height); } let data = ctx.getImageData(0, 0, image.current.width, image.current.height); for (let i = 0; i < data.data.length; i += 4) { let r = data.data[i], g = data.data[i + 1], b = data.data[i + 2]; if (r !== 255 || g !== 255 || b !== 255) { let pixelColor = Color('#000000') .mix(Color(shadow), b / 255) .mix(Color(color), r / 255) .mix(Color('#9acad5'), g / 255); data.data[i] = pixelColor.red(); data.data[i + 1] = pixelColor.green(); data.data[i + 2] = pixelColor.blue(); } } ctx.putImageData(data, 0, 0); if (isAlive) { if (backLayerHats.has(hat)) ctx.globalCompositeOperation = 'destination-over'; drawHat(); ctx.globalCompositeOperation = 'source-over'; ctx.drawImage(skinImg.current, 25, 46); } })(); }, [src, color, shadow, hat, skin, isAlive]); return ( <> ) }