A Canvas-based online chat room game where characters appear and move based on real-time chat messages from an API.
- Real-time Chat Integration: Fetches and displays chat messages from
https://phantoms-backend.onrender.com/onebot/latest/text - Character System:
- NPC characters with random images from job categories (Healer, Magical Ranged DPS, Melee DPS, Physical Ranged DPS, Tank)
- Hero character that can be controlled with keyboard
- All characters rendered at 52x60 pixels
- Movement System:
- NPCs move in either range-bound or free-roaming modes
- Characters face the direction they're moving
- Collision detection between NPCs (hero can pass through)
- Idle/moving states for more natural behavior
- Chat Bubbles:
- Display messages above characters
- Multiple bubbles per character
- Text truncation for long messages
- Fade effects for smooth transitions
- Visual Effects:
- Image flipping based on movement direction
- Fade-in/out effects for characters and bubbles
- Transparent chat bubbles
- Canvas API: For rendering characters, backgrounds, and chat bubbles
- JavaScript Game Loop: Using
requestAnimationFramefor smooth animation - Fetch API: For retrieving chat messages
- Collision Detection: Prevents NPCs from stacking too much
- Animation System: Uses alpha values for fade effects and state transitions
-
Clone or download the project files to your local machine
-
Start a local server:
# Using Python 3 python -m http.server 8000 # Using Python 2 python -m SimpleHTTPServer 8000 # Using Node.js (if available) npx http-server -p 8000
-
Open your browser and navigate to:
http://localhost:8000
- Arrow Keys: Move the Hero character around the scene
- Left/Right: Move horizontally and change facing direction
- Up/Down: Move vertically
The game fetches chat messages from:
- Endpoint:
https://phantoms-backend.onrender.com/onebot/latest/text - Method: GET
- Frequency: Every 5 seconds
The API returns messages in the following format:
[
{
"userId": "123456789",
"message": "Hello world!"
},
{
"userId": "987654321",
"message": "How are you?"
}
]phantom_canvas_game/
├── index.html # Main HTML file
├── js/
│ └── game.js # Core game logic
├── images/
│ ├── background.png # Background image
│ ├── hero.png # Hero character image
│ └── jobs/ # NPC character images by job category
│ ├── Healer/
│ ├── Magical Ranged DPS/
│ ├── Melee DPS/
│ ├── Physical Ranged DPS/
│ └── Tank/
└── README.md # This file
- Each chat message from a unique
userIdspawns a character - NPCs use random images from the available job categories
- The Hero character is associated with a specific
userId(3146672611) - Characters fade in when they appear and fade out when they leave
- Range Mode: NPCs move within a defined area around their spawn point
- Free Mode: NPCs move freely around the entire map
- Idle/Moving States: NPCs alternate between idle (5-15 seconds) and moving (2-5 seconds)
- Collision Detection: NPCs can overlap slightly (10px) but not stack completely
- Displayed above characters' heads
- Multiple bubbles per character (newest at bottom)
- Text truncated to "..." for long messages
- Fade out after 30 seconds
- Move with characters as they walk
The game should work in all modern browsers that support:
- HTML5 Canvas
- JavaScript ES5+
- Fetch API
- No characters appear: Check if the API endpoint is accessible and returning valid data
- Characters not moving: Ensure the game loop is running properly
- Images not loading: Verify the image paths are correct and files exist
- Open browser developer tools (F12)
- Check the Console tab for error messages
- Verify network requests to the API endpoint
This project is open source and available for modification and distribution.
- Character images from various job categories
- Canvas API for rendering
- Fetch API for data retrieval
- JavaScript game development techniques