Compare commits
22 commits
1f5f058519
...
b563215276
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b563215276 | ||
|
|
34b4109841 | ||
|
|
2d43e1c7b6 | ||
|
|
415919b3e1 | ||
|
|
1da96f34a6 | ||
|
|
ee5b0bee92 |
||
|
|
9b09f972b2 |
||
|
|
6d4d7fba7a |
||
|
|
58feaa68ef |
||
|
|
c029bdb44a |
||
|
|
ee9d3b4c48 |
||
|
|
29b84fbc89 |
||
|
|
42dad1c02a |
||
|
|
b4ecdb98ad |
||
|
|
c712f6a5dd |
||
|
|
1ee3d46b97 |
||
|
|
258085fdbe |
||
|
|
a76c7c6283 |
||
|
|
7861b71e41 |
||
|
|
0842752cd4 |
||
|
|
e2139e0cdb |
||
|
|
97b9fda920 |
70 changed files with 17888 additions and 1509 deletions
28
.claude/settings.local.json
Normal file
28
.claude/settings.local.json
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(mkdir:*)",
|
||||
"Bash(npm run build:*)",
|
||||
"Bash(npm view:*)",
|
||||
"Bash(npm install)",
|
||||
"Bash(rm:*)",
|
||||
"Bash(npm run dev:*)",
|
||||
"Bash(curl:*)",
|
||||
"Bash(node:*)",
|
||||
"Bash(taskkill:*)",
|
||||
"Bash(npx kill-port:*)",
|
||||
"Bash(mv:*)",
|
||||
"Bash(move start-dev.js scripts )",
|
||||
"Bash(move setup.js scripts)",
|
||||
"Bash(npm run type-check:*)",
|
||||
"Bash(git push:*)",
|
||||
"Bash(git add:*)",
|
||||
"Bash(git commit:*)"
|
||||
],
|
||||
"deny": [],
|
||||
"ask": [],
|
||||
"additionalDirectories": [
|
||||
"C:\\c\\Users\\marti"
|
||||
]
|
||||
}
|
||||
}
|
||||
24
.eslintrc.json
Normal file
24
.eslintrc.json
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"root": true,
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"prettier"
|
||||
],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": ["@typescript-eslint", "prettier"],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2020,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"env": {
|
||||
"node": true,
|
||||
"es6": true
|
||||
},
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
|
||||
"@typescript-eslint/explicit-function-return-type": "off",
|
||||
"@typescript-eslint/no-explicit-any": "warn"
|
||||
},
|
||||
"ignorePatterns": ["dist", "node_modules", ".next"]
|
||||
}
|
||||
121
.gitignore
vendored
121
.gitignore
vendored
|
|
@ -1,2 +1,121 @@
|
|||
node_modules
|
||||
# Dependencies
|
||||
node_modules/
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# Production builds
|
||||
dist/
|
||||
build/
|
||||
.next/
|
||||
|
||||
# Environment variables
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
# Database
|
||||
*.db
|
||||
*.sqlite
|
||||
*.sqlite3
|
||||
|
||||
# Logs
|
||||
logs/
|
||||
*.log
|
||||
|
||||
# Runtime data
|
||||
pids/
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage/
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
public
|
||||
|
||||
# Storybook build outputs
|
||||
.out
|
||||
.storybook-out
|
||||
|
||||
# Temporary folders
|
||||
tmp/
|
||||
temp/
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/
|
||||
.idea/
|
||||
.DS_Store
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS generated files
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Docker
|
||||
.dockerignore
|
||||
|
||||
# Canvas data (legacy)
|
||||
canvas_data.json
|
||||
|
||||
# Redis dumps
|
||||
dump.rdb
|
||||
|
||||
# PostgreSQL data
|
||||
postgres_data/
|
||||
8
.prettierrc
Normal file
8
.prettierrc
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"semi": true,
|
||||
"trailingComma": "es5",
|
||||
"singleQuote": true,
|
||||
"printWidth": 80,
|
||||
"tabWidth": 2,
|
||||
"useTabs": false
|
||||
}
|
||||
26
LICENSE
Normal file
26
LICENSE
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
|
||||
|
||||
Copyright (c) 2025 Collaborative Pixel Art Contributors
|
||||
|
||||
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
|
||||
|
||||
You are free to:
|
||||
- Share — copy and redistribute the material in any medium or format
|
||||
- Adapt — remix, transform, and build upon the material
|
||||
|
||||
Under the following terms:
|
||||
- Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
|
||||
- NonCommercial — You may not use the material for commercial purposes.
|
||||
- ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
|
||||
|
||||
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
319
MODERNIZATION_PLAN.md
Normal file
319
MODERNIZATION_PLAN.md
Normal file
|
|
@ -0,0 +1,319 @@
|
|||
# 🎨 Collaborative Pixel Art - Complete Modernization Plan
|
||||
|
||||
## 📊 Current State Analysis
|
||||
|
||||
### Existing Architecture
|
||||
- **Backend**: Basic Node.js + Express + Socket.IO
|
||||
- **Frontend**: Vanilla HTML/CSS/JS with DOM manipulation
|
||||
- **Storage**: Simple JSON file persistence
|
||||
- **Grid Size**: Fixed 200x200 pixels (40,000 DOM elements)
|
||||
- **Performance**: Severe browser lag, poor scalability
|
||||
|
||||
### Critical Issues Identified
|
||||
1. **DOM Performance**: 40,000 pixel divs cause severe rendering lag
|
||||
2. **Storage Inefficiency**: JSON file I/O on every pixel change
|
||||
3. **Network Overhead**: Unoptimized Socket.IO messages
|
||||
4. **No Scalability**: Fixed grid size, no chunking
|
||||
5. **Poor UX**: No mobile optimization, basic UI
|
||||
6. **No Security**: No rate limiting or user management
|
||||
7. **Legacy Code**: No TypeScript, modern patterns, or testing
|
||||
|
||||
## 🚀 Complete Modernization Strategy
|
||||
|
||||
### **PHASE 1: Backend Infrastructure Overhaul**
|
||||
|
||||
#### 1.1 TypeScript Migration & Modern Node.js
|
||||
- [ ] Upgrade to Node.js 18+ with native ES modules
|
||||
- [ ] Convert entire backend to TypeScript with strict mode
|
||||
- [ ] Implement modern async/await patterns
|
||||
- [ ] Add comprehensive type definitions
|
||||
- [ ] Setup ESLint + Prettier with strict rules
|
||||
|
||||
#### 1.2 Database & Storage Revolution
|
||||
- [ ] **Redis Implementation**
|
||||
- Spatial indexing for pixel coordinates
|
||||
- Hash maps for efficient pixel storage
|
||||
- Pub/Sub for real-time updates
|
||||
- Connection pooling and clustering
|
||||
- [ ] **PostgreSQL Integration**
|
||||
- User authentication and sessions
|
||||
- Canvas metadata and permissions
|
||||
- Pixel history and versioning
|
||||
- Analytics and usage tracking
|
||||
|
||||
#### 1.3 WebSocket Protocol Optimization
|
||||
- [ ] **Binary Protocol Implementation**
|
||||
- Custom binary message format
|
||||
- 90% reduction in network traffic
|
||||
- Batch pixel updates
|
||||
- Delta compression for changes
|
||||
- [ ] **Connection Management**
|
||||
- WebSocket heartbeat system
|
||||
- Automatic reconnection logic
|
||||
- Connection pooling
|
||||
- Room-based canvas isolation
|
||||
|
||||
#### 1.4 Security & Performance
|
||||
- [ ] **Rate Limiting System**
|
||||
- Per-user pixel placement cooldowns
|
||||
- IP-based rate limiting
|
||||
- DDoS protection middleware
|
||||
- Abuse detection algorithms
|
||||
- [ ] **Authentication Framework**
|
||||
- JWT-based session management
|
||||
- OAuth integration (Google, GitHub)
|
||||
- Guest user support
|
||||
- Role-based permissions
|
||||
|
||||
### **PHASE 2: Frontend Revolution**
|
||||
|
||||
#### 2.1 Next.js 14 + React 18 Migration
|
||||
- [ ] **Project Setup**
|
||||
- Next.js 14 with App Router
|
||||
- React 18 with Concurrent Features
|
||||
- TypeScript configuration
|
||||
- Tailwind CSS integration
|
||||
- [ ] **Server Components**
|
||||
- Static canvas metadata loading
|
||||
- SEO optimization
|
||||
- Performance improvements
|
||||
|
||||
#### 2.2 Canvas Virtualization Engine
|
||||
- [ ] **Virtual Canvas Implementation**
|
||||
- Viewport-based rendering (only visible pixels)
|
||||
- Infinite scrolling support
|
||||
- Zoom levels with dynamic detail
|
||||
- Memory-efficient pixel management
|
||||
- [ ] **WebGL Acceleration**
|
||||
- Hardware-accelerated rendering
|
||||
- Shader-based pixel drawing
|
||||
- Smooth zoom and pan
|
||||
- 60 FPS performance target
|
||||
|
||||
#### 2.3 Advanced Canvas Features
|
||||
- [ ] **Chunking System**
|
||||
- 64x64 pixel chunks for massive canvases
|
||||
- Lazy loading and unloading
|
||||
- Predictive chunk preloading
|
||||
- Efficient memory management
|
||||
- [ ] **Multi-layer Support**
|
||||
- Background and foreground layers
|
||||
- Layer blending modes
|
||||
- Individual layer opacity
|
||||
- Layer management UI
|
||||
|
||||
#### 2.4 Drawing Tools & Interaction
|
||||
- [ ] **Advanced Drawing Tools**
|
||||
- Brush tool with size/opacity
|
||||
- Fill bucket with flood fill algorithm
|
||||
- Line and shape tools
|
||||
- Eyedropper color picker
|
||||
- Copy/paste functionality
|
||||
- [ ] **Touch & Mobile Support**
|
||||
- Pinch-to-zoom gestures
|
||||
- Touch-optimized UI
|
||||
- Mobile color picker
|
||||
- Responsive breakpoints
|
||||
|
||||
### **PHASE 3: Real-time Collaboration**
|
||||
|
||||
#### 3.1 Live User Presence
|
||||
- [ ] **User Cursors**
|
||||
- Real-time cursor tracking
|
||||
- User identification and colors
|
||||
- Smooth cursor interpolation
|
||||
- Cursor state management
|
||||
- [ ] **User Awareness**
|
||||
- Active user count display
|
||||
- User list with avatars
|
||||
- Currently editing indicators
|
||||
- User activity feed
|
||||
|
||||
#### 3.2 Collaboration Features
|
||||
- [ ] **Pixel History System**
|
||||
- Complete undo/redo functionality
|
||||
- Branching timeline support
|
||||
- Conflict resolution algorithms
|
||||
- History compression
|
||||
- [ ] **Real-time Sync**
|
||||
- Operational Transform for consistency
|
||||
- Conflict-free replicated data types
|
||||
- Automatic conflict resolution
|
||||
- Offline support with sync
|
||||
|
||||
### **PHASE 4: Modern UI/UX Design**
|
||||
|
||||
#### 4.1 Design System
|
||||
- [ ] **Tailwind CSS + Design Tokens**
|
||||
- Consistent color palette
|
||||
- Typography scale
|
||||
- Spacing and layout system
|
||||
- Component library
|
||||
- [ ] **Dark Mode Implementation**
|
||||
- System preference detection
|
||||
- Manual toggle option
|
||||
- Smooth theme transitions
|
||||
- Persistent user preference
|
||||
|
||||
#### 4.2 Responsive & Accessibility
|
||||
- [ ] **Mobile-First Design**
|
||||
- Responsive grid layouts
|
||||
- Touch-friendly interactions
|
||||
- Mobile navigation patterns
|
||||
- Progressive enhancement
|
||||
- [ ] **Accessibility Features**
|
||||
- Screen reader support
|
||||
- Keyboard navigation
|
||||
- High contrast mode
|
||||
- Focus management
|
||||
|
||||
#### 4.3 Animations & Micro-interactions
|
||||
- [ ] **Framer Motion Integration**
|
||||
- Smooth page transitions
|
||||
- Loading animations
|
||||
- Gesture-based interactions
|
||||
- Performance-optimized animations
|
||||
|
||||
### **PHASE 5: Advanced Features**
|
||||
|
||||
#### 5.1 Canvas Management
|
||||
- [ ] **Import/Export System**
|
||||
- PNG/JPEG export functionality
|
||||
- SVG vector export
|
||||
- Canvas sharing via URLs
|
||||
- Template gallery
|
||||
- [ ] **Canvas Versioning**
|
||||
- Snapshot system
|
||||
- Version comparison
|
||||
- Rollback functionality
|
||||
- Branch management
|
||||
|
||||
#### 5.2 Social Features
|
||||
- [ ] **Community Features**
|
||||
- Canvas galleries
|
||||
- User profiles
|
||||
- Voting and favorites
|
||||
- Comments and discussions
|
||||
- [ ] **Collaboration Tools**
|
||||
- Private/public canvases
|
||||
- Invitation system
|
||||
- Permissions management
|
||||
- Team workspaces
|
||||
|
||||
### **PHASE 6: Production Infrastructure**
|
||||
|
||||
#### 6.1 Containerization & Deployment
|
||||
- [ ] **Docker Implementation**
|
||||
- Multi-stage build optimization
|
||||
- Development and production containers
|
||||
- Docker Compose for local development
|
||||
- Security hardening
|
||||
- [ ] **Kubernetes Orchestration**
|
||||
- Auto-scaling configuration
|
||||
- Load balancing setup
|
||||
- Health checks and monitoring
|
||||
- Rolling deployment strategy
|
||||
|
||||
#### 6.2 Monitoring & Observability
|
||||
- [ ] **Performance Monitoring**
|
||||
- Prometheus metrics collection
|
||||
- Grafana dashboards
|
||||
- Application performance monitoring
|
||||
- Real-time alerting system
|
||||
- [ ] **Error Tracking**
|
||||
- Comprehensive error logging
|
||||
- Error aggregation and analysis
|
||||
- Performance bottleneck identification
|
||||
- User experience monitoring
|
||||
|
||||
#### 6.3 Testing Strategy
|
||||
- [ ] **Comprehensive Test Suite**
|
||||
- Unit tests with Jest
|
||||
- Integration tests for APIs
|
||||
- E2E tests with Playwright
|
||||
- Performance benchmarking
|
||||
- [ ] **Continuous Integration**
|
||||
- GitHub Actions workflows
|
||||
- Automated testing pipeline
|
||||
- Code quality checks
|
||||
- Dependency security scanning
|
||||
|
||||
### **PHASE 7: Performance Optimization**
|
||||
|
||||
#### 7.1 Caching Strategy
|
||||
- [ ] **Multi-level Caching**
|
||||
- Redis for hot data
|
||||
- CDN for static assets
|
||||
- Browser caching optimization
|
||||
- Cache invalidation strategies
|
||||
|
||||
#### 7.2 Performance Targets
|
||||
- [ ] **Scalability Goals**
|
||||
- Support 10,000 x 10,000+ pixel canvases
|
||||
- Handle 1000+ concurrent users
|
||||
- <2 second load times
|
||||
- <100MB memory usage
|
||||
- <1KB per pixel operation
|
||||
|
||||
## 🔧 Technical Stack
|
||||
|
||||
### Backend Technologies
|
||||
- **Runtime**: Node.js 18+ with ES modules
|
||||
- **Framework**: Express.js with TypeScript
|
||||
- **Real-time**: Socket.IO with binary protocol
|
||||
- **Database**: PostgreSQL + Redis
|
||||
- **Authentication**: JWT + OAuth
|
||||
- **Testing**: Jest + Supertest
|
||||
|
||||
### Frontend Technologies
|
||||
- **Framework**: Next.js 14 + React 18
|
||||
- **Styling**: Tailwind CSS + Framer Motion
|
||||
- **State Management**: Zustand + React Query
|
||||
- **Canvas**: WebGL + Canvas API
|
||||
- **Testing**: Jest + React Testing Library + Playwright
|
||||
|
||||
### Infrastructure
|
||||
- **Containerization**: Docker + Kubernetes
|
||||
- **Monitoring**: Prometheus + Grafana
|
||||
- **CI/CD**: GitHub Actions
|
||||
- **Cloud**: AWS/GCP/Azure compatible
|
||||
|
||||
## 📈 Implementation Timeline
|
||||
|
||||
### Week 1-2: Foundation
|
||||
- Backend TypeScript migration
|
||||
- Database setup and modeling
|
||||
- Basic Next.js frontend setup
|
||||
|
||||
### Week 3-4: Core Features
|
||||
- Virtual canvas implementation
|
||||
- WebSocket optimization
|
||||
- Basic drawing tools
|
||||
|
||||
### Week 5-6: Collaboration
|
||||
- Real-time features
|
||||
- User presence system
|
||||
- Pixel history implementation
|
||||
|
||||
### Week 7-8: Polish & Production
|
||||
- UI/UX refinements
|
||||
- Performance optimization
|
||||
- Testing and deployment
|
||||
|
||||
## 🎯 Success Metrics
|
||||
|
||||
### Performance KPIs
|
||||
- **Canvas Size**: 10,000+ x 10,000+ pixels supported
|
||||
- **Concurrent Users**: 1000+ simultaneous users
|
||||
- **Load Time**: <2 seconds for any viewport
|
||||
- **Memory Usage**: <100MB for largest canvases
|
||||
- **Network Efficiency**: <1KB per pixel operation
|
||||
- **Frame Rate**: 60 FPS during interactions
|
||||
|
||||
### User Experience KPIs
|
||||
- **Mobile Usability**: Touch-optimized interface
|
||||
- **Accessibility**: WCAG 2.1 AA compliance
|
||||
- **Cross-browser**: 99%+ compatibility
|
||||
- **Offline Support**: Basic functionality without connection
|
||||
|
||||
This comprehensive plan transforms a basic pixel art application into a professional, scalable, and high-performance collaborative platform that can compete with modern web applications.
|
||||
263
README.md
263
README.md
|
|
@ -1,44 +1,253 @@
|
|||
# Collaborative Pixel Art
|
||||
# 🎨 GaPlace - Modern Collaborative Pixel Art Platform
|
||||
|
||||
Collaborative Pixel Art is a web application that allows multiple users to collaboratively create pixel art on a shared canvas in real-time.
|
||||
GaPlace is a high-performance, real-time collaborative pixel art platform built with modern web technologies. Create pixel art together with thousands of users on an infinite canvas with advanced features like real-time cursors, chunked loading, and optimized rendering.
|
||||
|
||||
## Setup Instructions
|
||||
## ✨ Features
|
||||
|
||||
To set up the project, follow these steps:
|
||||
### 🚀 Performance
|
||||
- **Virtual Canvas**: Only renders visible pixels for massive performance gains
|
||||
- **Chunked Loading**: Loads canvas data in 64x64 pixel chunks on-demand
|
||||
- **WebGL Acceleration**: Hardware-accelerated rendering for smooth interactions
|
||||
- **Infinite Canvas**: Support for canvases up to 10,000 x 10,000 pixels
|
||||
- **60 FPS**: Smooth animations and interactions
|
||||
|
||||
1. **Clone the repository:**
|
||||
### 🤝 Real-time Collaboration
|
||||
- **Live Cursors**: See other users' cursors in real-time
|
||||
- **User Presence**: Know who's online and active
|
||||
- **Instant Updates**: See pixel changes as they happen
|
||||
- **Rate Limiting**: Smart rate limiting to prevent spam
|
||||
|
||||
### 🎨 Advanced Drawing Tools
|
||||
- **Pixel Tool**: Classic pixel-by-pixel drawing
|
||||
- **Fill Tool**: Flood fill for large areas
|
||||
- **Eyedropper**: Pick colors from existing pixels
|
||||
- **Color Palette**: 21 predefined colors + custom color picker
|
||||
- **Zoom & Pan**: Smooth navigation with mouse and keyboard
|
||||
|
||||
### 🌙 Modern UI/UX
|
||||
- **Dark Mode**: System preference detection + manual toggle
|
||||
- **Responsive Design**: Works on desktop, tablet, and mobile
|
||||
- **Accessibility**: Screen reader support and keyboard navigation
|
||||
- **Smooth Animations**: Framer Motion powered interactions
|
||||
|
||||
### 🔒 Enterprise Ready
|
||||
- **TypeScript**: Full type safety across the stack
|
||||
- **Redis**: High-performance data storage and caching
|
||||
- **PostgreSQL**: Reliable user and canvas metadata storage
|
||||
- **Docker**: Containerized deployment
|
||||
- **Health Checks**: Comprehensive monitoring and alerting
|
||||
|
||||
## 🏗️ Architecture
|
||||
|
||||
### Backend Stack
|
||||
- **Node.js 18+** with TypeScript
|
||||
- **Express.js** for HTTP API
|
||||
- **Socket.IO** for real-time WebSocket communication
|
||||
- **Redis** for pixel data and caching
|
||||
- **PostgreSQL** for user data and canvas metadata
|
||||
- **JWT** authentication
|
||||
|
||||
### Frontend Stack
|
||||
- **Next.js 15.5** with App Router
|
||||
- **React 19** with Concurrent Features
|
||||
- **TypeScript** for type safety
|
||||
- **Tailwind CSS** for styling
|
||||
- **Framer Motion** for animations
|
||||
- **Zustand** for state management
|
||||
- **React Query** for server state
|
||||
|
||||
### Key Performance Optimizations
|
||||
- **Binary WebSocket Protocol**: 90% reduction in network traffic
|
||||
- **Canvas Virtualization**: Only render visible viewport
|
||||
- **Spatial Indexing**: Redis-based chunk organization
|
||||
- **Connection Pooling**: Optimized database connections
|
||||
- **Compression**: Gzip compression for all responses
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
### Prerequisites
|
||||
- Node.js 18+ and npm
|
||||
- Docker and Docker Compose
|
||||
- Git
|
||||
|
||||
### Development Setup
|
||||
|
||||
1. **Clone the repository**
|
||||
```bash
|
||||
git clone https://github.com/elektricm/collaborative-pixel-art.git
|
||||
```
|
||||
git clone <repository-url>
|
||||
cd collaborative-pixel-art
|
||||
```
|
||||
|
||||
2. **Install Dependencies:**
|
||||
2. **Install dependencies**
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
Make sure you have Node.js and npm (Node Package Manager) installed on your system. Then, install the project dependencies by running in the project folder:
|
||||
3. **Start development servers (Easy Mode)**
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
**OR manually:**
|
||||
```bash
|
||||
node start-dev.js
|
||||
```
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
4. **Open the application**
|
||||
- Frontend: http://localhost:3000
|
||||
- Backend API: http://localhost:3001
|
||||
- Health Check: http://localhost:3001/health
|
||||
|
||||
3. **Start the server:**
|
||||
**Note**: The application runs in development mode with mock databases by default (no Docker required). To use real Redis/PostgreSQL, see the Docker setup section below.
|
||||
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
### Full Docker Setup
|
||||
|
||||
or
|
||||
```bash
|
||||
# Start all services with Docker
|
||||
docker-compose up -d
|
||||
|
||||
```bash
|
||||
node server.js
|
||||
```
|
||||
# View logs
|
||||
docker-compose logs -f
|
||||
|
||||
4. **Open the application in your browser:**
|
||||
# Stop services
|
||||
docker-compose down
|
||||
```
|
||||
|
||||
Open the following URL in your browser: [http://localhost:3000](http://localhost:3000)
|
||||
## 📁 Project Structure
|
||||
|
||||
## Features
|
||||
```
|
||||
gaplace/
|
||||
├── backend/ # Node.js + TypeScript backend
|
||||
│ ├── src/
|
||||
│ │ ├── services/ # Business logic services
|
||||
│ │ ├── config/ # Database and environment config
|
||||
│ │ ├── middleware/ # Express middleware
|
||||
│ │ └── server.ts # Main server file
|
||||
│ ├── Dockerfile
|
||||
│ └── package.json
|
||||
│
|
||||
├── frontend/ # Next.js + React frontend
|
||||
│ ├── src/
|
||||
│ │ ├── app/ # Next.js app router
|
||||
│ │ ├── components/ # React components
|
||||
│ │ ├── hooks/ # Custom React hooks
|
||||
│ │ ├── store/ # Zustand state management
|
||||
│ │ └── styles/ # Global styles
|
||||
│ ├── Dockerfile
|
||||
│ └── package.json
|
||||
│
|
||||
├── shared/ # Shared TypeScript types and utilities
|
||||
│ ├── src/
|
||||
│ │ ├── types/ # Shared type definitions
|
||||
│ │ ├── constants/ # Shared constants
|
||||
│ │ └── utils/ # Shared utility functions
|
||||
│ └── package.json
|
||||
│
|
||||
├── docs/ # Documentation
|
||||
├── docker-compose.yml # Docker services
|
||||
└── MODERNIZATION_PLAN.md # Detailed technical plan
|
||||
```
|
||||
|
||||
Usage
|
||||
Each user can select a color from the color selector on the right side of the canvas.
|
||||
To place a pixel on the canvas, simply click on the desired grid cell using the selected color.
|
||||
All connected users will see the changes in real-time as pixels are placed or updated on the canvas.
|
||||
## 🎯 Performance Targets
|
||||
|
||||
- **Canvas Size**: 10,000+ x 10,000+ pixels supported
|
||||
- **Concurrent Users**: 1000+ simultaneous users
|
||||
- **Load Time**: <2 seconds for any viewport
|
||||
- **Memory Usage**: <100MB for largest canvases
|
||||
- **Network Traffic**: <1KB per pixel operation
|
||||
- **Frame Rate**: 60 FPS during interactions
|
||||
|
||||
## 🔧 Configuration
|
||||
|
||||
### Environment Variables
|
||||
|
||||
#### Backend (.env)
|
||||
```bash
|
||||
PORT=3001
|
||||
NODE_ENV=development
|
||||
JWT_SECRET=your-super-secret-key
|
||||
CORS_ORIGIN=http://localhost:3000
|
||||
|
||||
# Database
|
||||
REDIS_URL=redis://localhost:6379
|
||||
POSTGRES_HOST=localhost
|
||||
POSTGRES_PORT=5432
|
||||
POSTGRES_DB=gaplace
|
||||
POSTGRES_USER=gaplace
|
||||
POSTGRES_PASSWORD=password
|
||||
|
||||
# Rate Limiting
|
||||
RATE_LIMIT_PIXELS_PER_MINUTE=60
|
||||
RATE_LIMIT_PIXELS_PER_HOUR=1000
|
||||
```
|
||||
|
||||
#### Frontend (.env.local)
|
||||
```bash
|
||||
NEXT_PUBLIC_BACKEND_URL=http://localhost:3001
|
||||
```
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
npm run test
|
||||
|
||||
# Run tests in watch mode
|
||||
npm run test:watch
|
||||
|
||||
# Run specific workspace tests
|
||||
npm run test --workspace=backend
|
||||
npm run test --workspace=frontend
|
||||
```
|
||||
|
||||
## 📦 Deployment
|
||||
|
||||
### Production Build
|
||||
```bash
|
||||
# Build all packages
|
||||
npm run build
|
||||
|
||||
# Start production server
|
||||
npm start
|
||||
```
|
||||
|
||||
### Docker Production
|
||||
```bash
|
||||
# Build production images
|
||||
docker-compose -f docker-compose.prod.yml build
|
||||
|
||||
# Deploy to production
|
||||
docker-compose -f docker-compose.prod.yml up -d
|
||||
```
|
||||
|
||||
## 🤝 Contributing
|
||||
|
||||
1. Fork the repository
|
||||
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
||||
3. Make your changes
|
||||
4. Run tests (`npm run test`)
|
||||
5. Run linting (`npm run lint`)
|
||||
6. Commit your changes (`git commit -m 'Add amazing feature'`)
|
||||
7. Push to the branch (`git push origin feature/amazing-feature`)
|
||||
8. Open a Pull Request
|
||||
|
||||
## 📄 License
|
||||
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
|
||||
## 🙏 Acknowledgments
|
||||
|
||||
- Inspired by r/place and other collaborative art platforms
|
||||
- Built with modern web technologies and best practices
|
||||
- Optimized for performance and scalability
|
||||
|
||||
## 📚 Documentation
|
||||
|
||||
- [Technical Architecture](MODERNIZATION_PLAN.md)
|
||||
- [API Documentation](docs/api.md)
|
||||
- [Development Guide](docs/development.md)
|
||||
- [Deployment Guide](docs/deployment.md)
|
||||
|
||||
---
|
||||
|
||||
**GaPlace** - Where pixels meet collaboration 🎨✨
|
||||
30
backend/.env.example
Normal file
30
backend/.env.example
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Server Configuration
|
||||
PORT=3001
|
||||
NODE_ENV=development
|
||||
CORS_ORIGIN=http://localhost:3000
|
||||
|
||||
# Security
|
||||
JWT_SECRET=your-super-secret-jwt-key-change-in-production
|
||||
|
||||
# Database Configuration
|
||||
REDIS_URL=redis://localhost:6379
|
||||
REDIS_KEY_PREFIX=gaplace:
|
||||
|
||||
POSTGRES_HOST=localhost
|
||||
POSTGRES_PORT=5432
|
||||
POSTGRES_DB=gaplace
|
||||
POSTGRES_USER=gaplace
|
||||
POSTGRES_PASSWORD=password
|
||||
|
||||
# Rate Limiting
|
||||
RATE_LIMIT_PIXELS_PER_MINUTE=60
|
||||
RATE_LIMIT_PIXELS_PER_HOUR=1000
|
||||
RATE_LIMIT_CURSOR_PER_SECOND=10
|
||||
|
||||
# Canvas Configuration
|
||||
MAX_CANVAS_SIZE=10000
|
||||
DEFAULT_CANVAS_SIZE=1000
|
||||
CHUNK_SIZE=64
|
||||
|
||||
# Logging
|
||||
LOG_LEVEL=info
|
||||
9
backend/.eslintrc.json
Normal file
9
backend/.eslintrc.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"extends": ["../.eslintrc.json"],
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
}
|
||||
}
|
||||
26
backend/Dockerfile
Normal file
26
backend/Dockerfile
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
FROM node:18-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy package files
|
||||
COPY backend/package*.json ./
|
||||
COPY shared/ ../shared/
|
||||
|
||||
# Install dependencies
|
||||
RUN npm install
|
||||
|
||||
# Copy source code
|
||||
COPY backend/ .
|
||||
|
||||
# Build TypeScript
|
||||
RUN npm run build
|
||||
|
||||
# Expose port
|
||||
EXPOSE 3001
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget --no-verbose --tries=1 --spider http://localhost:3001/health || exit 1
|
||||
|
||||
# Start the application
|
||||
CMD ["npm", "start"]
|
||||
47
backend/package.json
Normal file
47
backend/package.json
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
{
|
||||
"name": "@gaplace/backend",
|
||||
"version": "1.0.0",
|
||||
"description": "GaPlace backend server with TypeScript, Redis, and WebSocket support",
|
||||
"main": "dist/server.js",
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/server.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/server.js",
|
||||
"clean": "rm -rf dist",
|
||||
"type-check": "tsc --noEmit",
|
||||
"lint": "eslint src --ext .ts",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@gaplace/shared": "file:../shared",
|
||||
"express": "^4.18.2",
|
||||
"socket.io": "^4.7.4",
|
||||
"redis": "^4.6.12",
|
||||
"cors": "^2.8.5",
|
||||
"helmet": "^7.1.0",
|
||||
"compression": "^1.7.4",
|
||||
"express-rate-limit": "^7.1.5",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"uuid": "^9.0.1",
|
||||
"pg": "^8.11.3",
|
||||
"dotenv": "^16.3.1",
|
||||
"winston": "^3.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/compression": "^1.7.5",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"@types/bcryptjs": "^2.4.6",
|
||||
"@types/uuid": "^9.0.7",
|
||||
"@types/pg": "^8.10.9",
|
||||
"@types/node": "^20.10.6",
|
||||
"@types/jest": "^29.5.11",
|
||||
"jest": "^29.7.0",
|
||||
"ts-jest": "^29.1.1",
|
||||
"tsx": "^4.7.0",
|
||||
"typescript": "^5.3.3"
|
||||
}
|
||||
}
|
||||
195
backend/src/config/database-dev.ts
Normal file
195
backend/src/config/database-dev.ts
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
// Development database configuration without external dependencies
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
// Mock Redis client for development
|
||||
class MockRedisClient extends EventEmitter {
|
||||
private storage = new Map<string, any>();
|
||||
private isConnected = false;
|
||||
|
||||
async connect() {
|
||||
this.isConnected = true;
|
||||
console.log('✅ Connected to Mock Redis (Development Mode)');
|
||||
this.emit('connect');
|
||||
return this;
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
this.isConnected = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
async set(key: string, value: string) {
|
||||
this.storage.set(key, value);
|
||||
return 'OK';
|
||||
}
|
||||
|
||||
async get(key: string) {
|
||||
return this.storage.get(key) || null;
|
||||
}
|
||||
|
||||
async incr(key: string) {
|
||||
const current = parseInt(this.storage.get(key) || '0');
|
||||
const newValue = current + 1;
|
||||
this.storage.set(key, newValue.toString());
|
||||
return newValue;
|
||||
}
|
||||
|
||||
async expire(key: string, seconds: number) {
|
||||
// In a real implementation, you'd set a timeout
|
||||
return 1;
|
||||
}
|
||||
|
||||
async hSet(key: string, field: string | Record<string, any>, value?: string) {
|
||||
if (typeof field === 'string' && value !== undefined) {
|
||||
let hash = this.storage.get(key);
|
||||
if (!hash || typeof hash !== 'object') {
|
||||
hash = {};
|
||||
}
|
||||
hash[field] = value;
|
||||
this.storage.set(key, hash);
|
||||
return 1;
|
||||
} else if (typeof field === 'object') {
|
||||
let hash = this.storage.get(key);
|
||||
if (!hash || typeof hash !== 'object') {
|
||||
hash = {};
|
||||
}
|
||||
Object.assign(hash, field);
|
||||
this.storage.set(key, hash);
|
||||
return Object.keys(field).length;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async hGetAll(key: string) {
|
||||
const hash = this.storage.get(key);
|
||||
return hash && typeof hash === 'object' ? hash : {};
|
||||
}
|
||||
|
||||
async sAdd(key: string, member: string) {
|
||||
let set = this.storage.get(key);
|
||||
if (!Array.isArray(set)) {
|
||||
set = [];
|
||||
}
|
||||
if (!set.includes(member)) {
|
||||
set.push(member);
|
||||
this.storage.set(key, set);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async sRem(key: string, member: string) {
|
||||
let set = this.storage.get(key);
|
||||
if (Array.isArray(set)) {
|
||||
const index = set.indexOf(member);
|
||||
if (index > -1) {
|
||||
set.splice(index, 1);
|
||||
this.storage.set(key, set);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async sMembers(key: string) {
|
||||
const set = this.storage.get(key);
|
||||
return Array.isArray(set) ? set : [];
|
||||
}
|
||||
|
||||
async sCard(key: string) {
|
||||
const set = this.storage.get(key);
|
||||
return Array.isArray(set) ? set.length : 0;
|
||||
}
|
||||
|
||||
async multi() {
|
||||
// Simplified mock - just return this for compatibility
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
// Mock PostgreSQL pool for development
|
||||
class MockPgPool extends EventEmitter {
|
||||
private isInitialized = false;
|
||||
|
||||
async connect() {
|
||||
if (!this.isInitialized) {
|
||||
console.log('✅ Connected to Mock PostgreSQL (Development Mode)');
|
||||
this.isInitialized = true;
|
||||
}
|
||||
return {
|
||||
query: async (sql: string, params?: any[]) => {
|
||||
// Mock responses for different queries
|
||||
if (sql.includes('CREATE TABLE')) {
|
||||
return { rows: [] };
|
||||
}
|
||||
if (sql.includes('INSERT INTO users')) {
|
||||
return {
|
||||
rows: [{
|
||||
id: 'mock-user-id',
|
||||
username: 'MockUser',
|
||||
email: 'mock@example.com',
|
||||
is_guest: true,
|
||||
created_at: new Date(),
|
||||
last_seen: new Date()
|
||||
}]
|
||||
};
|
||||
}
|
||||
if (sql.includes('SELECT') && sql.includes('users')) {
|
||||
return {
|
||||
rows: [{
|
||||
id: 'mock-user-id',
|
||||
username: 'MockUser',
|
||||
email: 'mock@example.com',
|
||||
is_guest: true,
|
||||
created_at: new Date(),
|
||||
last_seen: new Date()
|
||||
}]
|
||||
};
|
||||
}
|
||||
return { rows: [] };
|
||||
},
|
||||
release: () => {}
|
||||
};
|
||||
}
|
||||
|
||||
async query(sql: string, params?: any[]) {
|
||||
const client = await this.connect();
|
||||
const result = await client.query(sql, params);
|
||||
client.release();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const redisClient = new MockRedisClient() as any;
|
||||
export const pgPool = new MockPgPool() as any;
|
||||
|
||||
export async function initializeDatabase(): Promise<void> {
|
||||
try {
|
||||
console.log('🔌 Initializing development database (Mock)...');
|
||||
|
||||
// Connect to mock Redis
|
||||
await redisClient.connect();
|
||||
|
||||
// Test PostgreSQL connection
|
||||
const client = await pgPool.connect();
|
||||
console.log('✅ Connected to Mock PostgreSQL');
|
||||
client.release();
|
||||
|
||||
// Create mock tables
|
||||
await createTables();
|
||||
} catch (error) {
|
||||
console.error('❌ Database initialization failed:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function createTables(): Promise<void> {
|
||||
try {
|
||||
await pgPool.query(`CREATE TABLE IF NOT EXISTS users (...)`);
|
||||
await pgPool.query(`CREATE TABLE IF NOT EXISTS canvases (...)`);
|
||||
await pgPool.query(`CREATE TABLE IF NOT EXISTS user_sessions (...)`);
|
||||
console.log('✅ Database tables created/verified (Mock)');
|
||||
} catch (error) {
|
||||
console.log('✅ Mock tables setup complete');
|
||||
}
|
||||
}
|
||||
27
backend/src/config/database-factory.ts
Normal file
27
backend/src/config/database-factory.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Database factory that chooses between production and development database
|
||||
|
||||
// Check if we should use development mode (no Redis/PostgreSQL available)
|
||||
const useDevelopmentMode = process.env.NODE_ENV === 'development' &&
|
||||
(process.env.USE_MOCK_DB === 'true' || !process.env.REDIS_URL?.includes('://'));
|
||||
|
||||
let redisClient: any;
|
||||
let pgPool: any;
|
||||
let initializeDatabase: () => Promise<void>;
|
||||
|
||||
if (useDevelopmentMode) {
|
||||
// Use development mock
|
||||
const devDb = require('./database-dev');
|
||||
redisClient = devDb.redisClient;
|
||||
pgPool = devDb.pgPool;
|
||||
initializeDatabase = devDb.initializeDatabase;
|
||||
console.log('📦 Using development database (Mock)');
|
||||
} else {
|
||||
// Use production database
|
||||
const prodDb = require('./database');
|
||||
redisClient = prodDb.redisClient;
|
||||
pgPool = prodDb.pgPool;
|
||||
initializeDatabase = prodDb.initializeDatabase;
|
||||
console.log('🔥 Using production database');
|
||||
}
|
||||
|
||||
export { redisClient, pgPool, initializeDatabase };
|
||||
126
backend/src/config/database.ts
Normal file
126
backend/src/config/database.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import { createClient } from 'redis';
|
||||
import pkg from 'pg';
|
||||
const { Pool } = pkg;
|
||||
|
||||
export interface DatabaseConfig {
|
||||
redis: {
|
||||
url: string;
|
||||
maxRetriesPerRequest: number;
|
||||
};
|
||||
postgres: {
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
user: string;
|
||||
password: string;
|
||||
max: number;
|
||||
};
|
||||
}
|
||||
|
||||
export const databaseConfig: DatabaseConfig = {
|
||||
redis: {
|
||||
url: process.env.REDIS_URL || 'redis://localhost:6379',
|
||||
maxRetriesPerRequest: 3,
|
||||
},
|
||||
postgres: {
|
||||
host: process.env.POSTGRES_HOST || 'localhost',
|
||||
port: parseInt(process.env.POSTGRES_PORT || '5432'),
|
||||
database: process.env.POSTGRES_DB || 'gaplace',
|
||||
user: process.env.POSTGRES_USER || 'gaplace',
|
||||
password: process.env.POSTGRES_PASSWORD || 'password',
|
||||
max: 20,
|
||||
},
|
||||
};
|
||||
|
||||
// Redis client
|
||||
export const redisClient = createClient({
|
||||
url: databaseConfig.redis.url,
|
||||
});
|
||||
|
||||
redisClient.on('error', (err) => {
|
||||
console.error('Redis Client Error:', err);
|
||||
});
|
||||
|
||||
redisClient.on('connect', () => {
|
||||
console.log('✅ Connected to Redis');
|
||||
});
|
||||
|
||||
// PostgreSQL client
|
||||
export const pgPool = new Pool(databaseConfig.postgres);
|
||||
|
||||
pgPool.on('error', (err) => {
|
||||
console.error('PostgreSQL Pool Error:', err);
|
||||
});
|
||||
|
||||
export async function initializeDatabase(): Promise<void> {
|
||||
try {
|
||||
// Connect to Redis
|
||||
await redisClient.connect();
|
||||
|
||||
// Test PostgreSQL connection
|
||||
const client = await pgPool.connect();
|
||||
console.log('✅ Connected to PostgreSQL');
|
||||
client.release();
|
||||
|
||||
// Create tables if they don't exist
|
||||
await createTables();
|
||||
} catch (error) {
|
||||
console.error('❌ Database initialization failed:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function createTables(): Promise<void> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
email VARCHAR(255) UNIQUE,
|
||||
password_hash VARCHAR(255),
|
||||
avatar_url TEXT,
|
||||
is_guest BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
last_seen TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS canvases (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(100) NOT NULL,
|
||||
width INTEGER NOT NULL DEFAULT 1000,
|
||||
height INTEGER NOT NULL DEFAULT 1000,
|
||||
chunk_size INTEGER NOT NULL DEFAULT 64,
|
||||
is_public BOOLEAN DEFAULT TRUE,
|
||||
created_by UUID REFERENCES users(id),
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS user_sessions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID REFERENCES users(id),
|
||||
canvas_id UUID REFERENCES canvases(id),
|
||||
session_token VARCHAR(255) UNIQUE NOT NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
last_activity TIMESTAMP DEFAULT NOW(),
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_canvases_public ON canvases(is_public);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_active ON user_sessions(is_active, last_activity);
|
||||
`);
|
||||
|
||||
console.log('✅ Database tables created/verified');
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
43
backend/src/config/env.ts
Normal file
43
backend/src/config/env.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export const config = {
|
||||
port: parseInt(process.env.PORT || '3001'),
|
||||
host: process.env.HOST || 'localhost',
|
||||
nodeEnv: process.env.NODE_ENV || 'development',
|
||||
jwtSecret: process.env.JWT_SECRET || 'your-super-secret-jwt-key-change-in-production',
|
||||
corsOrigin: process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : (process.env.NODE_ENV === 'development' ? true : ['http://localhost:3000']),
|
||||
|
||||
// Rate limiting
|
||||
rateLimits: {
|
||||
pixelsPerMinute: parseInt(process.env.RATE_LIMIT_PIXELS_PER_MINUTE || '60'),
|
||||
pixelsPerHour: parseInt(process.env.RATE_LIMIT_PIXELS_PER_HOUR || '1000'),
|
||||
cursorUpdatesPerSecond: parseInt(process.env.RATE_LIMIT_CURSOR_PER_SECOND || '10'),
|
||||
},
|
||||
|
||||
// Canvas settings
|
||||
canvas: {
|
||||
maxSize: parseInt(process.env.MAX_CANVAS_SIZE || '10000'),
|
||||
defaultSize: parseInt(process.env.DEFAULT_CANVAS_SIZE || '1000'),
|
||||
chunkSize: parseInt(process.env.CHUNK_SIZE || '64'),
|
||||
},
|
||||
|
||||
// Redis settings
|
||||
redis: {
|
||||
url: process.env.REDIS_URL || 'redis://localhost:6379',
|
||||
keyPrefix: process.env.REDIS_KEY_PREFIX || 'gaplace:',
|
||||
},
|
||||
|
||||
// Logging
|
||||
logLevel: process.env.LOG_LEVEL || 'info',
|
||||
} as const;
|
||||
|
||||
export function validateConfig(): void {
|
||||
const required = ['JWT_SECRET'];
|
||||
const missing = required.filter(key => !process.env[key]);
|
||||
|
||||
if (missing.length > 0 && config.nodeEnv === 'production') {
|
||||
throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
|
||||
}
|
||||
}
|
||||
150
backend/src/server.ts
Normal file
150
backend/src/server.ts
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
import express from 'express';
|
||||
import { createServer } from 'http';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import compression from 'compression';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import { config, validateConfig } from './config/env';
|
||||
import { initializeDatabase } from './config/database-factory';
|
||||
import { WebSocketService } from './services/WebSocketService';
|
||||
|
||||
// Validate environment configuration
|
||||
validateConfig();
|
||||
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
|
||||
// Security middleware
|
||||
app.use(helmet({
|
||||
contentSecurityPolicy: {
|
||||
directives: {
|
||||
defaultSrc: ["'self'"],
|
||||
scriptSrc: ["'self'", "'unsafe-inline'"],
|
||||
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com"],
|
||||
fontSrc: ["'self'", "https://fonts.gstatic.com"],
|
||||
imgSrc: ["'self'", "data:", "https:"],
|
||||
connectSrc: ["'self'", "ws:", "wss:"],
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
// CORS configuration
|
||||
app.use(cors({
|
||||
origin: config.corsOrigin,
|
||||
credentials: true,
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||
allowedHeaders: ['Content-Type', 'Authorization'],
|
||||
}));
|
||||
|
||||
// Compression and parsing
|
||||
app.use(compression());
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Rate limiting
|
||||
const generalLimiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
max: 1000, // Limit each IP to 1000 requests per windowMs
|
||||
message: 'Too many requests from this IP, please try again later.',
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
});
|
||||
|
||||
app.use(generalLimiter);
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'healthy',
|
||||
timestamp: new Date().toISOString(),
|
||||
version: '2.0.0'
|
||||
});
|
||||
});
|
||||
|
||||
// API routes
|
||||
app.get('/api/canvas/:id/stats', async (req, res) => {
|
||||
try {
|
||||
// TODO: Implement canvas stats endpoint
|
||||
res.json({
|
||||
totalPixels: 0,
|
||||
activeUsers: 0,
|
||||
lastActivity: Date.now()
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching canvas stats:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Serve static files for development (in production, use a reverse proxy)
|
||||
if (config.nodeEnv === 'development') {
|
||||
app.use(express.static('public'));
|
||||
}
|
||||
|
||||
// Error handling middleware
|
||||
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
console.error('Unhandled error:', err);
|
||||
res.status(500).json({
|
||||
error: config.nodeEnv === 'development' ? err.message : 'Internal server error'
|
||||
});
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.use('*', (req, res) => {
|
||||
res.status(404).json({ error: 'Not found' });
|
||||
});
|
||||
|
||||
async function startServer() {
|
||||
try {
|
||||
// Initialize database connections
|
||||
console.log('🔌 Initializing database connections...');
|
||||
await initializeDatabase();
|
||||
|
||||
// Initialize WebSocket service
|
||||
console.log('🔌 Initializing WebSocket service...');
|
||||
const wsService = new WebSocketService(server);
|
||||
|
||||
// Start server
|
||||
server.listen(config.port, config.host, () => {
|
||||
console.log(`🚀 GaPlace server running on http://${config.host}:${config.port}`);
|
||||
console.log(`📁 Environment: ${config.nodeEnv}`);
|
||||
console.log(`🌐 CORS origins: ${Array.isArray(config.corsOrigin) ? config.corsOrigin.join(', ') : config.corsOrigin}`);
|
||||
console.log(`📊 Canvas max size: ${config.canvas.maxSize}x${config.canvas.maxSize}`);
|
||||
console.log(`⚡ Ready for connections!`);
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('📛 SIGTERM received, shutting down gracefully');
|
||||
server.close(() => {
|
||||
console.log('💤 Server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
console.log('📛 SIGINT received, shutting down gracefully');
|
||||
server.close(() => {
|
||||
console.log('💤 Server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle uncaught exceptions
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('❌ Uncaught Exception:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('❌ Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
startServer();
|
||||
167
backend/src/services/CanvasService.ts
Normal file
167
backend/src/services/CanvasService.ts
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
import { redisClient } from '../config/database-factory';
|
||||
import {
|
||||
Pixel,
|
||||
PixelChunk,
|
||||
getChunkCoordinates,
|
||||
getChunkKey,
|
||||
getPixelKey,
|
||||
CANVAS_CONFIG
|
||||
} from '@gaplace/shared';
|
||||
import { config } from '../config/env';
|
||||
|
||||
export class CanvasService {
|
||||
private readonly keyPrefix = config.redis.keyPrefix;
|
||||
|
||||
async placePixel(canvasId: string, x: number, y: number, color: string, userId: string, username?: string): Promise<boolean> {
|
||||
try {
|
||||
const { chunkX, chunkY } = getChunkCoordinates(x, y);
|
||||
const chunkKey = `${this.keyPrefix}canvas:${canvasId}:chunk:${getChunkKey(chunkX, chunkY)}`;
|
||||
const pixelKey = getPixelKey(x % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE, y % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
|
||||
// Store pixel with user information as JSON
|
||||
const pixelData = {
|
||||
color,
|
||||
userId,
|
||||
username: username || userId,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
await redisClient.hSet(chunkKey, pixelKey, JSON.stringify(pixelData));
|
||||
|
||||
// Update chunk metadata
|
||||
await redisClient.hSet(`${chunkKey}:meta`, {
|
||||
lastModified: Date.now().toString(),
|
||||
lastUser: userId,
|
||||
});
|
||||
|
||||
// Track user pixel count
|
||||
await redisClient.incr(`${this.keyPrefix}user:${userId}:pixels`);
|
||||
|
||||
// Update canvas stats
|
||||
await redisClient.incr(`${this.keyPrefix}canvas:${canvasId}:totalPixels`);
|
||||
await redisClient.set(`${this.keyPrefix}canvas:${canvasId}:lastActivity`, Date.now().toString());
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error placing pixel:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async getChunk(canvasId: string, chunkX: number, chunkY: number): Promise<PixelChunk | null> {
|
||||
try {
|
||||
const chunkKey = `${this.keyPrefix}canvas:${canvasId}:chunk:${getChunkKey(chunkX, chunkY)}`;
|
||||
const [pixelData, metadata] = await Promise.all([
|
||||
redisClient.hGetAll(chunkKey),
|
||||
redisClient.hGetAll(`${chunkKey}:meta`)
|
||||
]);
|
||||
|
||||
if (Object.keys(pixelData).length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pixels = new Map<string, any>();
|
||||
for (const [key, data] of Object.entries(pixelData)) {
|
||||
try {
|
||||
// Try to parse as JSON (new format with user info)
|
||||
const parsedData = JSON.parse(String(data));
|
||||
pixels.set(key, parsedData);
|
||||
} catch {
|
||||
// Fallback for old format (just color string)
|
||||
pixels.set(key, { color: String(data), userId: null, username: null, timestamp: 0 });
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
chunkX,
|
||||
chunkY,
|
||||
pixels,
|
||||
lastModified: parseInt(metadata.lastModified || '0'),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting chunk:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getVisibleChunks(canvasId: string, startX: number, startY: number, endX: number, endY: number): Promise<PixelChunk[]> {
|
||||
const chunks: PixelChunk[] = [];
|
||||
|
||||
const startChunkX = Math.floor(startX / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
const startChunkY = Math.floor(startY / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
const endChunkX = Math.floor(endX / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
const endChunkY = Math.floor(endY / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
|
||||
const promises: Promise<PixelChunk | null>[] = [];
|
||||
|
||||
for (let chunkX = startChunkX; chunkX <= endChunkX; chunkX++) {
|
||||
for (let chunkY = startChunkY; chunkY <= endChunkY; chunkY++) {
|
||||
promises.push(this.getChunk(canvasId, chunkX, chunkY));
|
||||
}
|
||||
}
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
|
||||
for (const chunk of results) {
|
||||
if (chunk) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
async getCanvasStats(canvasId: string): Promise<{
|
||||
totalPixels: number;
|
||||
lastActivity: number;
|
||||
activeUsers: number;
|
||||
}> {
|
||||
try {
|
||||
const [totalPixels, lastActivity, activeUsers] = await Promise.all([
|
||||
redisClient.get(`${this.keyPrefix}canvas:${canvasId}:totalPixels`),
|
||||
redisClient.get(`${this.keyPrefix}canvas:${canvasId}:lastActivity`),
|
||||
redisClient.sCard(`${this.keyPrefix}canvas:${canvasId}:activeUsers`)
|
||||
]);
|
||||
|
||||
return {
|
||||
totalPixels: parseInt(totalPixels || '0'),
|
||||
lastActivity: parseInt(lastActivity || '0'),
|
||||
activeUsers: activeUsers || 0,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting canvas stats:', error);
|
||||
return {
|
||||
totalPixels: 0,
|
||||
lastActivity: 0,
|
||||
activeUsers: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async addActiveUser(canvasId: string, userId: string): Promise<void> {
|
||||
try {
|
||||
await redisClient.sAdd(`${this.keyPrefix}canvas:${canvasId}:activeUsers`, userId);
|
||||
// Set expiration to auto-remove inactive users
|
||||
await redisClient.expire(`${this.keyPrefix}canvas:${canvasId}:activeUsers`, 300); // 5 minutes
|
||||
} catch (error) {
|
||||
console.error('Error adding active user:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async removeActiveUser(canvasId: string, userId: string): Promise<void> {
|
||||
try {
|
||||
await redisClient.sRem(`${this.keyPrefix}canvas:${canvasId}:activeUsers`, userId);
|
||||
} catch (error) {
|
||||
console.error('Error removing active user:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async getActiveUsers(canvasId: string): Promise<string[]> {
|
||||
try {
|
||||
return await redisClient.sMembers(`${this.keyPrefix}canvas:${canvasId}:activeUsers`);
|
||||
} catch (error) {
|
||||
console.error('Error getting active users:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
91
backend/src/services/RateLimitService.ts
Normal file
91
backend/src/services/RateLimitService.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import { redisClient } from '../config/database-factory';
|
||||
import { config } from '../config/env';
|
||||
|
||||
export class RateLimitService {
|
||||
private readonly keyPrefix = config.redis.keyPrefix;
|
||||
|
||||
async checkPixelRateLimit(userId: string): Promise<{ allowed: boolean; resetTime: number }> {
|
||||
const now = Date.now();
|
||||
const minuteKey = `${this.keyPrefix}ratelimit:pixel:${userId}:${Math.floor(now / 60000)}`;
|
||||
const hourKey = `${this.keyPrefix}ratelimit:pixel:${userId}:${Math.floor(now / 3600000)}`;
|
||||
|
||||
try {
|
||||
// Simple approach without pipeline for better compatibility
|
||||
const minuteCount = await redisClient.incr(minuteKey);
|
||||
await redisClient.expire(minuteKey, 60);
|
||||
|
||||
const hourCount = await redisClient.incr(hourKey);
|
||||
await redisClient.expire(hourKey, 3600);
|
||||
|
||||
const minuteExceeded = minuteCount > config.rateLimits.pixelsPerMinute;
|
||||
const hourExceeded = hourCount > config.rateLimits.pixelsPerHour;
|
||||
|
||||
if (minuteExceeded) {
|
||||
return {
|
||||
allowed: false,
|
||||
resetTime: Math.ceil(now / 60000) * 60000
|
||||
};
|
||||
}
|
||||
|
||||
if (hourExceeded) {
|
||||
return {
|
||||
allowed: false,
|
||||
resetTime: Math.ceil(now / 3600000) * 3600000
|
||||
};
|
||||
}
|
||||
|
||||
return { allowed: true, resetTime: 0 };
|
||||
} catch (error) {
|
||||
console.error('Rate limit check failed:', error);
|
||||
// Fail open - allow the request if Redis is down
|
||||
return { allowed: true, resetTime: 0 };
|
||||
}
|
||||
}
|
||||
|
||||
async checkCursorRateLimit(userId: string): Promise<boolean> {
|
||||
const now = Date.now();
|
||||
const secondKey = `${this.keyPrefix}ratelimit:cursor:${userId}:${Math.floor(now / 1000)}`;
|
||||
|
||||
try {
|
||||
const count = await redisClient.incr(secondKey);
|
||||
await redisClient.expire(secondKey, 1);
|
||||
|
||||
return count <= config.rateLimits.cursorUpdatesPerSecond;
|
||||
} catch (error) {
|
||||
console.error('Cursor rate limit check failed:', error);
|
||||
return true; // Fail open
|
||||
}
|
||||
}
|
||||
|
||||
async getUserPixelStats(userId: string): Promise<{
|
||||
totalPixels: number;
|
||||
pixelsThisHour: number;
|
||||
pixelsThisMinute: number;
|
||||
}> {
|
||||
const now = Date.now();
|
||||
const minuteKey = `${this.keyPrefix}ratelimit:pixel:${userId}:${Math.floor(now / 60000)}`;
|
||||
const hourKey = `${this.keyPrefix}ratelimit:pixel:${userId}:${Math.floor(now / 3600000)}`;
|
||||
const totalKey = `${this.keyPrefix}user:${userId}:pixels`;
|
||||
|
||||
try {
|
||||
const [totalPixels, pixelsThisHour, pixelsThisMinute] = await Promise.all([
|
||||
redisClient.get(totalKey),
|
||||
redisClient.get(hourKey),
|
||||
redisClient.get(minuteKey)
|
||||
]);
|
||||
|
||||
return {
|
||||
totalPixels: parseInt(totalPixels || '0'),
|
||||
pixelsThisHour: parseInt(pixelsThisHour || '0'),
|
||||
pixelsThisMinute: parseInt(pixelsThisMinute || '0'),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error getting user pixel stats:', error);
|
||||
return {
|
||||
totalPixels: 0,
|
||||
pixelsThisHour: 0,
|
||||
pixelsThisMinute: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
202
backend/src/services/UserService.ts
Normal file
202
backend/src/services/UserService.ts
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
import { pgPool } from '../config/database-factory';
|
||||
import { User } from '@gaplace/shared';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { config } from '../config/env';
|
||||
|
||||
export class UserService {
|
||||
async createGuestUser(): Promise<User> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
const guestId = uuidv4();
|
||||
const username = `Guest_${guestId.slice(0, 8)}`;
|
||||
|
||||
const result = await client.query(`
|
||||
INSERT INTO users (id, username, is_guest)
|
||||
VALUES ($1, $2, true)
|
||||
RETURNING *
|
||||
`, [guestId, username]);
|
||||
|
||||
const row = result.rows[0];
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
avatar: row.avatar_url,
|
||||
isGuest: true,
|
||||
createdAt: new Date(row.created_at).getTime(),
|
||||
lastSeen: new Date(row.last_seen).getTime(),
|
||||
};
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async createUser(username: string, email: string, password: string): Promise<User> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
// Check if username or email already exists
|
||||
const existingUser = await client.query(`
|
||||
SELECT id FROM users WHERE username = $1 OR email = $2
|
||||
`, [username, email]);
|
||||
|
||||
if (existingUser.rows.length > 0) {
|
||||
throw new Error('Username or email already exists');
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const passwordHash = await bcrypt.hash(password, 12);
|
||||
|
||||
const result = await client.query(`
|
||||
INSERT INTO users (username, email, password_hash, is_guest)
|
||||
VALUES ($1, $2, $3, false)
|
||||
RETURNING *
|
||||
`, [username, email, passwordHash]);
|
||||
|
||||
const row = result.rows[0];
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
avatar: row.avatar_url,
|
||||
isGuest: false,
|
||||
createdAt: new Date(row.created_at).getTime(),
|
||||
lastSeen: new Date(row.last_seen).getTime(),
|
||||
};
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async authenticateUser(usernameOrEmail: string, password: string): Promise<User | null> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
const result = await client.query(`
|
||||
SELECT * FROM users
|
||||
WHERE (username = $1 OR email = $1) AND is_guest = false
|
||||
`, [usernameOrEmail]);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const row = result.rows[0];
|
||||
const isValid = await bcrypt.compare(password, row.password_hash);
|
||||
|
||||
if (!isValid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Update last seen
|
||||
await client.query(`
|
||||
UPDATE users SET last_seen = NOW() WHERE id = $1
|
||||
`, [row.id]);
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
avatar: row.avatar_url,
|
||||
isGuest: false,
|
||||
createdAt: new Date(row.created_at).getTime(),
|
||||
lastSeen: Date.now(),
|
||||
};
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async getUser(userId: string): Promise<User | null> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
const result = await client.query(`
|
||||
SELECT * FROM users WHERE id = $1
|
||||
`, [userId]);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const row = result.rows[0];
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
email: row.email,
|
||||
avatar: row.avatar_url,
|
||||
isGuest: row.is_guest,
|
||||
createdAt: new Date(row.created_at).getTime(),
|
||||
lastSeen: new Date(row.last_seen).getTime(),
|
||||
};
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async updateUserLastSeen(userId: string): Promise<void> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
await client.query(`
|
||||
UPDATE users SET last_seen = NOW() WHERE id = $1
|
||||
`, [userId]);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
generateJWT(user: User): string {
|
||||
return jwt.sign(
|
||||
{
|
||||
userId: user.id,
|
||||
username: user.username,
|
||||
isGuest: user.isGuest
|
||||
},
|
||||
config.jwtSecret,
|
||||
{ expiresIn: '7d' }
|
||||
);
|
||||
}
|
||||
|
||||
verifyJWT(token: string): { userId: string; username: string; isGuest: boolean } | null {
|
||||
try {
|
||||
return jwt.verify(token, config.jwtSecret) as any;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getUserStats(userId: string): Promise<{
|
||||
totalPixels: number;
|
||||
joinedCanvases: number;
|
||||
accountAge: number;
|
||||
}> {
|
||||
const client = await pgPool.connect();
|
||||
|
||||
try {
|
||||
const [userResult, canvasResult] = await Promise.all([
|
||||
client.query(`
|
||||
SELECT created_at FROM users WHERE id = $1
|
||||
`, [userId]),
|
||||
client.query(`
|
||||
SELECT COUNT(DISTINCT canvas_id) as canvas_count
|
||||
FROM user_sessions WHERE user_id = $1
|
||||
`, [userId])
|
||||
]);
|
||||
|
||||
const user = userResult.rows[0];
|
||||
const canvasCount = canvasResult.rows[0]?.canvas_count || 0;
|
||||
|
||||
return {
|
||||
totalPixels: 0, // Will be fetched from Redis via RateLimitService
|
||||
joinedCanvases: parseInt(canvasCount),
|
||||
accountAge: user ? Date.now() - new Date(user.created_at).getTime() : 0,
|
||||
};
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
271
backend/src/services/WebSocketService.ts
Normal file
271
backend/src/services/WebSocketService.ts
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
import { Server as SocketIOServer, Socket } from 'socket.io';
|
||||
import { Server as HTTPServer } from 'http';
|
||||
import {
|
||||
WebSocketMessage,
|
||||
MessageType,
|
||||
PlacePixelMessage,
|
||||
LoadChunkMessage,
|
||||
CursorMoveMessage,
|
||||
UserPresence,
|
||||
isValidColor
|
||||
} from '@gaplace/shared';
|
||||
import { CanvasService } from './CanvasService';
|
||||
import { RateLimitService } from './RateLimitService';
|
||||
import { UserService } from './UserService';
|
||||
import { config } from '../config/env';
|
||||
|
||||
export class WebSocketService {
|
||||
private io: SocketIOServer;
|
||||
private canvasService: CanvasService;
|
||||
private rateLimitService: RateLimitService;
|
||||
private userService: UserService;
|
||||
private userPresence = new Map<string, UserPresence>();
|
||||
|
||||
constructor(server: HTTPServer) {
|
||||
this.io = new SocketIOServer(server, {
|
||||
cors: {
|
||||
origin: config.corsOrigin,
|
||||
methods: ['GET', 'POST'],
|
||||
credentials: true
|
||||
},
|
||||
// Enable binary support for better performance
|
||||
parser: undefined, // Use default parser for now, can optimize later
|
||||
transports: ['polling', 'websocket'], // Start with polling for better compatibility
|
||||
allowEIO3: true, // Allow Engine.IO v3 compatibility
|
||||
pingTimeout: 60000,
|
||||
pingInterval: 25000,
|
||||
});
|
||||
|
||||
this.canvasService = new CanvasService();
|
||||
this.rateLimitService = new RateLimitService();
|
||||
this.userService = new UserService();
|
||||
|
||||
this.setupEventHandlers();
|
||||
}
|
||||
|
||||
private setupEventHandlers(): void {
|
||||
this.io.on('connection', (socket: Socket) => {
|
||||
console.log(`User connected: ${socket.id}`);
|
||||
|
||||
// Handle user authentication/identification
|
||||
socket.on('auth', async (data: { userId?: string; canvasId: string; username?: string }) => {
|
||||
try {
|
||||
let userId = data.userId;
|
||||
|
||||
// Create guest user if no userId provided
|
||||
if (!userId) {
|
||||
const guestUser = await this.userService.createGuestUser();
|
||||
userId = guestUser.id;
|
||||
}
|
||||
|
||||
// Store user info in socket
|
||||
socket.data.userId = userId;
|
||||
socket.data.canvasId = data.canvasId;
|
||||
socket.data.username = data.username || `Guest-${userId?.slice(-4)}`;
|
||||
|
||||
// Join canvas room
|
||||
await socket.join(`canvas:${data.canvasId}`);
|
||||
|
||||
// Add to active users
|
||||
await this.canvasService.addActiveUser(data.canvasId, userId);
|
||||
|
||||
// Send canvas info
|
||||
const stats = await this.canvasService.getCanvasStats(data.canvasId);
|
||||
socket.emit('canvas_info', stats);
|
||||
|
||||
// Send current user list
|
||||
const activeUsers = await this.canvasService.getActiveUsers(data.canvasId);
|
||||
this.io.to(`canvas:${data.canvasId}`).emit('user_list', activeUsers);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Auth error:', error);
|
||||
socket.emit('error', { message: 'Authentication failed' });
|
||||
}
|
||||
});
|
||||
|
||||
// Handle pixel placement
|
||||
socket.on('place_pixel', async (message: PlacePixelMessage) => {
|
||||
await this.handlePlacePixel(socket, message);
|
||||
});
|
||||
|
||||
// Handle chunk loading
|
||||
socket.on('load_chunk', async (message: LoadChunkMessage) => {
|
||||
await this.handleLoadChunk(socket, message);
|
||||
});
|
||||
|
||||
// Handle cursor movement
|
||||
socket.on('cursor_move', async (message: CursorMoveMessage) => {
|
||||
await this.handleCursorMove(socket, message);
|
||||
});
|
||||
|
||||
// Handle disconnect
|
||||
socket.on('disconnect', async () => {
|
||||
console.log(`User disconnected: ${socket.id}`);
|
||||
|
||||
if (socket.data.userId && socket.data.canvasId) {
|
||||
await this.canvasService.removeActiveUser(socket.data.canvasId, socket.data.userId);
|
||||
|
||||
// Remove from presence tracking
|
||||
this.userPresence.delete(socket.data.userId);
|
||||
|
||||
// Notify others
|
||||
const activeUsers = await this.canvasService.getActiveUsers(socket.data.canvasId);
|
||||
this.io.to(`canvas:${socket.data.canvasId}`).emit('user_list', activeUsers);
|
||||
}
|
||||
});
|
||||
|
||||
// Heartbeat for connection monitoring
|
||||
socket.on('heartbeat', () => {
|
||||
socket.emit('heartbeat_ack', { timestamp: Date.now() });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async handlePlacePixel(socket: Socket, message: PlacePixelMessage): Promise<void> {
|
||||
const { userId, canvasId } = socket.data;
|
||||
|
||||
if (!userId || !canvasId) {
|
||||
socket.emit('error', { message: 'Not authenticated' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate input
|
||||
if (!isValidColor(message.color)) {
|
||||
socket.emit('error', { message: 'Invalid color format' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Check rate limit
|
||||
const rateLimit = await this.rateLimitService.checkPixelRateLimit(userId);
|
||||
if (!rateLimit.allowed) {
|
||||
socket.emit('rate_limited', {
|
||||
message: 'Rate limit exceeded',
|
||||
resetTime: rateLimit.resetTime
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Place pixel
|
||||
const success = await this.canvasService.placePixel(
|
||||
canvasId,
|
||||
message.x,
|
||||
message.y,
|
||||
message.color,
|
||||
userId,
|
||||
socket.data.username
|
||||
);
|
||||
|
||||
if (success) {
|
||||
// Broadcast to all users in the canvas
|
||||
this.io.to(`canvas:${canvasId}`).emit('pixel_placed', {
|
||||
type: MessageType.PIXEL_PLACED,
|
||||
x: message.x,
|
||||
y: message.y,
|
||||
color: message.color,
|
||||
userId,
|
||||
username: socket.data.username,
|
||||
canvasId,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
// Send updated stats
|
||||
const stats = await this.canvasService.getCanvasStats(canvasId);
|
||||
this.io.to(`canvas:${canvasId}`).emit('canvas_updated', stats);
|
||||
} else {
|
||||
socket.emit('error', { message: 'Failed to place pixel' });
|
||||
}
|
||||
}
|
||||
|
||||
private async handleLoadChunk(socket: Socket, message: LoadChunkMessage): Promise<void> {
|
||||
const { canvasId } = socket.data;
|
||||
|
||||
if (!canvasId) {
|
||||
socket.emit('error', { message: 'Not authenticated' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const chunk = await this.canvasService.getChunk(canvasId, message.chunkX, message.chunkY);
|
||||
|
||||
if (chunk) {
|
||||
const pixels = Array.from(chunk.pixels.entries()).map(([key, pixelInfo]) => {
|
||||
const [localX, localY] = key.split(',').map(Number);
|
||||
return {
|
||||
x: message.chunkX * 64 + localX,
|
||||
y: message.chunkY * 64 + localY,
|
||||
color: pixelInfo.color,
|
||||
userId: pixelInfo.userId,
|
||||
username: pixelInfo.username,
|
||||
timestamp: pixelInfo.timestamp
|
||||
};
|
||||
});
|
||||
|
||||
socket.emit('chunk_data', {
|
||||
type: MessageType.CHUNK_DATA,
|
||||
chunkX: message.chunkX,
|
||||
chunkY: message.chunkY,
|
||||
pixels,
|
||||
canvasId,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
} else {
|
||||
// Send empty chunk
|
||||
socket.emit('chunk_data', {
|
||||
type: MessageType.CHUNK_DATA,
|
||||
chunkX: message.chunkX,
|
||||
chunkY: message.chunkY,
|
||||
pixels: [],
|
||||
canvasId,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading chunk:', error);
|
||||
socket.emit('error', { message: 'Failed to load chunk' });
|
||||
}
|
||||
}
|
||||
|
||||
private async handleCursorMove(socket: Socket, message: CursorMoveMessage): Promise<void> {
|
||||
const { userId, canvasId } = socket.data;
|
||||
|
||||
if (!userId || !canvasId) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check rate limit
|
||||
const allowed = await this.rateLimitService.checkCursorRateLimit(userId);
|
||||
if (!allowed) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update presence
|
||||
const user = await this.userService.getUser(userId);
|
||||
if (user) {
|
||||
this.userPresence.set(userId, {
|
||||
userId,
|
||||
username: user.username,
|
||||
cursor: { x: message.x, y: message.y },
|
||||
color: '#ff0000', // TODO: Get user's selected color
|
||||
tool: message.tool,
|
||||
isActive: true
|
||||
});
|
||||
|
||||
// Broadcast cursor position to others in the canvas (excluding sender)
|
||||
socket.to(`canvas:${canvasId}`).emit('cursor_update', {
|
||||
userId,
|
||||
username: user.username,
|
||||
x: message.x,
|
||||
y: message.y,
|
||||
tool: message.tool
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public getIO(): SocketIOServer {
|
||||
return this.io;
|
||||
}
|
||||
|
||||
public async broadcastToCanvas(canvasId: string, event: string, data: any): Promise<void> {
|
||||
this.io.to(`canvas:${canvasId}`).emit(event, data);
|
||||
}
|
||||
}
|
||||
23
backend/tsconfig.json
Normal file
23
backend/tsconfig.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "CommonJS",
|
||||
"moduleResolution": "node",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"allowJs": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"resolveJsonModule": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
||||
}
|
||||
76
docker-compose.yml
Normal file
76
docker-compose.yml
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
version: '3.8'
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
command: redis-server --appendonly yes
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
|
||||
postgres:
|
||||
image: postgres:15-alpine
|
||||
environment:
|
||||
POSTGRES_DB: gaplace
|
||||
POSTGRES_USER: gaplace
|
||||
POSTGRES_PASSWORD: password
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U gaplace"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
|
||||
backend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: backend/Dockerfile
|
||||
ports:
|
||||
- "3001:3001"
|
||||
environment:
|
||||
- NODE_ENV=development
|
||||
- REDIS_URL=redis://redis:6379
|
||||
- POSTGRES_HOST=postgres
|
||||
- POSTGRES_PORT=5432
|
||||
- POSTGRES_DB=gaplace
|
||||
- POSTGRES_USER=gaplace
|
||||
- POSTGRES_PASSWORD=password
|
||||
- JWT_SECRET=development-secret-change-in-production
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./backend:/app
|
||||
- /app/node_modules
|
||||
command: npm run dev
|
||||
|
||||
frontend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: frontend/Dockerfile
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- NEXT_PUBLIC_BACKEND_URL=http://localhost:3001
|
||||
depends_on:
|
||||
- backend
|
||||
volumes:
|
||||
- ./frontend:/app
|
||||
- /app/node_modules
|
||||
- /app/.next
|
||||
command: npm run dev
|
||||
|
||||
volumes:
|
||||
redis_data:
|
||||
postgres_data:
|
||||
1
frontend/.env.local.example
Normal file
1
frontend/.env.local.example
Normal file
|
|
@ -0,0 +1 @@
|
|||
NEXT_PUBLIC_BACKEND_URL=http://localhost:3001
|
||||
10
frontend/.eslintrc.json
Normal file
10
frontend/.eslintrc.json
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"extends": ["next/core-web-vitals", "../.eslintrc.json"],
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es6": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
}
|
||||
}
|
||||
26
frontend/Dockerfile
Normal file
26
frontend/Dockerfile
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
FROM node:18-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy package files
|
||||
COPY frontend/package*.json ./
|
||||
COPY shared/ ../shared/
|
||||
|
||||
# Install dependencies
|
||||
RUN npm install
|
||||
|
||||
# Copy source code
|
||||
COPY frontend/ .
|
||||
|
||||
# Build the application
|
||||
RUN npm run build
|
||||
|
||||
# Expose port
|
||||
EXPOSE 3000
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget --no-verbose --tries=1 --spider http://localhost:3000 || exit 1
|
||||
|
||||
# Start the application
|
||||
CMD ["npm", "start"]
|
||||
6
frontend/next-env.d.ts
vendored
Normal file
6
frontend/next-env.d.ts
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
/// <reference path="./.next/types/routes.d.ts" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
30
frontend/next.config.js
Normal file
30
frontend/next.config.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
images: {
|
||||
remotePatterns: [
|
||||
{
|
||||
protocol: 'http',
|
||||
hostname: 'localhost',
|
||||
port: '3001',
|
||||
pathname: '/**',
|
||||
},
|
||||
{
|
||||
protocol: 'http',
|
||||
hostname: '192.168.1.110',
|
||||
port: '3001',
|
||||
pathname: '/**',
|
||||
},
|
||||
],
|
||||
},
|
||||
allowedDevOrigins: ['192.168.1.110:3000', '192.168.1.110'],
|
||||
async rewrites() {
|
||||
return [
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: `${process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:3001'}/api/:path*`,
|
||||
},
|
||||
];
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = nextConfig;
|
||||
35
frontend/package.json
Normal file
35
frontend/package.json
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"name": "@gaplace/frontend",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint",
|
||||
"type-check": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@gaplace/shared": "file:../shared",
|
||||
"next": "^15.5.0",
|
||||
"react": "^19.1.1",
|
||||
"react-dom": "^19.1.1",
|
||||
"socket.io-client": "^4.8.1",
|
||||
"zustand": "^5.0.2",
|
||||
"@tanstack/react-query": "^5.62.8",
|
||||
"framer-motion": "^11.15.0",
|
||||
"tailwindcss": "^3.4.17",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"postcss": "^8.5.4",
|
||||
"clsx": "^2.1.1",
|
||||
"tailwind-merge": "^2.5.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.7.3",
|
||||
"@types/node": "^22.10.6",
|
||||
"@types/react": "^19.0.2",
|
||||
"@types/react-dom": "^19.0.2",
|
||||
"eslint": "^8.57.1",
|
||||
"eslint-config-next": "^15.5.0"
|
||||
}
|
||||
}
|
||||
6
frontend/postcss.config.js
Normal file
6
frontend/postcss.config.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
module.exports = {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
};
|
||||
104
frontend/src/app/globals.css
Normal file
104
frontend/src/app/globals.css
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@layer base {
|
||||
html {
|
||||
scroll-behavior: smooth;
|
||||
touch-action: manipulation;
|
||||
}
|
||||
|
||||
body {
|
||||
@apply bg-white dark:bg-gray-900 text-gray-900 dark:text-white transition-colors duration-300;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
-webkit-touch-callout: none;
|
||||
-webkit-user-select: none;
|
||||
-khtml-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
}
|
||||
|
||||
@layer components {
|
||||
.canvas-container {
|
||||
@apply relative overflow-hidden bg-white dark:bg-gray-800 rounded-lg shadow-lg;
|
||||
image-rendering: pixelated;
|
||||
image-rendering: -moz-crisp-edges;
|
||||
image-rendering: crisp-edges;
|
||||
}
|
||||
|
||||
.pixel {
|
||||
@apply absolute cursor-crosshair;
|
||||
image-rendering: pixelated;
|
||||
}
|
||||
|
||||
.color-picker-button {
|
||||
@apply w-8 h-8 rounded-full border-2 border-gray-300 dark:border-gray-600 cursor-pointer transition-all duration-200 hover:scale-110;
|
||||
}
|
||||
|
||||
.color-picker-button.selected {
|
||||
@apply border-4 border-blue-500 scale-110 shadow-lg;
|
||||
}
|
||||
|
||||
.tool-button {
|
||||
@apply p-2 rounded-lg bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors duration-200;
|
||||
}
|
||||
|
||||
.tool-button.active {
|
||||
@apply bg-blue-500 text-white hover:bg-blue-600;
|
||||
}
|
||||
}
|
||||
|
||||
@layer utilities {
|
||||
.scrollbar-hide {
|
||||
-ms-overflow-style: none;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
|
||||
.scrollbar-hide::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Mobile-first touch optimizations */
|
||||
.touch-target {
|
||||
min-height: 44px;
|
||||
min-width: 44px;
|
||||
}
|
||||
|
||||
/* Ensure crisp rendering on all devices */
|
||||
.crisp-edges {
|
||||
image-rendering: pixelated;
|
||||
image-rendering: -moz-crisp-edges;
|
||||
image-rendering: crisp-edges;
|
||||
image-rendering: -webkit-optimize-contrast;
|
||||
}
|
||||
|
||||
/* Mobile modal centering improvements */
|
||||
.mobile-modal {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 1rem;
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
/* Safe area support for modern mobile devices */
|
||||
@supports (padding: max(0px)) {
|
||||
.mobile-modal {
|
||||
padding-top: max(1rem, env(safe-area-inset-top));
|
||||
padding-bottom: max(1rem, env(safe-area-inset-bottom));
|
||||
padding-left: max(1rem, env(safe-area-inset-left));
|
||||
padding-right: max(1rem, env(safe-area-inset-right));
|
||||
}
|
||||
}
|
||||
}
|
||||
37
frontend/src/app/layout.tsx
Normal file
37
frontend/src/app/layout.tsx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import type { Metadata } from 'next';
|
||||
import { Inter } from 'next/font/google';
|
||||
import './globals.css';
|
||||
import { Providers } from './providers';
|
||||
|
||||
const inter = Inter({ subsets: ['latin'] });
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'GaPlace - Collaborative Pixel Art',
|
||||
description: 'Create collaborative pixel art in real-time with infinite canvas and modern features',
|
||||
keywords: ['pixel art', 'collaborative', 'real-time', 'canvas', 'drawing'],
|
||||
authors: [{ name: 'GaPlace Team' }],
|
||||
};
|
||||
|
||||
export const viewport = {
|
||||
width: 'device-width',
|
||||
initialScale: 1,
|
||||
maximumScale: 5,
|
||||
userScalable: true,
|
||||
viewportFit: 'cover',
|
||||
};
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<body className={inter.className}>
|
||||
<Providers>
|
||||
{children}
|
||||
</Providers>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
298
frontend/src/app/page.tsx
Normal file
298
frontend/src/app/page.tsx
Normal file
|
|
@ -0,0 +1,298 @@
|
|||
'use client';
|
||||
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import { VirtualCanvas } from '../components/canvas/VirtualCanvas';
|
||||
import { CooldownTimer } from '../components/ui/CooldownTimer';
|
||||
import { PixelConfirmModal } from '../components/ui/PixelConfirmModal';
|
||||
import { StatsOverlay } from '../components/ui/StatsOverlay';
|
||||
import { CoordinateDisplay } from '../components/ui/CoordinateDisplay';
|
||||
import { UsernameModal } from '../components/ui/UsernameModal';
|
||||
import { SettingsButton } from '../components/ui/SettingsButton';
|
||||
import { ZoomControls } from '../components/ui/ZoomControls';
|
||||
import { useWebSocket } from '../hooks/useWebSocket';
|
||||
import { useCanvasStore } from '../store/canvasStore';
|
||||
import { ErrorBoundary } from '../components/ErrorBoundary';
|
||||
import type { PixelPlacedMessage, ChunkDataMessage } from '@gaplace/shared';
|
||||
|
||||
export default function HomePage() {
|
||||
const [selectedColor, setSelectedColor] = useState('#FF0000');
|
||||
const [pendingPixel, setPendingPixel] = useState<{ x: number; y: number } | null>(null);
|
||||
const [isCooldownActive, setIsCooldownActive] = useState(false);
|
||||
const [onlineUsers, setOnlineUsers] = useState(1);
|
||||
const [totalPixels, setTotalPixels] = useState(0);
|
||||
const [hoverCoords, setHoverCoords] = useState<{ x: number; y: number; pixelInfo: { color: string; userId?: string; username?: string } | null } | null>(null);
|
||||
const [username, setUsername] = useState('');
|
||||
const [showUsernameModal, setShowUsernameModal] = useState(false);
|
||||
|
||||
// Generate userId once and keep it stable, store in localStorage
|
||||
const [userId] = useState(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
let storedUserId = localStorage.getItem('gaplace-user-id');
|
||||
if (!storedUserId) {
|
||||
storedUserId = 'guest-' + Math.random().toString(36).substr(2, 9);
|
||||
localStorage.setItem('gaplace-user-id', storedUserId);
|
||||
}
|
||||
return storedUserId;
|
||||
}
|
||||
return 'guest-' + Math.random().toString(36).substr(2, 9);
|
||||
});
|
||||
|
||||
// Load username from localStorage and show modal if none exists
|
||||
useEffect(() => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const storedUsername = localStorage.getItem('gaplace-username');
|
||||
if (storedUsername) {
|
||||
setUsername(storedUsername);
|
||||
} else {
|
||||
setShowUsernameModal(true);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Canvas store
|
||||
const { setPixel, loadChunk: loadChunkToStore, viewport, setZoom, setViewport } = useCanvasStore();
|
||||
|
||||
const handlePixelPlaced = useCallback((message: PixelPlacedMessage & { username?: string }) => {
|
||||
console.log('Pixel placed:', message);
|
||||
setPixel(message.x, message.y, message.color, message.userId, message.username);
|
||||
}, [setPixel]);
|
||||
|
||||
const handleChunkData = useCallback((message: ChunkDataMessage) => {
|
||||
console.log('Chunk data received:', message);
|
||||
loadChunkToStore(message.chunkX, message.chunkY, message.pixels);
|
||||
}, [loadChunkToStore]);
|
||||
|
||||
const handleUserList = useCallback((users: string[]) => {
|
||||
setOnlineUsers(users.length);
|
||||
}, []);
|
||||
|
||||
const handleCanvasStats = useCallback((stats: { totalPixels?: number; activeUsers?: number; lastActivity?: number }) => {
|
||||
setTotalPixels(stats.totalPixels || 0);
|
||||
}, []);
|
||||
|
||||
const { isConnected, placePixel, loadChunk, moveCursor } = useWebSocket({
|
||||
canvasId: 'main',
|
||||
userId,
|
||||
username,
|
||||
onPixelPlaced: handlePixelPlaced,
|
||||
onChunkData: handleChunkData,
|
||||
onUserList: handleUserList,
|
||||
onCanvasStats: handleCanvasStats,
|
||||
});
|
||||
|
||||
const handlePixelClick = useCallback((x: number, y: number) => {
|
||||
if (isCooldownActive) return;
|
||||
setPendingPixel({ x, y });
|
||||
}, [isCooldownActive]);
|
||||
|
||||
const handleConfirmPixel = useCallback(() => {
|
||||
if (!pendingPixel) return;
|
||||
|
||||
// Immediately place pixel locally for instant feedback
|
||||
setPixel(pendingPixel.x, pendingPixel.y, selectedColor, userId, username);
|
||||
|
||||
// Send to server
|
||||
placePixel(pendingPixel.x, pendingPixel.y, selectedColor);
|
||||
setPendingPixel(null);
|
||||
setIsCooldownActive(true);
|
||||
}, [pendingPixel, selectedColor, placePixel, setPixel, userId, username]);
|
||||
|
||||
const handleCancelPixel = useCallback(() => {
|
||||
setPendingPixel(null);
|
||||
}, []);
|
||||
|
||||
const handleCooldownComplete = useCallback(() => {
|
||||
setIsCooldownActive(false);
|
||||
}, []);
|
||||
|
||||
const handleCursorMove = useCallback((x: number, y: number) => {
|
||||
moveCursor(x, y, 'pixel');
|
||||
}, [moveCursor]);
|
||||
|
||||
const handleChunkNeeded = useCallback((chunkX: number, chunkY: number) => {
|
||||
loadChunk(chunkX, chunkY);
|
||||
}, [loadChunk]);
|
||||
|
||||
const handleHoverChange = useCallback((x: number, y: number, pixelInfo: { color: string; userId?: string } | null) => {
|
||||
setHoverCoords({ x, y, pixelInfo });
|
||||
}, []);
|
||||
|
||||
const handleUsernameChange = useCallback((newUsername: string) => {
|
||||
setUsername(newUsername);
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('gaplace-username', newUsername);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleZoomIn = useCallback(() => {
|
||||
const newZoom = Math.min(viewport.zoom * 1.2, 5.0);
|
||||
|
||||
// Zoom towards center of viewport (canvas center)
|
||||
if (typeof window !== 'undefined') {
|
||||
const screenCenterX = window.innerWidth / 2;
|
||||
const screenCenterY = window.innerHeight / 2;
|
||||
|
||||
// Calculate what canvas coordinate is currently at screen center
|
||||
const BASE_PIXEL_SIZE = 32;
|
||||
const currentPixelSize = BASE_PIXEL_SIZE * viewport.zoom;
|
||||
const newPixelSize = BASE_PIXEL_SIZE * newZoom;
|
||||
|
||||
const canvasCenterX = (screenCenterX + viewport.x) / currentPixelSize;
|
||||
const canvasCenterY = (screenCenterY + viewport.y) / currentPixelSize;
|
||||
|
||||
// Calculate new viewport to keep same canvas point at screen center
|
||||
const newViewportX = canvasCenterX * newPixelSize - screenCenterX;
|
||||
const newViewportY = canvasCenterY * newPixelSize - screenCenterY;
|
||||
|
||||
setViewport({
|
||||
zoom: newZoom,
|
||||
x: newViewportX,
|
||||
y: newViewportY,
|
||||
});
|
||||
} else {
|
||||
setZoom(newZoom);
|
||||
}
|
||||
}, [setZoom, setViewport, viewport]);
|
||||
|
||||
const handleZoomOut = useCallback(() => {
|
||||
const newZoom = Math.max(viewport.zoom / 1.2, 0.1);
|
||||
|
||||
// Zoom towards center of viewport (canvas center)
|
||||
if (typeof window !== 'undefined') {
|
||||
const screenCenterX = window.innerWidth / 2;
|
||||
const screenCenterY = window.innerHeight / 2;
|
||||
|
||||
// Calculate what canvas coordinate is currently at screen center
|
||||
const BASE_PIXEL_SIZE = 32;
|
||||
const currentPixelSize = BASE_PIXEL_SIZE * viewport.zoom;
|
||||
const newPixelSize = BASE_PIXEL_SIZE * newZoom;
|
||||
|
||||
const canvasCenterX = (screenCenterX + viewport.x) / currentPixelSize;
|
||||
const canvasCenterY = (screenCenterY + viewport.y) / currentPixelSize;
|
||||
|
||||
// Calculate new viewport to keep same canvas point at screen center
|
||||
const newViewportX = canvasCenterX * newPixelSize - screenCenterX;
|
||||
const newViewportY = canvasCenterY * newPixelSize - screenCenterY;
|
||||
|
||||
setViewport({
|
||||
zoom: newZoom,
|
||||
x: newViewportX,
|
||||
y: newViewportY,
|
||||
});
|
||||
} else {
|
||||
setZoom(newZoom);
|
||||
}
|
||||
}, [setZoom, setViewport, viewport]);
|
||||
|
||||
return (
|
||||
<ErrorBoundary>
|
||||
<div className="relative w-full h-screen overflow-hidden">
|
||||
{/* Fullscreen Canvas */}
|
||||
<ErrorBoundary fallback={
|
||||
<div className="w-full h-full bg-gray-900 flex items-center justify-center">
|
||||
<div className="text-white text-center">
|
||||
<div className="text-4xl mb-4">🎨</div>
|
||||
<div>Canvas failed to load</div>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="mt-4 px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg transition-colors"
|
||||
>
|
||||
Reload
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
}>
|
||||
<VirtualCanvas
|
||||
onPixelClick={handlePixelClick}
|
||||
onCursorMove={handleCursorMove}
|
||||
onChunkNeeded={handleChunkNeeded}
|
||||
onHoverChange={handleHoverChange}
|
||||
selectedColor={selectedColor}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
|
||||
{/* Overlay UI Components */}
|
||||
<ErrorBoundary>
|
||||
<StatsOverlay
|
||||
onlineUsers={onlineUsers}
|
||||
totalPixels={totalPixels}
|
||||
zoom={viewport.zoom}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
|
||||
{/* Zoom Controls */}
|
||||
<ErrorBoundary>
|
||||
<ZoomControls
|
||||
zoom={viewport.zoom}
|
||||
onZoomIn={handleZoomIn}
|
||||
onZoomOut={handleZoomOut}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
|
||||
{/* Username Settings Button - Only show when no stats are visible */}
|
||||
{username && (
|
||||
<ErrorBoundary>
|
||||
<SettingsButton
|
||||
username={username}
|
||||
onOpenSettings={() => setShowUsernameModal(true)}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
)}
|
||||
|
||||
<ErrorBoundary>
|
||||
<CooldownTimer
|
||||
isActive={isCooldownActive}
|
||||
duration={10}
|
||||
onComplete={handleCooldownComplete}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
|
||||
<ErrorBoundary>
|
||||
<PixelConfirmModal
|
||||
isOpen={!!pendingPixel}
|
||||
x={pendingPixel?.x || 0}
|
||||
y={pendingPixel?.y || 0}
|
||||
color={selectedColor}
|
||||
onColorChange={setSelectedColor}
|
||||
onConfirm={handleConfirmPixel}
|
||||
onCancel={handleCancelPixel}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
|
||||
{/* Coordinate Display */}
|
||||
{hoverCoords && (
|
||||
<ErrorBoundary>
|
||||
<CoordinateDisplay
|
||||
x={hoverCoords.x}
|
||||
y={hoverCoords.y}
|
||||
pixelColor={hoverCoords.pixelInfo?.color || null}
|
||||
pixelOwner={hoverCoords.pixelInfo?.username || hoverCoords.pixelInfo?.userId || null}
|
||||
zoom={viewport.zoom}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
)}
|
||||
|
||||
{/* Username Modal */}
|
||||
<ErrorBoundary>
|
||||
<UsernameModal
|
||||
isOpen={showUsernameModal}
|
||||
currentUsername={username}
|
||||
onUsernameChange={handleUsernameChange}
|
||||
onClose={() => setShowUsernameModal(false)}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
|
||||
{/* Connection Status */}
|
||||
{!isConnected && (
|
||||
<ErrorBoundary>
|
||||
<div className="fixed top-4 sm:top-6 left-1/2 transform -translate-x-1/2 z-50">
|
||||
<div className="bg-red-500/90 backdrop-blur-md rounded-xl px-3 sm:px-4 py-2 text-white text-xs sm:text-sm font-medium">
|
||||
Connecting...
|
||||
</div>
|
||||
</div>
|
||||
</ErrorBoundary>
|
||||
)}
|
||||
</div>
|
||||
</ErrorBoundary>
|
||||
);
|
||||
}
|
||||
27
frontend/src/app/providers.tsx
Normal file
27
frontend/src/app/providers.tsx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
'use client';
|
||||
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { useState } from 'react';
|
||||
import { ThemeProvider } from '../components/ThemeProvider';
|
||||
|
||||
export function Providers({ children }: { children: React.ReactNode }) {
|
||||
const [queryClient] = useState(
|
||||
() =>
|
||||
new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: 60 * 1000, // 1 minute
|
||||
refetchOnWindowFocus: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ThemeProvider>
|
||||
{children}
|
||||
</ThemeProvider>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
58
frontend/src/components/ErrorBoundary.tsx
Normal file
58
frontend/src/components/ErrorBoundary.tsx
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
'use client';
|
||||
|
||||
import React, { Component, ErrorInfo, ReactNode } from 'react';
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
fallback?: ReactNode;
|
||||
}
|
||||
|
||||
interface State {
|
||||
hasError: boolean;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
export class ErrorBoundary extends Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.state = { hasError: false };
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: Error): State {
|
||||
return { hasError: true, error };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||||
console.error('ErrorBoundary caught an error:', error, errorInfo);
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
if (this.props.fallback) {
|
||||
return this.props.fallback;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full h-full flex items-center justify-center bg-red-50 dark:bg-red-900/20 rounded-lg border border-red-200 dark:border-red-800">
|
||||
<div className="text-center p-8">
|
||||
<div className="text-6xl mb-4">⚠️</div>
|
||||
<h2 className="text-xl font-semibold text-red-800 dark:text-red-200 mb-2">
|
||||
Something went wrong
|
||||
</h2>
|
||||
<p className="text-red-600 dark:text-red-300 mb-4 max-w-md">
|
||||
{this.state.error?.message || 'An unexpected error occurred'}
|
||||
</p>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg transition-colors"
|
||||
>
|
||||
Reload Page
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
53
frontend/src/components/ThemeProvider.tsx
Normal file
53
frontend/src/components/ThemeProvider.tsx
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
'use client';
|
||||
|
||||
import { createContext, useContext, useEffect, useState } from 'react';
|
||||
|
||||
type Theme = 'light' | 'dark' | 'system';
|
||||
|
||||
interface ThemeContextType {
|
||||
theme: Theme;
|
||||
setTheme: (theme: Theme) => void;
|
||||
}
|
||||
|
||||
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
|
||||
|
||||
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
||||
const [theme, setTheme] = useState<Theme>('system');
|
||||
|
||||
useEffect(() => {
|
||||
const stored = localStorage.getItem('theme') as Theme;
|
||||
if (stored) {
|
||||
setTheme(stored);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const root = window.document.documentElement;
|
||||
root.classList.remove('light', 'dark');
|
||||
|
||||
if (theme === 'system') {
|
||||
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
? 'dark'
|
||||
: 'light';
|
||||
root.classList.add(systemTheme);
|
||||
} else {
|
||||
root.classList.add(theme);
|
||||
}
|
||||
|
||||
localStorage.setItem('theme', theme);
|
||||
}, [theme]);
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ theme, setTheme }}>
|
||||
{children}
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const useTheme = () => {
|
||||
const context = useContext(ThemeContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useTheme must be used within a ThemeProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
113
frontend/src/components/canvas/CanvasContainer.tsx
Normal file
113
frontend/src/components/canvas/CanvasContainer.tsx
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { VirtualCanvas } from './VirtualCanvas';
|
||||
import { useWebSocket } from '../../hooks/useWebSocket';
|
||||
import { useCanvasStore } from '../../store/canvasStore';
|
||||
import { PixelPlacedMessage, ChunkDataMessage } from '@gaplace/shared';
|
||||
|
||||
export function CanvasContainer() {
|
||||
const [userId] = useState(() => `user_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`);
|
||||
const canvasId = 'default'; // TODO: Make this dynamic
|
||||
|
||||
const {
|
||||
selectedColor,
|
||||
selectedTool,
|
||||
setPixel,
|
||||
loadChunk,
|
||||
setUserCursor,
|
||||
removeUserCursor,
|
||||
setActiveUsers,
|
||||
setStats,
|
||||
} = useCanvasStore();
|
||||
|
||||
const { isConnected, connectionError, placePixel, loadChunk: requestChunk, moveCursor } = useWebSocket({
|
||||
canvasId,
|
||||
userId,
|
||||
onPixelPlaced: (message: PixelPlacedMessage) => {
|
||||
setPixel(message.x, message.y, message.color, message.userId);
|
||||
},
|
||||
onChunkData: (message: ChunkDataMessage) => {
|
||||
loadChunk(message.chunkX, message.chunkY, message.pixels);
|
||||
},
|
||||
onUserList: (users: string[]) => {
|
||||
setActiveUsers(users);
|
||||
},
|
||||
onCanvasStats: (stats: { totalPixels?: number; activeUsers?: number; lastActivity?: number; userPixels?: number }) => {
|
||||
setStats(stats.totalPixels || 0, stats.userPixels || 0);
|
||||
},
|
||||
onCursorUpdate: (data: { userId: string; username: string; x: number; y: number; tool: string }) => {
|
||||
setUserCursor(data.userId, data.x, data.y, data.username, '#ff0000');
|
||||
},
|
||||
});
|
||||
|
||||
const handlePixelClick = (x: number, y: number) => {
|
||||
if (!isConnected) return;
|
||||
|
||||
if (selectedTool === 'pixel') {
|
||||
placePixel(x, y, selectedColor);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCursorMove = (x: number, y: number) => {
|
||||
if (!isConnected) return;
|
||||
moveCursor(x, y, selectedTool);
|
||||
};
|
||||
|
||||
const handleChunkNeeded = (chunkX: number, chunkY: number) => {
|
||||
if (!isConnected) return;
|
||||
requestChunk(chunkX, chunkY);
|
||||
};
|
||||
|
||||
const handleHoverChange = (x: number, y: number, pixelInfo: { color: string; userId?: string } | null) => {
|
||||
// Handle pixel hover for tooltips or UI updates
|
||||
};
|
||||
|
||||
if (connectionError) {
|
||||
return (
|
||||
<div className="w-full h-full flex items-center justify-center bg-gray-100 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-center">
|
||||
<div className="text-red-500 mb-2">⚠️ Connection Error</div>
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">{connectionError}</div>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
|
||||
>
|
||||
Refresh Page
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!isConnected) {
|
||||
return (
|
||||
<div className="w-full h-full flex items-center justify-center bg-gray-100 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-center">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500 mx-auto mb-4"></div>
|
||||
<div className="text-gray-600 dark:text-gray-400">Connecting to GaPlace...</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full h-full bg-white dark:bg-gray-800 rounded-lg shadow-lg overflow-hidden">
|
||||
<VirtualCanvas
|
||||
onPixelClick={handlePixelClick}
|
||||
onCursorMove={handleCursorMove}
|
||||
onChunkNeeded={handleChunkNeeded}
|
||||
onHoverChange={handleHoverChange}
|
||||
selectedColor={selectedColor}
|
||||
/>
|
||||
|
||||
{/* Connection status indicator */}
|
||||
<div className="absolute top-4 right-4 flex items-center space-x-2 bg-black/20 backdrop-blur-sm rounded-lg px-3 py-1">
|
||||
<div className={`w-2 h-2 rounded-full ${isConnected ? 'bg-green-400' : 'bg-red-400'}`} />
|
||||
<span className="text-sm text-white">
|
||||
{isConnected ? 'Connected' : 'Disconnected'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
582
frontend/src/components/canvas/VirtualCanvas.tsx
Normal file
582
frontend/src/components/canvas/VirtualCanvas.tsx
Normal file
|
|
@ -0,0 +1,582 @@
|
|||
'use client';
|
||||
|
||||
import { useRef, useEffect, useState, useCallback } from 'react';
|
||||
import { useCanvasStore } from '../../store/canvasStore';
|
||||
import { CANVAS_CONFIG } from '@gaplace/shared';
|
||||
|
||||
interface VirtualCanvasProps {
|
||||
onPixelClick: (x: number, y: number) => void;
|
||||
onCursorMove: (x: number, y: number) => void;
|
||||
onChunkNeeded: (chunkX: number, chunkY: number) => void;
|
||||
onHoverChange: (x: number, y: number, pixelInfo: { color: string; userId?: string } | null) => void;
|
||||
selectedColor: string;
|
||||
}
|
||||
|
||||
export function VirtualCanvas({ onPixelClick, onCursorMove, onChunkNeeded, onHoverChange, selectedColor }: VirtualCanvasProps) {
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const animationFrameRef = useRef<number | undefined>(undefined);
|
||||
const isMouseDownRef = useRef(false);
|
||||
const isPanningRef = useRef(false);
|
||||
const lastPanPointRef = useRef<{ x: number; y: number } | null>(null);
|
||||
const mouseDownPositionRef = useRef<{ x: number; y: number } | null>(null);
|
||||
const DRAG_THRESHOLD = 5; // pixels
|
||||
const [cursorStyle, setCursorStyle] = useState<'crosshair' | 'grab' | 'grabbing'>('crosshair');
|
||||
const [hoverPixel, setHoverPixel] = useState<{ x: number; y: number } | null>(null);
|
||||
|
||||
// Touch handling state
|
||||
const touchStartRef = useRef<{ x: number; y: number; time: number } | null>(null);
|
||||
const lastTouchesRef = useRef<TouchList | null>(null);
|
||||
const pinchStartDistanceRef = useRef<number | null>(null);
|
||||
const pinchStartZoomRef = useRef<number | null>(null);
|
||||
|
||||
const {
|
||||
viewport,
|
||||
chunks,
|
||||
selectedTool,
|
||||
showGrid,
|
||||
userCursors,
|
||||
setViewport,
|
||||
setZoom,
|
||||
pan,
|
||||
getChunkCoordinates,
|
||||
getPixelAt,
|
||||
getPixelInfo,
|
||||
} = useCanvasStore();
|
||||
|
||||
// Large modern pixel size - fixed base size to avoid circular dependencies
|
||||
const BASE_PIXEL_SIZE = 32;
|
||||
const pixelSize = BASE_PIXEL_SIZE * viewport.zoom;
|
||||
|
||||
// Convert screen coordinates to canvas coordinates
|
||||
const screenToCanvas = useCallback((screenX: number, screenY: number) => {
|
||||
const rect = canvasRef.current?.getBoundingClientRect();
|
||||
if (!rect) return { x: 0, y: 0 };
|
||||
|
||||
const x = Math.floor((screenX - rect.left + viewport.x) / pixelSize);
|
||||
const y = Math.floor((screenY - rect.top + viewport.y) / pixelSize);
|
||||
return { x, y };
|
||||
}, [viewport.x, viewport.y, pixelSize]);
|
||||
|
||||
// Convert canvas coordinates to screen coordinates
|
||||
const canvasToScreen = useCallback((canvasX: number, canvasY: number) => {
|
||||
return {
|
||||
x: canvasX * pixelSize - viewport.x,
|
||||
y: canvasY * pixelSize - viewport.y,
|
||||
};
|
||||
}, [viewport.x, viewport.y, pixelSize]);
|
||||
|
||||
// Track requested chunks to prevent spam
|
||||
const requestedChunksRef = useRef(new Set<string>());
|
||||
|
||||
// Get visible chunks and request loading if needed
|
||||
const getVisibleChunks = useCallback(() => {
|
||||
const canvas = canvasRef.current;
|
||||
if (!canvas) return [];
|
||||
|
||||
const startX = Math.floor(viewport.x / pixelSize);
|
||||
const startY = Math.floor(viewport.y / pixelSize);
|
||||
const endX = Math.floor((viewport.x + canvas.width) / pixelSize);
|
||||
const endY = Math.floor((viewport.y + canvas.height) / pixelSize);
|
||||
|
||||
const startChunkX = Math.floor(startX / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
const startChunkY = Math.floor(startY / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
const endChunkX = Math.floor(endX / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
const endChunkY = Math.floor(endY / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE);
|
||||
|
||||
const visibleChunks: Array<{ chunkX: number; chunkY: number }> = [];
|
||||
|
||||
for (let chunkX = startChunkX; chunkX <= endChunkX; chunkX++) {
|
||||
for (let chunkY = startChunkY; chunkY <= endChunkY; chunkY++) {
|
||||
visibleChunks.push({ chunkX, chunkY });
|
||||
|
||||
// Request chunk if not loaded and not already requested
|
||||
const chunkKey = `${chunkX},${chunkY}`;
|
||||
const chunk = chunks.get(chunkKey);
|
||||
if (!chunk || !chunk.isLoaded) {
|
||||
if (!requestedChunksRef.current.has(chunkKey)) {
|
||||
requestedChunksRef.current.add(chunkKey);
|
||||
onChunkNeeded(chunkX, chunkY);
|
||||
// Remove from requested after a delay to allow retry
|
||||
setTimeout(() => {
|
||||
requestedChunksRef.current.delete(chunkKey);
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return visibleChunks;
|
||||
}, [viewport, pixelSize, chunks, onChunkNeeded]);
|
||||
|
||||
// Track dirty state to avoid unnecessary renders
|
||||
const isDirtyRef = useRef(true);
|
||||
const lastRenderTimeRef = useRef(0);
|
||||
const MIN_RENDER_INTERVAL = 16; // ~60fps max
|
||||
|
||||
// Render function with performance optimizations
|
||||
const render = useCallback(() => {
|
||||
const now = performance.now();
|
||||
if (now - lastRenderTimeRef.current < MIN_RENDER_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDirtyRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
const canvas = canvasRef.current;
|
||||
const ctx = canvas?.getContext('2d');
|
||||
if (!canvas || !ctx) return;
|
||||
|
||||
// Clear canvas
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
// Set pixel rendering
|
||||
ctx.imageSmoothingEnabled = false;
|
||||
|
||||
const visibleChunks = getVisibleChunks();
|
||||
|
||||
// Render pixels from visible chunks
|
||||
for (const { chunkX, chunkY } of visibleChunks) {
|
||||
const chunkKey = `${chunkX},${chunkY}`;
|
||||
const chunk = chunks.get(chunkKey);
|
||||
|
||||
if (!chunk || !chunk.isLoaded) continue;
|
||||
|
||||
for (const [pixelKey, pixelInfo] of chunk.pixels) {
|
||||
const [localX, localY] = pixelKey.split(',').map(Number);
|
||||
const worldX = chunkX * CANVAS_CONFIG.DEFAULT_CHUNK_SIZE + localX;
|
||||
const worldY = chunkY * CANVAS_CONFIG.DEFAULT_CHUNK_SIZE + localY;
|
||||
|
||||
const screenPos = canvasToScreen(worldX, worldY);
|
||||
|
||||
// Only render if pixel is visible
|
||||
if (
|
||||
screenPos.x >= -pixelSize &&
|
||||
screenPos.y >= -pixelSize &&
|
||||
screenPos.x < canvas.width &&
|
||||
screenPos.y < canvas.height
|
||||
) {
|
||||
ctx.fillStyle = pixelInfo.color;
|
||||
ctx.fillRect(screenPos.x, screenPos.y, pixelSize, pixelSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Render grid only when enabled and zoomed in enough (pixel size > 16px)
|
||||
if (showGrid && pixelSize > 16) {
|
||||
ctx.strokeStyle = 'rgba(255, 255, 255, 0.08)';
|
||||
ctx.lineWidth = 1;
|
||||
ctx.shadowColor = 'rgba(0, 0, 0, 0.1)';
|
||||
ctx.shadowBlur = 2;
|
||||
|
||||
const startX = Math.floor(viewport.x / pixelSize) * pixelSize - viewport.x;
|
||||
const startY = Math.floor(viewport.y / pixelSize) * pixelSize - viewport.y;
|
||||
|
||||
// Vertical grid lines
|
||||
for (let x = startX; x < canvas.width; x += pixelSize) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, 0);
|
||||
ctx.lineTo(x, canvas.height);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// Horizontal grid lines
|
||||
for (let y = startY; y < canvas.height; y += pixelSize) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, y);
|
||||
ctx.lineTo(canvas.width, y);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// Reset shadow
|
||||
ctx.shadowBlur = 0;
|
||||
}
|
||||
|
||||
// Render user cursors
|
||||
for (const [userId, cursor] of userCursors) {
|
||||
const screenPos = canvasToScreen(cursor.x, cursor.y);
|
||||
|
||||
if (
|
||||
screenPos.x >= 0 &&
|
||||
screenPos.y >= 0 &&
|
||||
screenPos.x < canvas.width &&
|
||||
screenPos.y < canvas.height
|
||||
) {
|
||||
// Draw cursor
|
||||
ctx.strokeStyle = cursor.color;
|
||||
ctx.lineWidth = 2;
|
||||
ctx.beginPath();
|
||||
ctx.arc(screenPos.x + pixelSize / 2, screenPos.y + pixelSize / 2, pixelSize + 4, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
|
||||
// Draw username
|
||||
ctx.fillStyle = cursor.color;
|
||||
ctx.font = '12px sans-serif';
|
||||
ctx.fillText(cursor.username, screenPos.x, screenPos.y - 8);
|
||||
}
|
||||
}
|
||||
|
||||
// Render hover cursor indicator when zoomed in enough
|
||||
if (hoverPixel && pixelSize > 32) {
|
||||
const screenPos = canvasToScreen(hoverPixel.x, hoverPixel.y);
|
||||
|
||||
if (
|
||||
screenPos.x >= 0 &&
|
||||
screenPos.y >= 0 &&
|
||||
screenPos.x < canvas.width &&
|
||||
screenPos.y < canvas.height
|
||||
) {
|
||||
// Draw subtle pixel highlight border
|
||||
ctx.strokeStyle = 'rgba(255, 255, 255, 0.8)';
|
||||
ctx.lineWidth = 2;
|
||||
ctx.setLineDash([4, 4]);
|
||||
ctx.strokeRect(screenPos.x + 1, screenPos.y + 1, pixelSize - 2, pixelSize - 2);
|
||||
ctx.setLineDash([]); // Reset line dash
|
||||
|
||||
// Draw small corner indicators
|
||||
ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';
|
||||
const cornerSize = 3;
|
||||
// Top-left corner
|
||||
ctx.fillRect(screenPos.x, screenPos.y, cornerSize, cornerSize);
|
||||
// Top-right corner
|
||||
ctx.fillRect(screenPos.x + pixelSize - cornerSize, screenPos.y, cornerSize, cornerSize);
|
||||
// Bottom-left corner
|
||||
ctx.fillRect(screenPos.x, screenPos.y + pixelSize - cornerSize, cornerSize, cornerSize);
|
||||
// Bottom-right corner
|
||||
ctx.fillRect(screenPos.x + pixelSize - cornerSize, screenPos.y + pixelSize - cornerSize, cornerSize, cornerSize);
|
||||
}
|
||||
}
|
||||
|
||||
isDirtyRef.current = false;
|
||||
lastRenderTimeRef.current = now;
|
||||
}, [viewport, chunks, pixelSize, showGrid, userCursors, hoverPixel]);
|
||||
|
||||
// Mouse event handlers - Left click for both pixel placement and panning
|
||||
const handleMouseDown = (e: React.MouseEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (e.button === 0) { // Left click only
|
||||
isMouseDownRef.current = true;
|
||||
isPanningRef.current = false; // Reset panning state
|
||||
mouseDownPositionRef.current = { x: e.clientX, y: e.clientY };
|
||||
lastPanPointRef.current = { x: e.clientX, y: e.clientY };
|
||||
setCursorStyle('grab');
|
||||
}
|
||||
};
|
||||
|
||||
const handleMouseMove = (e: React.MouseEvent) => {
|
||||
const { x, y } = screenToCanvas(e.clientX, e.clientY);
|
||||
onCursorMove(x, y);
|
||||
|
||||
// Update hover pixel for cursor indicator
|
||||
setHoverPixel({ x, y });
|
||||
|
||||
// Get pixel info at this position and call onHoverChange
|
||||
const pixelInfo = getPixelInfo(x, y);
|
||||
onHoverChange(x, y, pixelInfo);
|
||||
|
||||
if (isMouseDownRef.current && mouseDownPositionRef.current && lastPanPointRef.current) {
|
||||
// Calculate distance from initial mouse down position
|
||||
const deltaFromStart = Math.sqrt(
|
||||
Math.pow(e.clientX - mouseDownPositionRef.current.x, 2) +
|
||||
Math.pow(e.clientY - mouseDownPositionRef.current.y, 2)
|
||||
);
|
||||
|
||||
// If moved more than threshold, start panning
|
||||
if (deltaFromStart > DRAG_THRESHOLD && !isPanningRef.current) {
|
||||
isPanningRef.current = true;
|
||||
setCursorStyle('grabbing');
|
||||
}
|
||||
|
||||
// If we're panning, update viewport
|
||||
if (isPanningRef.current) {
|
||||
const deltaX = lastPanPointRef.current.x - e.clientX;
|
||||
const deltaY = lastPanPointRef.current.y - e.clientY;
|
||||
pan(deltaX, deltaY);
|
||||
lastPanPointRef.current = { x: e.clientX, y: e.clientY };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleMouseUp = (e: React.MouseEvent) => {
|
||||
if (e.button === 0) {
|
||||
// If we weren't panning, treat as pixel click
|
||||
if (!isPanningRef.current && mouseDownPositionRef.current) {
|
||||
const { x, y } = screenToCanvas(e.clientX, e.clientY);
|
||||
onPixelClick(x, y);
|
||||
}
|
||||
|
||||
// Reset all mouse state
|
||||
isMouseDownRef.current = false;
|
||||
isPanningRef.current = false;
|
||||
lastPanPointRef.current = null;
|
||||
mouseDownPositionRef.current = null;
|
||||
setCursorStyle('crosshair');
|
||||
}
|
||||
};
|
||||
|
||||
const handleMouseLeave = () => {
|
||||
// Stop all interactions when mouse leaves canvas
|
||||
isMouseDownRef.current = false;
|
||||
isPanningRef.current = false;
|
||||
lastPanPointRef.current = null;
|
||||
mouseDownPositionRef.current = null;
|
||||
setHoverPixel(null);
|
||||
setCursorStyle('crosshair');
|
||||
};
|
||||
|
||||
const handleWheel = (e: React.WheelEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
// Get mouse position in screen coordinates (relative to canvas)
|
||||
const rect = canvasRef.current?.getBoundingClientRect();
|
||||
if (!rect) return;
|
||||
|
||||
const mouseScreenX = e.clientX - rect.left;
|
||||
const mouseScreenY = e.clientY - rect.top;
|
||||
|
||||
// Calculate world position that mouse is pointing to
|
||||
const worldX = (mouseScreenX + viewport.x) / pixelSize;
|
||||
const worldY = (mouseScreenY + viewport.y) / pixelSize;
|
||||
|
||||
// Calculate new zoom with better zoom increments
|
||||
const zoomFactor = e.deltaY > 0 ? 0.8 : 1.25;
|
||||
const newZoom = Math.max(0.1, Math.min(10.0, viewport.zoom * zoomFactor));
|
||||
|
||||
// Calculate new pixel size
|
||||
const newPixelSize = BASE_PIXEL_SIZE * newZoom;
|
||||
|
||||
// Calculate new viewport position to keep world position under cursor
|
||||
const newViewportX = worldX * newPixelSize - mouseScreenX;
|
||||
const newViewportY = worldY * newPixelSize - mouseScreenY;
|
||||
|
||||
// Update viewport with new zoom and position
|
||||
setViewport({
|
||||
zoom: newZoom,
|
||||
x: newViewportX,
|
||||
y: newViewportY,
|
||||
});
|
||||
};
|
||||
|
||||
// Resize handler
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
const container = containerRef.current;
|
||||
const canvas = canvasRef.current;
|
||||
if (!container || !canvas) return;
|
||||
|
||||
const rect = container.getBoundingClientRect();
|
||||
const devicePixelRatio = window.devicePixelRatio || 1;
|
||||
|
||||
// Set the internal size to actual resolution
|
||||
canvas.width = rect.width * devicePixelRatio;
|
||||
canvas.height = rect.height * devicePixelRatio;
|
||||
|
||||
// Scale the canvas back down using CSS
|
||||
canvas.style.width = rect.width + 'px';
|
||||
canvas.style.height = rect.height + 'px';
|
||||
|
||||
// Scale the drawing context so everything draws at the correct size
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (ctx) {
|
||||
ctx.scale(devicePixelRatio, devicePixelRatio);
|
||||
}
|
||||
|
||||
setViewport({ width: rect.width, height: rect.height });
|
||||
};
|
||||
|
||||
handleResize();
|
||||
window.addEventListener('resize', handleResize);
|
||||
return () => window.removeEventListener('resize', handleResize);
|
||||
}, [setViewport]);
|
||||
|
||||
// Mark as dirty when viewport, chunks, or other dependencies change
|
||||
useEffect(() => {
|
||||
isDirtyRef.current = true;
|
||||
}, [viewport, chunks, userCursors, hoverPixel]);
|
||||
|
||||
// Animation loop with render on demand
|
||||
useEffect(() => {
|
||||
const animate = () => {
|
||||
render();
|
||||
animationFrameRef.current = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
animationFrameRef.current = requestAnimationFrame(animate);
|
||||
|
||||
return () => {
|
||||
if (animationFrameRef.current) {
|
||||
cancelAnimationFrame(animationFrameRef.current);
|
||||
}
|
||||
};
|
||||
}, [render]);
|
||||
|
||||
// Touch event handlers for mobile
|
||||
const handleTouchStart = (e: React.TouchEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (e.touches.length === 1) {
|
||||
// Single touch - potential tap or pan
|
||||
const touch = e.touches[0];
|
||||
touchStartRef.current = {
|
||||
x: touch.clientX,
|
||||
y: touch.clientY,
|
||||
time: Date.now()
|
||||
};
|
||||
lastPanPointRef.current = { x: touch.clientX, y: touch.clientY };
|
||||
} else if (e.touches.length === 2) {
|
||||
// Two finger pinch to zoom
|
||||
const touch1 = e.touches[0];
|
||||
const touch2 = e.touches[1];
|
||||
const distance = Math.sqrt(
|
||||
Math.pow(touch1.clientX - touch2.clientX, 2) +
|
||||
Math.pow(touch1.clientY - touch2.clientY, 2)
|
||||
);
|
||||
pinchStartDistanceRef.current = distance;
|
||||
pinchStartZoomRef.current = viewport.zoom;
|
||||
|
||||
// Center point between touches for zoom
|
||||
const centerX = (touch1.clientX + touch2.clientX) / 2;
|
||||
const centerY = (touch1.clientY + touch2.clientY) / 2;
|
||||
lastPanPointRef.current = { x: centerX, y: centerY };
|
||||
}
|
||||
|
||||
lastTouchesRef.current = Array.from(e.touches) as any;
|
||||
};
|
||||
|
||||
const handleTouchMove = (e: React.TouchEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (e.touches.length === 1 && touchStartRef.current && lastPanPointRef.current) {
|
||||
// Single touch pan
|
||||
const touch = e.touches[0];
|
||||
const deltaX = lastPanPointRef.current.x - touch.clientX;
|
||||
const deltaY = lastPanPointRef.current.y - touch.clientY;
|
||||
|
||||
// Check if we've moved enough to start panning
|
||||
const totalDistance = Math.sqrt(
|
||||
Math.pow(touch.clientX - touchStartRef.current.x, 2) +
|
||||
Math.pow(touch.clientY - touchStartRef.current.y, 2)
|
||||
);
|
||||
|
||||
if (totalDistance > DRAG_THRESHOLD) {
|
||||
isPanningRef.current = true;
|
||||
|
||||
// Pan the viewport
|
||||
const newViewportX = viewport.x + deltaX;
|
||||
const newViewportY = viewport.y + deltaY;
|
||||
|
||||
setViewport({
|
||||
x: newViewportX,
|
||||
y: newViewportY,
|
||||
});
|
||||
|
||||
lastPanPointRef.current = { x: touch.clientX, y: touch.clientY };
|
||||
}
|
||||
|
||||
// Update hover for single touch
|
||||
const { x, y } = screenToCanvas(touch.clientX, touch.clientY);
|
||||
setHoverPixel({ x, y });
|
||||
const pixelInfo = getPixelInfo(x, y);
|
||||
onHoverChange(x, y, pixelInfo);
|
||||
|
||||
} else if (e.touches.length === 2 && pinchStartDistanceRef.current && pinchStartZoomRef.current) {
|
||||
// Two finger pinch zoom
|
||||
const touch1 = e.touches[0];
|
||||
const touch2 = e.touches[1];
|
||||
const currentDistance = Math.sqrt(
|
||||
Math.pow(touch1.clientX - touch2.clientX, 2) +
|
||||
Math.pow(touch1.clientY - touch2.clientY, 2)
|
||||
);
|
||||
|
||||
const scale = currentDistance / pinchStartDistanceRef.current;
|
||||
const newZoom = Math.max(0.1, Math.min(5.0, pinchStartZoomRef.current * scale));
|
||||
|
||||
// Zoom towards center of pinch
|
||||
const rect = canvasRef.current?.getBoundingClientRect();
|
||||
if (rect) {
|
||||
const centerX = (touch1.clientX + touch2.clientX) / 2 - rect.left;
|
||||
const centerY = (touch1.clientY + touch2.clientY) / 2 - rect.top;
|
||||
|
||||
const worldX = (centerX + viewport.x) / pixelSize;
|
||||
const worldY = (centerY + viewport.y) / pixelSize;
|
||||
|
||||
const newPixelSize = BASE_PIXEL_SIZE * newZoom;
|
||||
const newViewportX = worldX * newPixelSize - centerX;
|
||||
const newViewportY = worldY * newPixelSize - centerY;
|
||||
|
||||
setViewport({
|
||||
zoom: newZoom,
|
||||
x: newViewportX,
|
||||
y: newViewportY,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleTouchEnd = (e: React.TouchEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (e.touches.length === 0) {
|
||||
// All touches ended
|
||||
if (touchStartRef.current && !isPanningRef.current) {
|
||||
// This was a tap, not a pan
|
||||
const timeDiff = Date.now() - touchStartRef.current.time;
|
||||
if (timeDiff < 300) { // Quick tap
|
||||
const { x, y } = screenToCanvas(touchStartRef.current.x, touchStartRef.current.y);
|
||||
onPixelClick(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset touch state
|
||||
touchStartRef.current = null;
|
||||
isPanningRef.current = false;
|
||||
lastPanPointRef.current = null;
|
||||
pinchStartDistanceRef.current = null;
|
||||
pinchStartZoomRef.current = null;
|
||||
}
|
||||
|
||||
lastTouchesRef.current = Array.from(e.touches) as any;
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className="fixed inset-0 w-full h-full"
|
||||
style={{
|
||||
cursor: cursorStyle,
|
||||
background: `
|
||||
linear-gradient(135deg,
|
||||
rgba(15, 23, 42, 0.95) 0%,
|
||||
rgba(30, 41, 59, 0.9) 25%,
|
||||
rgba(51, 65, 85, 0.85) 50%,
|
||||
rgba(30, 58, 138, 0.8) 75%,
|
||||
rgba(29, 78, 216, 0.75) 100%
|
||||
),
|
||||
radial-gradient(circle at 20% 20%, rgba(99, 102, 241, 0.15) 0%, transparent 40%),
|
||||
radial-gradient(circle at 80% 80%, rgba(59, 130, 246, 0.12) 0%, transparent 40%),
|
||||
radial-gradient(circle at 40% 70%, rgba(147, 51, 234, 0.1) 0%, transparent 40%),
|
||||
radial-gradient(circle at 70% 30%, rgba(16, 185, 129, 0.08) 0%, transparent 40%)
|
||||
`,
|
||||
backgroundAttachment: 'fixed'
|
||||
}}
|
||||
>
|
||||
<canvas
|
||||
ref={canvasRef}
|
||||
onMouseDown={handleMouseDown}
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseUp={handleMouseUp}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
onWheel={handleWheel}
|
||||
onTouchStart={handleTouchStart}
|
||||
onTouchMove={handleTouchMove}
|
||||
onTouchEnd={handleTouchEnd}
|
||||
onContextMenu={(e) => e.preventDefault()}
|
||||
className="w-full h-full touch-none"
|
||||
style={{ touchAction: 'none' }}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
131
frontend/src/components/ui/ColorPalette.tsx
Normal file
131
frontend/src/components/ui/ColorPalette.tsx
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { motion } from 'framer-motion';
|
||||
|
||||
const PIXEL_COLORS = [
|
||||
// Primary colors
|
||||
'#FF0000', // Bright Red
|
||||
'#00FF00', // Bright Green
|
||||
'#0000FF', // Bright Blue
|
||||
'#FFFF00', // Bright Yellow
|
||||
|
||||
// Secondary colors
|
||||
'#FF8C00', // Dark Orange
|
||||
'#FF69B4', // Hot Pink
|
||||
'#9400D3', // Violet
|
||||
'#00CED1', // Dark Turquoise
|
||||
|
||||
// Earth tones
|
||||
'#8B4513', // Saddle Brown
|
||||
'#228B22', // Forest Green
|
||||
'#B22222', // Fire Brick
|
||||
'#4682B4', // Steel Blue
|
||||
|
||||
// Grays and basics
|
||||
'#000000', // Black
|
||||
'#FFFFFF', // White
|
||||
'#808080', // Gray
|
||||
'#C0C0C0', // Silver
|
||||
|
||||
// Pastels
|
||||
'#FFB6C1', // Light Pink
|
||||
'#87CEEB', // Sky Blue
|
||||
'#98FB98', // Pale Green
|
||||
'#F0E68C', // Khaki
|
||||
|
||||
// Additional vibrant colors
|
||||
'#FF1493', // Deep Pink
|
||||
'#00BFFF', // Deep Sky Blue
|
||||
'#32CD32', // Lime Green
|
||||
'#FF4500', // Orange Red
|
||||
];
|
||||
|
||||
interface ColorPaletteProps {
|
||||
selectedColor: string;
|
||||
onColorSelect: (color: string) => void;
|
||||
}
|
||||
|
||||
export function ColorPalette({ selectedColor, onColorSelect }: ColorPaletteProps) {
|
||||
const [isExpanded, setIsExpanded] = useState(false);
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
className="fixed bottom-4 md:bottom-6 left-4 md:left-6 z-50 max-w-xs"
|
||||
initial={{ opacity: 0, y: 50 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.3 }}
|
||||
>
|
||||
<div className="bg-gradient-to-br from-gray-900/95 to-black/90 backdrop-blur-xl rounded-2xl p-4 border border-white/10 shadow-2xl">
|
||||
<div className="flex items-center gap-3 mb-3">
|
||||
<motion.div
|
||||
className="w-10 h-10 rounded-xl border-2 border-white/20 cursor-pointer shadow-lg overflow-hidden"
|
||||
style={{ backgroundColor: selectedColor }}
|
||||
onClick={() => setIsExpanded(!isExpanded)}
|
||||
whileHover={{ scale: 1.05 }}
|
||||
whileTap={{ scale: 0.95 }}
|
||||
>
|
||||
{selectedColor === '#FFFFFF' && (
|
||||
<div className="w-full h-full bg-white border border-gray-300" />
|
||||
)}
|
||||
</motion.div>
|
||||
<div className="flex flex-col">
|
||||
<span className="text-white/90 font-semibold text-sm">Color Palette</span>
|
||||
<span className="text-white/60 text-xs">{selectedColor.toUpperCase()}</span>
|
||||
</div>
|
||||
<motion.div
|
||||
className="ml-auto text-white/60"
|
||||
animate={{ rotate: isExpanded ? 180 : 0 }}
|
||||
transition={{ duration: 0.2 }}
|
||||
>
|
||||
▼
|
||||
</motion.div>
|
||||
</div>
|
||||
|
||||
<motion.div
|
||||
className="grid grid-cols-6 gap-2"
|
||||
initial={{ height: 0, opacity: 0 }}
|
||||
animate={{
|
||||
height: isExpanded ? 'auto' : 0,
|
||||
opacity: isExpanded ? 1 : 0
|
||||
}}
|
||||
transition={{ duration: 0.3, ease: "easeInOut" }}
|
||||
style={{ overflow: 'hidden' }}
|
||||
>
|
||||
{PIXEL_COLORS.map((color, index) => (
|
||||
<motion.button
|
||||
key={color}
|
||||
className={`w-8 h-8 rounded-lg border-2 transition-all duration-200 relative overflow-hidden ${
|
||||
selectedColor === color
|
||||
? 'border-white/80 shadow-lg ring-2 ring-white/30'
|
||||
: 'border-white/20 hover:border-white/50 hover:shadow-md'
|
||||
}`}
|
||||
style={{ backgroundColor: color }}
|
||||
onClick={() => {
|
||||
onColorSelect(color);
|
||||
setIsExpanded(false);
|
||||
}}
|
||||
whileHover={{ scale: 1.1, y: -2 }}
|
||||
whileTap={{ scale: 0.9 }}
|
||||
initial={{ opacity: 0, scale: 0.8 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
transition={{ delay: index * 0.02 }}
|
||||
>
|
||||
{color === '#FFFFFF' && (
|
||||
<div className="absolute inset-0 bg-white border border-gray-300" />
|
||||
)}
|
||||
{selectedColor === color && (
|
||||
<motion.div
|
||||
className="absolute inset-0 bg-white/20 rounded-md"
|
||||
initial={{ scale: 0 }}
|
||||
animate={{ scale: 1 }}
|
||||
transition={{ duration: 0.2 }}
|
||||
/>
|
||||
)}
|
||||
</motion.button>
|
||||
))}
|
||||
</motion.div>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
100
frontend/src/components/ui/ColorPicker.tsx
Normal file
100
frontend/src/components/ui/ColorPicker.tsx
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useCanvasStore } from '../../store/canvasStore';
|
||||
import { COLORS } from '@gaplace/shared';
|
||||
|
||||
export function ColorPicker() {
|
||||
const { selectedColor, setSelectedColor } = useCanvasStore();
|
||||
const [customColor, setCustomColor] = useState('#000000');
|
||||
|
||||
const handleColorSelect = (color: string) => {
|
||||
setSelectedColor(color);
|
||||
};
|
||||
|
||||
const handleCustomColorChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const color = e.target.value;
|
||||
setCustomColor(color);
|
||||
setSelectedColor(color);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-4">
|
||||
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
|
||||
Color Palette
|
||||
</h3>
|
||||
|
||||
{/* Current color display */}
|
||||
<div className="mb-4">
|
||||
<div className="flex items-center space-x-3">
|
||||
<div
|
||||
className="w-12 h-12 rounded-lg border-2 border-gray-300 dark:border-gray-600 shadow-inner"
|
||||
style={{ backgroundColor: selectedColor }}
|
||||
/>
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">
|
||||
Current: {selectedColor.toUpperCase()}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Predefined colors */}
|
||||
<div className="grid grid-cols-6 gap-2 mb-4">
|
||||
{COLORS.PALETTE.map((color, index) => (
|
||||
<button
|
||||
key={index}
|
||||
className={`color-picker-button ${
|
||||
selectedColor === color ? 'selected' : ''
|
||||
}`}
|
||||
style={{ backgroundColor: color }}
|
||||
onClick={() => handleColorSelect(color)}
|
||||
title={color}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Custom color picker */}
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Custom Color
|
||||
</label>
|
||||
<div className="flex items-center space-x-2">
|
||||
<input
|
||||
type="color"
|
||||
value={customColor}
|
||||
onChange={handleCustomColorChange}
|
||||
className="w-10 h-10 rounded border border-gray-300 dark:border-gray-600 cursor-pointer"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={customColor}
|
||||
onChange={(e) => {
|
||||
const color = e.target.value;
|
||||
if (/^#[0-9A-Fa-f]{6}$/.test(color)) {
|
||||
setCustomColor(color);
|
||||
setSelectedColor(color);
|
||||
}
|
||||
}}
|
||||
className="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 text-gray-900 dark:text-white text-sm"
|
||||
placeholder="#000000"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Recent colors */}
|
||||
<div className="mt-4">
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
Recent Colors
|
||||
</label>
|
||||
<div className="grid grid-cols-8 gap-1">
|
||||
{/* TODO: Implement recent colors storage */}
|
||||
{Array.from({ length: 8 }, (_, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className="w-6 h-6 rounded bg-gray-200 dark:bg-gray-600"
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
80
frontend/src/components/ui/CooldownTimer.tsx
Normal file
80
frontend/src/components/ui/CooldownTimer.tsx
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { motion } from 'framer-motion';
|
||||
|
||||
interface CooldownTimerProps {
|
||||
isActive: boolean;
|
||||
duration: number; // in seconds
|
||||
onComplete: () => void;
|
||||
}
|
||||
|
||||
export function CooldownTimer({ isActive, duration, onComplete }: CooldownTimerProps) {
|
||||
const [timeLeft, setTimeLeft] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (isActive) {
|
||||
setTimeLeft(duration);
|
||||
const interval = setInterval(() => {
|
||||
setTimeLeft((prev) => {
|
||||
if (prev <= 1) {
|
||||
clearInterval(interval);
|
||||
// Use setTimeout to avoid setState during render
|
||||
setTimeout(() => onComplete(), 0);
|
||||
return 0;
|
||||
}
|
||||
return prev - 1;
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}
|
||||
}, [isActive, duration, onComplete]);
|
||||
|
||||
if (!isActive || timeLeft === 0) return null;
|
||||
|
||||
const progress = ((duration - timeLeft) / duration) * 100;
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
className="fixed top-16 sm:top-20 left-1/2 transform -translate-x-1/2 z-50"
|
||||
initial={{ opacity: 0, scale: 0.8 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
exit={{ opacity: 0, scale: 0.8 }}
|
||||
transition={{
|
||||
type: "spring",
|
||||
stiffness: 400,
|
||||
damping: 25
|
||||
}}
|
||||
>
|
||||
<motion.div
|
||||
className="bg-white/5 backdrop-blur-2xl rounded-full px-4 sm:px-6 py-2 sm:py-3 border border-white/20 shadow-lg ring-1 ring-white/10"
|
||||
whileHover={{
|
||||
scale: 1.05
|
||||
}}
|
||||
transition={{ type: "spring", stiffness: 500, damping: 25 }}
|
||||
>
|
||||
<div className="flex items-center gap-3 sm:gap-4">
|
||||
<motion.div
|
||||
className="text-white font-medium text-sm"
|
||||
animate={{
|
||||
color: timeLeft <= 3 ? "#f87171" : "#ffffff"
|
||||
}}
|
||||
transition={{ duration: 0.3 }}
|
||||
>
|
||||
{timeLeft}s
|
||||
</motion.div>
|
||||
|
||||
<div className="w-16 sm:w-24 h-2 bg-white/20 rounded-full overflow-hidden">
|
||||
<motion.div
|
||||
className="h-full bg-gradient-to-r from-cyan-400 to-blue-500 rounded-full"
|
||||
initial={{ width: 0 }}
|
||||
animate={{ width: `${progress}%` }}
|
||||
transition={{ duration: 0.5, ease: "easeOut" }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
76
frontend/src/components/ui/CoordinateDisplay.tsx
Normal file
76
frontend/src/components/ui/CoordinateDisplay.tsx
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
'use client';
|
||||
|
||||
import { motion } from 'framer-motion';
|
||||
|
||||
interface CoordinateDisplayProps {
|
||||
x: number;
|
||||
y: number;
|
||||
pixelColor?: string | null;
|
||||
pixelOwner?: string | null;
|
||||
zoom: number;
|
||||
}
|
||||
|
||||
export function CoordinateDisplay({
|
||||
x,
|
||||
y,
|
||||
pixelColor,
|
||||
pixelOwner,
|
||||
zoom
|
||||
}: CoordinateDisplayProps) {
|
||||
return (
|
||||
<motion.div
|
||||
className="fixed bottom-4 left-4 sm:bottom-6 sm:left-6 z-50 max-w-[calc(100vw-120px)] sm:max-w-none"
|
||||
initial={{ opacity: 0, y: 30 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0, y: 30 }}
|
||||
transition={{
|
||||
type: "spring",
|
||||
stiffness: 400,
|
||||
damping: 25
|
||||
}}
|
||||
>
|
||||
<motion.div
|
||||
className="bg-white/5 backdrop-blur-2xl rounded-xl sm:rounded-2xl px-3 sm:px-4 py-2 sm:py-3 border border-white/20 shadow-lg ring-1 ring-white/10"
|
||||
whileHover={{
|
||||
scale: 1.02
|
||||
}}
|
||||
transition={{ type: "spring", stiffness: 400, damping: 25 }}
|
||||
>
|
||||
<div className="space-y-1 sm:space-y-2 text-xs sm:text-sm">
|
||||
{/* Coordinates */}
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-white/70 font-medium">Pos:</span>
|
||||
<span className="text-white font-mono font-bold">
|
||||
({x}, {y})
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Pixel info */}
|
||||
{pixelColor && (
|
||||
<>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-white/70 font-medium">Pixel:</span>
|
||||
<div
|
||||
className="w-3 h-3 sm:w-4 sm:h-4 rounded border border-white/30"
|
||||
style={{ backgroundColor: pixelColor }}
|
||||
/>
|
||||
<span className="text-white font-mono text-xs">
|
||||
{pixelColor.toUpperCase()}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{pixelOwner && (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-white/70 font-medium">By:</span>
|
||||
<span className="text-blue-300 font-medium text-xs max-w-[80px] sm:max-w-none truncate">
|
||||
{pixelOwner}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
204
frontend/src/components/ui/PixelConfirmModal.tsx
Normal file
204
frontend/src/components/ui/PixelConfirmModal.tsx
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { motion, AnimatePresence } from 'framer-motion';
|
||||
|
||||
const PIXEL_COLORS = [
|
||||
'#FF0000', // Red
|
||||
'#00FF00', // Green
|
||||
'#0000FF', // Blue
|
||||
'#FFFF00', // Yellow
|
||||
'#FF00FF', // Magenta
|
||||
'#00FFFF', // Cyan
|
||||
'#FFA500', // Orange
|
||||
'#800080', // Purple
|
||||
'#FFC0CB', // Pink
|
||||
'#A52A2A', // Brown
|
||||
'#808080', // Gray
|
||||
'#000000', // Black
|
||||
'#FFFFFF', // White
|
||||
'#90EE90', // Light Green
|
||||
'#FFB6C1', // Light Pink
|
||||
'#87CEEB', // Sky Blue
|
||||
];
|
||||
|
||||
interface PixelConfirmModalProps {
|
||||
isOpen: boolean;
|
||||
x: number;
|
||||
y: number;
|
||||
color: string;
|
||||
onColorChange: (color: string) => void;
|
||||
onConfirm: () => void;
|
||||
onCancel: () => void;
|
||||
}
|
||||
|
||||
export function PixelConfirmModal({
|
||||
isOpen,
|
||||
x,
|
||||
y,
|
||||
color,
|
||||
onColorChange,
|
||||
onConfirm,
|
||||
onCancel
|
||||
}: PixelConfirmModalProps) {
|
||||
const [selectedLocalColor, setSelectedLocalColor] = useState(color);
|
||||
|
||||
const handleColorSelect = (newColor: string) => {
|
||||
setSelectedLocalColor(newColor);
|
||||
onColorChange(newColor);
|
||||
};
|
||||
|
||||
const handleConfirm = () => {
|
||||
onColorChange(selectedLocalColor);
|
||||
onConfirm();
|
||||
};
|
||||
|
||||
return (
|
||||
<AnimatePresence>
|
||||
{isOpen && (
|
||||
<>
|
||||
{/* Backdrop */}
|
||||
<motion.div
|
||||
className="fixed inset-0 bg-black/30 backdrop-blur-md z-50"
|
||||
initial={{ opacity: 0, backdropFilter: "blur(0px)" }}
|
||||
animate={{ opacity: 1, backdropFilter: "blur(16px)" }}
|
||||
exit={{ opacity: 0, backdropFilter: "blur(0px)" }}
|
||||
transition={{ duration: 0.4, ease: "easeOut" }}
|
||||
onClick={onCancel}
|
||||
/>
|
||||
|
||||
{/* Modal */}
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 sm:p-6">
|
||||
<motion.div
|
||||
className="w-full max-w-[350px] sm:max-w-[400px]"
|
||||
initial={{ opacity: 0, scale: 0.8, y: 40, rotateX: 10 }}
|
||||
animate={{ opacity: 1, scale: 1, y: 0, rotateX: 0 }}
|
||||
exit={{ opacity: 0, scale: 0.8, y: 40, rotateX: 10 }}
|
||||
transition={{
|
||||
type: "spring",
|
||||
stiffness: 400,
|
||||
damping: 30,
|
||||
opacity: { duration: 0.3 }
|
||||
}}
|
||||
>
|
||||
<motion.div
|
||||
className="bg-white/5 backdrop-blur-2xl rounded-2xl sm:rounded-3xl p-4 sm:p-6 md:p-8 border border-white/20 shadow-2xl ring-1 ring-white/10 relative overflow-hidden mx-auto"
|
||||
whileHover={{
|
||||
boxShadow: "0 20px 40px rgba(0,0,0,0.2)"
|
||||
}}
|
||||
transition={{ type: "spring", stiffness: 400, damping: 25 }}
|
||||
>
|
||||
{/* Flowing glass background effect */}
|
||||
<motion.div
|
||||
className="absolute inset-0 bg-gradient-to-br from-white/10 via-transparent to-white/5 rounded-3xl"
|
||||
animate={{
|
||||
background: [
|
||||
"linear-gradient(45deg, rgba(255,255,255,0.1) 0%, transparent 50%, rgba(255,255,255,0.05) 100%)",
|
||||
"linear-gradient(225deg, rgba(255,255,255,0.1) 0%, transparent 50%, rgba(255,255,255,0.05) 100%)",
|
||||
"linear-gradient(45deg, rgba(255,255,255,0.1) 0%, transparent 50%, rgba(255,255,255,0.05) 100%)"
|
||||
]
|
||||
}}
|
||||
transition={{ duration: 8, repeat: Infinity, ease: "linear" }}
|
||||
/>
|
||||
<div className="text-center relative z-10">
|
||||
<motion.h3
|
||||
className="text-white text-xl font-bold mb-2"
|
||||
initial={{ opacity: 0, y: -10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.1 }}
|
||||
>
|
||||
Place Pixel
|
||||
</motion.h3>
|
||||
|
||||
<motion.div
|
||||
className="flex items-center justify-center gap-4 mb-6"
|
||||
initial={{ opacity: 0, y: -10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.15 }}
|
||||
>
|
||||
<div className="text-white/70 font-mono">
|
||||
Position: ({x}, {y})
|
||||
</div>
|
||||
<motion.div
|
||||
className="w-10 h-10 rounded-xl border-2 border-white/30 shadow-xl ring-1 ring-white/20"
|
||||
style={{ backgroundColor: selectedLocalColor }}
|
||||
whileHover={{ scale: 1.1 }}
|
||||
transition={{ type: "spring", stiffness: 400 }}
|
||||
/>
|
||||
</motion.div>
|
||||
|
||||
{/* Color Palette */}
|
||||
<motion.div
|
||||
className="mb-8"
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.2 }}
|
||||
>
|
||||
<h4 className="text-white/90 text-sm font-medium mb-4">Choose Color</h4>
|
||||
<div className="grid grid-cols-6 sm:grid-cols-8 gap-3 sm:gap-4 max-w-xs sm:max-w-sm mx-auto">
|
||||
{PIXEL_COLORS.map((paletteColor, index) => (
|
||||
<motion.button
|
||||
key={paletteColor}
|
||||
className={`w-9 h-9 sm:w-10 sm:h-10 rounded-lg border-2 transition-all duration-200 ring-1 ring-white/10 touch-manipulation min-h-[36px] min-w-[36px] ${
|
||||
selectedLocalColor === paletteColor
|
||||
? 'border-white/80 scale-110 shadow-xl ring-white/30'
|
||||
: 'border-white/30 hover:border-white/60 hover:ring-white/20'
|
||||
}`}
|
||||
style={{ backgroundColor: paletteColor }}
|
||||
onClick={() => handleColorSelect(paletteColor)}
|
||||
whileHover={{
|
||||
scale: 1.1,
|
||||
boxShadow: "0 4px 15px rgba(0,0,0,0.3)"
|
||||
}}
|
||||
whileTap={{ scale: 0.95 }}
|
||||
initial={{ opacity: 0, scale: 0.8 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
transition={{
|
||||
delay: 0.1 + index * 0.01,
|
||||
type: "spring",
|
||||
stiffness: 500,
|
||||
damping: 20
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
<motion.div
|
||||
className="flex gap-4"
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.3 }}
|
||||
>
|
||||
<motion.button
|
||||
className="flex-1 px-4 sm:px-6 py-4 sm:py-3 bg-white/5 backdrop-blur-xl text-white rounded-xl border border-white/20 hover:bg-white/10 active:bg-white/15 transition-all duration-200 font-medium ring-1 ring-white/10 relative overflow-hidden touch-manipulation min-h-[48px] text-sm sm:text-base"
|
||||
onClick={onCancel}
|
||||
whileHover={{
|
||||
backgroundColor: "rgba(255,255,255,0.15)"
|
||||
}}
|
||||
whileTap={{ scale: 0.98 }}
|
||||
transition={{ type: "spring", stiffness: 400, damping: 25 }}
|
||||
>
|
||||
Cancel
|
||||
</motion.button>
|
||||
<motion.button
|
||||
className="flex-1 px-4 sm:px-6 py-4 sm:py-3 bg-gradient-to-r from-blue-500/80 to-purple-600/80 backdrop-blur-xl text-white rounded-xl hover:from-blue-600/90 hover:to-purple-700/90 active:from-blue-700/90 active:to-purple-800/90 transition-all duration-200 font-medium shadow-xl ring-1 ring-white/20 relative overflow-hidden touch-manipulation min-h-[48px] text-sm sm:text-base"
|
||||
onClick={handleConfirm}
|
||||
whileHover={{
|
||||
boxShadow: "0 10px 25px rgba(0,0,0,0.4)"
|
||||
}}
|
||||
whileTap={{ scale: 0.98 }}
|
||||
transition={{ type: "spring", stiffness: 400, damping: 25 }}
|
||||
>
|
||||
Place Pixel
|
||||
</motion.button>
|
||||
</motion.div>
|
||||
</div>
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
);
|
||||
}
|
||||
43
frontend/src/components/ui/SettingsButton.tsx
Normal file
43
frontend/src/components/ui/SettingsButton.tsx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
'use client';
|
||||
|
||||
import { motion } from 'framer-motion';
|
||||
|
||||
interface SettingsButtonProps {
|
||||
username: string;
|
||||
onOpenSettings: () => void;
|
||||
}
|
||||
|
||||
export function SettingsButton({ username, onOpenSettings }: SettingsButtonProps) {
|
||||
return (
|
||||
<motion.div
|
||||
className="fixed top-4 right-4 sm:top-6 sm:right-6 z-40"
|
||||
initial={{ opacity: 0, x: 30 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
transition={{
|
||||
type: "spring",
|
||||
stiffness: 400,
|
||||
damping: 25,
|
||||
delay: 0.2
|
||||
}}
|
||||
>
|
||||
<motion.button
|
||||
className="bg-white/5 backdrop-blur-2xl rounded-full pl-3 pr-4 sm:pl-4 sm:pr-6 py-2 sm:py-3 border border-white/20 shadow-lg hover:bg-white/10 active:bg-white/15 transition-all duration-200 ring-1 ring-white/10 touch-manipulation"
|
||||
onClick={onOpenSettings}
|
||||
whileHover={{
|
||||
scale: 1.05,
|
||||
boxShadow: "0 8px 25px rgba(0,0,0,0.3)"
|
||||
}}
|
||||
whileTap={{ scale: 0.95 }}
|
||||
>
|
||||
<div className="flex items-center gap-2 sm:gap-3">
|
||||
<div className="w-6 h-6 sm:w-8 sm:h-8 bg-gradient-to-br from-blue-500 to-purple-600 rounded-full flex items-center justify-center text-white font-bold text-xs sm:text-sm">
|
||||
{username.charAt(0).toUpperCase()}
|
||||
</div>
|
||||
<span className="text-white font-medium text-xs sm:text-sm max-w-[80px] sm:max-w-none truncate">
|
||||
{username}
|
||||
</span>
|
||||
</div>
|
||||
</motion.button>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
69
frontend/src/components/ui/StatsOverlay.tsx
Normal file
69
frontend/src/components/ui/StatsOverlay.tsx
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
'use client';
|
||||
|
||||
import { motion } from 'framer-motion';
|
||||
|
||||
interface StatsOverlayProps {
|
||||
onlineUsers: number;
|
||||
totalPixels: number;
|
||||
zoom: number;
|
||||
}
|
||||
|
||||
export function StatsOverlay({ onlineUsers, totalPixels, zoom }: StatsOverlayProps) {
|
||||
return (
|
||||
<motion.div
|
||||
className="fixed top-4 left-4 sm:top-6 sm:left-6 z-50"
|
||||
initial={{ opacity: 0, x: -30 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
transition={{
|
||||
type: "spring",
|
||||
stiffness: 400,
|
||||
damping: 25
|
||||
}}
|
||||
>
|
||||
<motion.div
|
||||
className="bg-white/5 backdrop-blur-2xl rounded-xl sm:rounded-2xl p-3 sm:p-4 border border-white/20 shadow-lg ring-1 ring-white/10"
|
||||
whileHover={{
|
||||
scale: 1.05,
|
||||
boxShadow: "0 10px 25px rgba(0,0,0,0.3)"
|
||||
}}
|
||||
transition={{ type: "spring", stiffness: 400, damping: 25 }}
|
||||
>
|
||||
<div className="space-y-2 sm:space-y-3">
|
||||
<motion.div
|
||||
className="flex items-center gap-2 sm:gap-3"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
transition={{ delay: 0.1 }}
|
||||
>
|
||||
<motion.div
|
||||
className="w-2.5 h-2.5 sm:w-3 sm:h-3 bg-green-400 rounded-full"
|
||||
animate={{
|
||||
scale: [1, 1.3, 1]
|
||||
}}
|
||||
transition={{
|
||||
duration: 2,
|
||||
repeat: Infinity,
|
||||
ease: "easeInOut"
|
||||
}}
|
||||
/>
|
||||
<span className="text-white font-medium text-xs sm:text-sm">
|
||||
{onlineUsers} online
|
||||
</span>
|
||||
</motion.div>
|
||||
|
||||
<motion.div
|
||||
className="flex items-center gap-2 sm:gap-3"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
transition={{ delay: 0.2 }}
|
||||
>
|
||||
<div className="w-2.5 h-2.5 sm:w-3 sm:h-3 bg-blue-400 rounded-full" />
|
||||
<span className="text-white font-medium text-xs sm:text-sm">
|
||||
{totalPixels.toLocaleString()} pixels
|
||||
</span>
|
||||
</motion.div>
|
||||
</div>
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
148
frontend/src/components/ui/StatusBar.tsx
Normal file
148
frontend/src/components/ui/StatusBar.tsx
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
'use client';
|
||||
|
||||
import { useCanvasStore } from '../../store/canvasStore';
|
||||
import { motion, AnimatePresence } from 'framer-motion';
|
||||
|
||||
export function StatusBar() {
|
||||
const {
|
||||
totalPixels,
|
||||
userPixels,
|
||||
activeUsers,
|
||||
viewport,
|
||||
} = useCanvasStore();
|
||||
|
||||
const formatNumber = (num: number) => {
|
||||
if (num >= 1000000) {
|
||||
return `${(num / 1000000).toFixed(1)}M`;
|
||||
}
|
||||
if (num >= 1000) {
|
||||
return `${(num / 1000).toFixed(1)}K`;
|
||||
}
|
||||
return num.toString();
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Statistics Card */}
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-4">
|
||||
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
|
||||
Statistics
|
||||
</h3>
|
||||
|
||||
<div className="space-y-3">
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-sm text-gray-600 dark:text-gray-400">Total Pixels:</span>
|
||||
<motion.span
|
||||
className="text-sm font-medium text-gray-900 dark:text-white"
|
||||
key={totalPixels}
|
||||
initial={{ scale: 1.2 }}
|
||||
animate={{ scale: 1 }}
|
||||
transition={{ duration: 0.2 }}
|
||||
>
|
||||
{formatNumber(totalPixels)}
|
||||
</motion.span>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-sm text-gray-600 dark:text-gray-400">Your Pixels:</span>
|
||||
<motion.span
|
||||
className="text-sm font-medium text-blue-600 dark:text-blue-400"
|
||||
key={userPixels}
|
||||
initial={{ scale: 1.2 }}
|
||||
animate={{ scale: 1 }}
|
||||
transition={{ duration: 0.2 }}
|
||||
>
|
||||
{formatNumber(userPixels)}
|
||||
</motion.span>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-sm text-gray-600 dark:text-gray-400">Zoom:</span>
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{Math.round(viewport.zoom * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-sm text-gray-600 dark:text-gray-400">Position:</span>
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
({Math.round(viewport.x)}, {Math.round(viewport.y)})
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Active Users Card */}
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-4">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white">
|
||||
Active Users
|
||||
</h3>
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="w-2 h-2 bg-green-400 rounded-full animate-pulse" />
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{activeUsers.length}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2 max-h-32 overflow-y-auto scrollbar-hide">
|
||||
<AnimatePresence>
|
||||
{activeUsers.map((userId, index) => (
|
||||
<motion.div
|
||||
key={userId}
|
||||
initial={{ opacity: 0, x: -20 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: 20 }}
|
||||
transition={{ duration: 0.2, delay: index * 0.05 }}
|
||||
className="flex items-center space-x-2 py-1"
|
||||
>
|
||||
<div
|
||||
className="w-3 h-3 rounded-full"
|
||||
style={{
|
||||
backgroundColor: `hsl(${userId.slice(-6).split('').reduce((a, c) => a + c.charCodeAt(0), 0) % 360}, 70%, 50%)`
|
||||
}}
|
||||
/>
|
||||
<span className="text-sm text-gray-600 dark:text-gray-400 truncate">
|
||||
{userId.startsWith('Guest_') ? userId : `User ${userId.slice(0, 8)}...`}
|
||||
</span>
|
||||
</motion.div>
|
||||
))}
|
||||
</AnimatePresence>
|
||||
|
||||
{activeUsers.length === 0 && (
|
||||
<div className="text-sm text-gray-500 dark:text-gray-500 text-center py-2">
|
||||
No other users online
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Help Card */}
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-4">
|
||||
<h3 className="text-lg font-semibold mb-3 text-gray-900 dark:text-white">
|
||||
Controls
|
||||
</h3>
|
||||
|
||||
<div className="space-y-2 text-sm text-gray-600 dark:text-gray-400">
|
||||
<div className="flex justify-between">
|
||||
<span>Place pixel:</span>
|
||||
<span className="font-mono">Click</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span>Pan canvas:</span>
|
||||
<span className="font-mono">Ctrl + Drag</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span>Zoom:</span>
|
||||
<span className="font-mono">Mouse wheel</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span>Reset view:</span>
|
||||
<span className="font-mono">🏠 button</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
146
frontend/src/components/ui/Toolbar.tsx
Normal file
146
frontend/src/components/ui/Toolbar.tsx
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
'use client';
|
||||
|
||||
import { useCanvasStore } from '../../store/canvasStore';
|
||||
import { useTheme } from '../ThemeProvider';
|
||||
|
||||
export function Toolbar() {
|
||||
const {
|
||||
selectedTool,
|
||||
setSelectedTool,
|
||||
brushSize,
|
||||
setBrushSize,
|
||||
showGrid,
|
||||
showCursors,
|
||||
viewport,
|
||||
setZoom,
|
||||
setViewport,
|
||||
} = useCanvasStore();
|
||||
|
||||
const { theme, setTheme } = useTheme();
|
||||
|
||||
const tools = [
|
||||
{ id: 'pixel', name: 'Pixel', icon: '🖊️' },
|
||||
{ id: 'fill', name: 'Fill', icon: '🪣' },
|
||||
{ id: 'eyedropper', name: 'Eyedropper', icon: '💉' },
|
||||
] as const;
|
||||
|
||||
const handleZoomIn = () => {
|
||||
setZoom(viewport.zoom * 1.2);
|
||||
};
|
||||
|
||||
const handleZoomOut = () => {
|
||||
setZoom(viewport.zoom * 0.8);
|
||||
};
|
||||
|
||||
const handleResetView = () => {
|
||||
setViewport({ x: 0, y: 0, zoom: 1 });
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed top-6 left-1/2 transform -translate-x-1/2 z-40 flex flex-wrap items-center gap-2 md:gap-3 bg-gradient-to-r from-gray-900/95 to-black/90 backdrop-blur-xl rounded-2xl p-2 md:p-4 border border-white/10 shadow-2xl max-w-[calc(100vw-2rem)]">
|
||||
{/* Tools */}
|
||||
<div className="flex items-center gap-1 bg-white/5 rounded-xl p-1 border border-white/10">
|
||||
{tools.map((tool) => (
|
||||
<button
|
||||
key={tool.id}
|
||||
className={`px-2 md:px-3 py-1 md:py-2 rounded-lg text-xs md:text-sm font-medium transition-all duration-200 ${
|
||||
selectedTool === tool.id
|
||||
? 'bg-blue-500 text-white shadow-lg'
|
||||
: 'text-white/70 hover:text-white hover:bg-white/10'
|
||||
}`}
|
||||
onClick={() => setSelectedTool(tool.id)}
|
||||
title={tool.name}
|
||||
>
|
||||
<span className="text-base md:mr-1">{tool.icon}</span>
|
||||
<span className="hidden md:inline">{tool.name}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Brush size (only for pixel tool) */}
|
||||
{selectedTool === 'pixel' && (
|
||||
<div className="flex items-center gap-3 bg-white/5 rounded-xl p-3 border border-white/10">
|
||||
<span className="text-sm text-white/80 font-medium">Size:</span>
|
||||
<input
|
||||
type="range"
|
||||
min={1}
|
||||
max={10}
|
||||
value={brushSize}
|
||||
onChange={(e) => setBrushSize(parseInt(e.target.value))}
|
||||
className="w-20 h-2 bg-white/20 rounded-lg appearance-none cursor-pointer"
|
||||
style={{
|
||||
background: `linear-gradient(to right, #3b82f6 0%, #3b82f6 ${(brushSize - 1) * 11.11}%, rgba(255,255,255,0.2) ${(brushSize - 1) * 11.11}%, rgba(255,255,255,0.2) 100%)`
|
||||
}}
|
||||
/>
|
||||
<span className="text-sm text-white/80 font-medium min-w-[1.5rem] text-center">
|
||||
{brushSize}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Zoom controls */}
|
||||
<div className="flex items-center gap-1 bg-white/5 rounded-xl p-1 border border-white/10">
|
||||
<button
|
||||
className="px-3 py-2 rounded-lg text-white/70 hover:text-white hover:bg-white/10 transition-all duration-200"
|
||||
onClick={handleZoomOut}
|
||||
title="Zoom Out"
|
||||
>
|
||||
🔍➖
|
||||
</button>
|
||||
<div className="px-3 py-2 text-sm text-white/80 font-mono min-w-[4rem] text-center">
|
||||
{Math.round(viewport.zoom * 100)}%
|
||||
</div>
|
||||
<button
|
||||
className="px-3 py-2 rounded-lg text-white/70 hover:text-white hover:bg-white/10 transition-all duration-200"
|
||||
onClick={handleZoomIn}
|
||||
title="Zoom In"
|
||||
>
|
||||
🔍➕
|
||||
</button>
|
||||
<div className="w-px h-6 bg-white/20 mx-1" />
|
||||
<button
|
||||
className="px-3 py-2 rounded-lg text-white/70 hover:text-white hover:bg-white/10 transition-all duration-200"
|
||||
onClick={handleResetView}
|
||||
title="Reset View"
|
||||
>
|
||||
🏠
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* View options */}
|
||||
<div className="flex items-center gap-1 bg-white/5 rounded-xl p-1 border border-white/10">
|
||||
<button
|
||||
className={`px-3 py-2 rounded-lg transition-all duration-200 ${
|
||||
showGrid
|
||||
? 'bg-green-500 text-white shadow-lg'
|
||||
: 'text-white/70 hover:text-white hover:bg-white/10'
|
||||
}`}
|
||||
onClick={() => useCanvasStore.setState({ showGrid: !showGrid })}
|
||||
title="Toggle Grid"
|
||||
>
|
||||
⬜
|
||||
</button>
|
||||
<button
|
||||
className={`px-3 py-2 rounded-lg transition-all duration-200 ${
|
||||
showCursors
|
||||
? 'bg-purple-500 text-white shadow-lg'
|
||||
: 'text-white/70 hover:text-white hover:bg-white/10'
|
||||
}`}
|
||||
onClick={() => useCanvasStore.setState({ showCursors: !showCursors })}
|
||||
title="Toggle Cursors"
|
||||
>
|
||||
👁️
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Theme toggle */}
|
||||
<button
|
||||
className="px-3 py-2 rounded-xl bg-white/5 border border-white/10 text-white/70 hover:text-white hover:bg-white/10 transition-all duration-200"
|
||||
onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
|
||||
title="Toggle Theme"
|
||||
>
|
||||
{theme === 'dark' ? '☀️' : '🌙'}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
130
frontend/src/components/ui/UsernameModal.tsx
Normal file
130
frontend/src/components/ui/UsernameModal.tsx
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { motion, AnimatePresence } from 'framer-motion';
|
||||
|
||||
interface UsernameModalProps {
|
||||
isOpen: boolean;
|
||||
currentUsername: string;
|
||||
onUsernameChange: (username: string) => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
export function UsernameModal({
|
||||
isOpen,
|
||||
currentUsername,
|
||||
onUsernameChange,
|
||||
onClose
|
||||
}: UsernameModalProps) {
|
||||
const [username, setUsername] = useState(currentUsername);
|
||||
|
||||
useEffect(() => {
|
||||
setUsername(currentUsername);
|
||||
}, [currentUsername]);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (username.trim()) {
|
||||
onUsernameChange(username.trim());
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<AnimatePresence>
|
||||
{isOpen && (
|
||||
<>
|
||||
{/* Backdrop */}
|
||||
<motion.div
|
||||
className="fixed inset-0 bg-black/60 backdrop-blur-sm z-50"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
onClick={onClose}
|
||||
/>
|
||||
|
||||
{/* Modal */}
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 sm:p-6">
|
||||
<motion.div
|
||||
className="w-full max-w-[320px] sm:max-w-[350px]"
|
||||
initial={{ opacity: 0, scale: 0.7, y: 30 }}
|
||||
animate={{ opacity: 1, scale: 1, y: 0 }}
|
||||
exit={{ opacity: 0, scale: 0.7, y: 30 }}
|
||||
transition={{ type: "spring", stiffness: 300, damping: 25 }}
|
||||
>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="bg-white/5 backdrop-blur-2xl rounded-2xl sm:rounded-3xl p-4 sm:p-6 lg:p-8 border border-white/20 shadow-2xl ring-1 ring-white/10 mx-auto">
|
||||
<div className="text-center">
|
||||
<motion.h3
|
||||
className="text-white text-lg sm:text-xl font-bold mb-2"
|
||||
initial={{ opacity: 0, y: -10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.1 }}
|
||||
>
|
||||
Choose Username
|
||||
</motion.h3>
|
||||
|
||||
<motion.p
|
||||
className="text-white/70 text-xs sm:text-sm mb-4 sm:mb-6"
|
||||
initial={{ opacity: 0, y: -10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.15 }}
|
||||
>
|
||||
Your username will be shown when you place pixels
|
||||
</motion.p>
|
||||
|
||||
<motion.div
|
||||
className="mb-6 sm:mb-8"
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.2 }}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
placeholder="Enter your username..."
|
||||
className="w-full px-3 sm:px-4 py-3 sm:py-3 bg-white/5 backdrop-blur-xl border border-white/20 rounded-xl text-white text-sm sm:text-base placeholder-white/50 focus:outline-none focus:border-blue-400/60 focus:bg-white/10 transition-all duration-200 ring-1 ring-white/10 touch-manipulation"
|
||||
maxLength={20}
|
||||
autoFocus
|
||||
/>
|
||||
</motion.div>
|
||||
|
||||
<motion.div
|
||||
className="flex gap-3 sm:gap-4"
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ delay: 0.25 }}
|
||||
>
|
||||
<motion.button
|
||||
type="button"
|
||||
className="flex-1 px-4 sm:px-6 py-3 bg-white/5 backdrop-blur-xl text-white text-sm sm:text-base rounded-xl border border-white/20 hover:bg-white/10 active:bg-white/15 transition-all duration-200 font-medium ring-1 ring-white/10 touch-manipulation"
|
||||
onClick={onClose}
|
||||
whileHover={{ scale: 1.02 }}
|
||||
whileTap={{ scale: 0.98 }}
|
||||
>
|
||||
Cancel
|
||||
</motion.button>
|
||||
<motion.button
|
||||
type="submit"
|
||||
className="flex-1 px-4 sm:px-6 py-3 bg-gradient-to-r from-blue-500/80 to-purple-600/80 backdrop-blur-xl text-white text-sm sm:text-base rounded-xl hover:from-blue-600/90 hover:to-purple-700/90 active:from-blue-700/90 active:to-purple-800/90 transition-all duration-200 font-medium shadow-xl ring-1 ring-white/20 disabled:opacity-50 disabled:cursor-not-allowed touch-manipulation"
|
||||
disabled={!username.trim()}
|
||||
whileHover={{
|
||||
scale: username.trim() ? 1.02 : 1,
|
||||
boxShadow: username.trim() ? "0 10px 25px rgba(0,0,0,0.3)" : undefined
|
||||
}}
|
||||
whileTap={{ scale: username.trim() ? 0.98 : 1 }}
|
||||
>
|
||||
Save
|
||||
</motion.button>
|
||||
</motion.div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</motion.div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
);
|
||||
}
|
||||
51
frontend/src/components/ui/ZoomControls.tsx
Normal file
51
frontend/src/components/ui/ZoomControls.tsx
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
'use client';
|
||||
|
||||
import { motion } from 'framer-motion';
|
||||
|
||||
interface ZoomControlsProps {
|
||||
zoom: number;
|
||||
onZoomIn: () => void;
|
||||
onZoomOut: () => void;
|
||||
}
|
||||
|
||||
export function ZoomControls({ zoom, onZoomIn, onZoomOut }: ZoomControlsProps) {
|
||||
return (
|
||||
<motion.div
|
||||
className="fixed bottom-4 right-4 sm:bottom-6 sm:right-6 z-40"
|
||||
initial={{ opacity: 0, scale: 0.8 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
transition={{ type: "spring", stiffness: 400, damping: 25 }}
|
||||
>
|
||||
<div className="flex flex-col gap-2 sm:gap-3">
|
||||
{/* Zoom In Button */}
|
||||
<motion.button
|
||||
className="w-14 h-14 sm:w-16 sm:h-16 bg-white/10 backdrop-blur-2xl rounded-full border border-white/20 shadow-lg ring-1 ring-white/10 flex items-center justify-center text-white text-2xl sm:text-3xl font-bold hover:bg-white/20 active:bg-white/30 transition-all duration-200 select-none touch-manipulation min-h-[56px] min-w-[56px]"
|
||||
onClick={onZoomIn}
|
||||
whileHover={{ scale: 1.05 }}
|
||||
whileTap={{ scale: 0.95 }}
|
||||
transition={{ type: "spring", stiffness: 400 }}
|
||||
>
|
||||
+
|
||||
</motion.button>
|
||||
|
||||
{/* Zoom Out Button */}
|
||||
<motion.button
|
||||
className="w-14 h-14 sm:w-16 sm:h-16 bg-white/10 backdrop-blur-2xl rounded-full border border-white/20 shadow-lg ring-1 ring-white/10 flex items-center justify-center text-white text-2xl sm:text-3xl font-bold hover:bg-white/20 active:bg-white/30 transition-all duration-200 select-none touch-manipulation min-h-[56px] min-w-[56px]"
|
||||
onClick={onZoomOut}
|
||||
whileHover={{ scale: 1.05 }}
|
||||
whileTap={{ scale: 0.95 }}
|
||||
transition={{ type: "spring", stiffness: 400 }}
|
||||
>
|
||||
−
|
||||
</motion.button>
|
||||
|
||||
{/* Zoom Display */}
|
||||
<div className="bg-white/5 backdrop-blur-2xl rounded-xl sm:rounded-2xl px-2 py-1 sm:px-3 sm:py-2 border border-white/20 shadow-lg ring-1 ring-white/10">
|
||||
<div className="text-white font-bold text-xs sm:text-sm text-center min-w-[40px] sm:min-w-[50px]">
|
||||
{Math.round(zoom * 100)}%
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
262
frontend/src/hooks/useWebSocket.ts
Normal file
262
frontend/src/hooks/useWebSocket.ts
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
'use client';
|
||||
|
||||
import { useEffect, useRef, useState, useCallback } from 'react';
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import {
|
||||
WebSocketMessage,
|
||||
MessageType,
|
||||
PlacePixelMessage,
|
||||
PixelPlacedMessage,
|
||||
ChunkDataMessage,
|
||||
LoadChunkMessage
|
||||
} from '@gaplace/shared';
|
||||
|
||||
interface UseWebSocketProps {
|
||||
canvasId: string;
|
||||
userId?: string;
|
||||
username?: string;
|
||||
onPixelPlaced?: (message: PixelPlacedMessage & { username?: string }) => void;
|
||||
onChunkData?: (message: ChunkDataMessage) => void;
|
||||
onUserList?: (users: string[]) => void;
|
||||
onCanvasStats?: (stats: { totalPixels?: number; activeUsers?: number; lastActivity?: number }) => void;
|
||||
onCursorUpdate?: (data: { userId: string; username: string; x: number; y: number; tool: string }) => void;
|
||||
}
|
||||
|
||||
export function useWebSocket({
|
||||
canvasId,
|
||||
userId,
|
||||
username,
|
||||
onPixelPlaced,
|
||||
onChunkData,
|
||||
onUserList,
|
||||
onCanvasStats,
|
||||
onCursorUpdate,
|
||||
}: UseWebSocketProps) {
|
||||
const [socket, setSocket] = useState<Socket | null>(null);
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [connectionError, setConnectionError] = useState<string | null>(null);
|
||||
const reconnectAttempts = useRef(0);
|
||||
const maxReconnectAttempts = 5;
|
||||
|
||||
// Simple refs for callbacks to avoid dependency issues
|
||||
const callbacksRef = useRef({
|
||||
onPixelPlaced,
|
||||
onChunkData,
|
||||
onUserList,
|
||||
onCanvasStats,
|
||||
onCursorUpdate,
|
||||
});
|
||||
|
||||
// Update refs on every render
|
||||
callbacksRef.current = {
|
||||
onPixelPlaced,
|
||||
onChunkData,
|
||||
onUserList,
|
||||
onCanvasStats,
|
||||
onCursorUpdate,
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// Dynamically determine backend URL based on current hostname
|
||||
const getBackendUrl = () => {
|
||||
if (typeof window === 'undefined') return 'http://localhost:3001';
|
||||
|
||||
const currentHost = window.location.hostname;
|
||||
const backendPort = '3001';
|
||||
|
||||
// If we have a custom backend URL from env, use it
|
||||
if (process.env.NEXT_PUBLIC_BACKEND_URL) {
|
||||
return process.env.NEXT_PUBLIC_BACKEND_URL;
|
||||
}
|
||||
|
||||
// Otherwise, use the same hostname as frontend but with backend port
|
||||
return `http://${currentHost}:${backendPort}`;
|
||||
};
|
||||
|
||||
const backendUrl = getBackendUrl();
|
||||
console.log('🔌 Initializing WebSocket connection to:', backendUrl);
|
||||
|
||||
const newSocket = io(backendUrl, {
|
||||
transports: ['polling', 'websocket'], // Start with polling first for better compatibility
|
||||
autoConnect: true,
|
||||
reconnection: true,
|
||||
reconnectionAttempts: maxReconnectAttempts,
|
||||
reconnectionDelay: 1000,
|
||||
timeout: 10000,
|
||||
forceNew: true,
|
||||
upgrade: true, // Allow upgrading from polling to websocket
|
||||
});
|
||||
|
||||
// Add error handling for all Socket.IO events
|
||||
newSocket.on('error', (error) => {
|
||||
console.error('❌ Socket.IO error:', error);
|
||||
setConnectionError(error.message || 'Unknown socket error');
|
||||
});
|
||||
|
||||
// Connection event handlers
|
||||
newSocket.on('connect', () => {
|
||||
console.log('✅ Connected to WebSocket server');
|
||||
console.log('Transport:', newSocket.io.engine.transport.name);
|
||||
setIsConnected(true);
|
||||
setConnectionError(null);
|
||||
reconnectAttempts.current = 0;
|
||||
|
||||
// Authenticate after connecting
|
||||
try {
|
||||
newSocket.emit('auth', { userId, canvasId, username });
|
||||
console.log('🔑 Authentication sent for user:', userId);
|
||||
} catch (error) {
|
||||
console.error('❌ Auth error:', error);
|
||||
}
|
||||
});
|
||||
|
||||
newSocket.on('disconnect', (reason) => {
|
||||
console.log('❌ Disconnected from WebSocket server:', reason);
|
||||
setIsConnected(false);
|
||||
});
|
||||
|
||||
newSocket.on('connect_error', (error) => {
|
||||
console.error('Connection error:', error);
|
||||
console.error('Error details:', {
|
||||
message: error.message,
|
||||
description: (error as any).description,
|
||||
context: (error as any).context,
|
||||
type: (error as any).type
|
||||
});
|
||||
reconnectAttempts.current++;
|
||||
|
||||
if (reconnectAttempts.current >= maxReconnectAttempts) {
|
||||
setConnectionError('Failed to connect to server. Please refresh the page.');
|
||||
} else {
|
||||
setConnectionError(`Connection attempt ${reconnectAttempts.current}/${maxReconnectAttempts}...`);
|
||||
}
|
||||
});
|
||||
|
||||
// Canvas event handlers with error wrapping
|
||||
newSocket.on('pixel_placed', (message) => {
|
||||
try {
|
||||
callbacksRef.current.onPixelPlaced?.(message);
|
||||
} catch (error) {
|
||||
console.error('❌ Error in onPixelPlaced callback:', error);
|
||||
}
|
||||
});
|
||||
|
||||
newSocket.on('chunk_data', (message) => {
|
||||
try {
|
||||
callbacksRef.current.onChunkData?.(message);
|
||||
} catch (error) {
|
||||
console.error('❌ Error in onChunkData callback:', error);
|
||||
}
|
||||
});
|
||||
|
||||
newSocket.on('user_list', (users) => {
|
||||
try {
|
||||
callbacksRef.current.onUserList?.(users);
|
||||
} catch (error) {
|
||||
console.error('❌ Error in onUserList callback:', error);
|
||||
}
|
||||
});
|
||||
|
||||
newSocket.on('canvas_info', (stats) => {
|
||||
try {
|
||||
callbacksRef.current.onCanvasStats?.(stats);
|
||||
} catch (error) {
|
||||
console.error('❌ Error in onCanvasStats callback:', error);
|
||||
}
|
||||
});
|
||||
|
||||
newSocket.on('canvas_updated', (stats) => {
|
||||
try {
|
||||
callbacksRef.current.onCanvasStats?.(stats);
|
||||
} catch (error) {
|
||||
console.error('❌ Error in onCanvasStats callback:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// Error handlers (removing duplicate error handler)
|
||||
// Already handled above
|
||||
|
||||
newSocket.on('rate_limited', (data: { message: string; resetTime: number }) => {
|
||||
console.warn('Rate limited:', data.message);
|
||||
setConnectionError(`Rate limited. Try again in ${Math.ceil((data.resetTime - Date.now()) / 1000)} seconds.`);
|
||||
});
|
||||
|
||||
newSocket.on('cursor_update', (data: { userId: string; username: string; x: number; y: number; tool: string }) => {
|
||||
try {
|
||||
callbacksRef.current.onCursorUpdate?.(data);
|
||||
} catch (error) {
|
||||
console.error('❌ Error in onCursorUpdate callback:', error);
|
||||
}
|
||||
});
|
||||
|
||||
setSocket(newSocket);
|
||||
|
||||
return () => {
|
||||
newSocket.disconnect();
|
||||
};
|
||||
}, [canvasId, userId, username]);
|
||||
|
||||
const placePixel = (x: number, y: number, color: string) => {
|
||||
if (!socket || !isConnected) {
|
||||
console.warn('Cannot place pixel: not connected');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const message: PlacePixelMessage = {
|
||||
type: MessageType.PLACE_PIXEL,
|
||||
x,
|
||||
y,
|
||||
color,
|
||||
canvasId,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
socket.emit('place_pixel', message);
|
||||
console.log('📍 Placed pixel at:', { x, y, color });
|
||||
} catch (error) {
|
||||
console.error('❌ Error placing pixel:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const loadChunk = (chunkX: number, chunkY: number) => {
|
||||
if (!socket || !isConnected) {
|
||||
console.warn('Cannot load chunk: not connected');
|
||||
return;
|
||||
}
|
||||
|
||||
const message: LoadChunkMessage = {
|
||||
type: MessageType.LOAD_CHUNK,
|
||||
chunkX,
|
||||
chunkY,
|
||||
canvasId,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
socket.emit('load_chunk', message);
|
||||
};
|
||||
|
||||
const moveCursor = (x: number, y: number, tool: string) => {
|
||||
if (!socket || !isConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('cursor_move', {
|
||||
type: MessageType.CURSOR_MOVE,
|
||||
x,
|
||||
y,
|
||||
tool,
|
||||
canvasId,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
socket,
|
||||
isConnected,
|
||||
connectionError,
|
||||
placePixel,
|
||||
loadChunk,
|
||||
moveCursor,
|
||||
};
|
||||
}
|
||||
284
frontend/src/store/canvasStore.ts
Normal file
284
frontend/src/store/canvasStore.ts
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { CANVAS_CONFIG, COLORS } from '@gaplace/shared';
|
||||
|
||||
interface PixelData {
|
||||
x: number;
|
||||
y: number;
|
||||
color: string;
|
||||
timestamp?: number;
|
||||
userId?: string;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
interface PixelInfo {
|
||||
color: string;
|
||||
userId?: string;
|
||||
username?: string;
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
interface Chunk {
|
||||
chunkX: number;
|
||||
chunkY: number;
|
||||
pixels: Map<string, PixelInfo>; // key: "x,y", value: pixel info
|
||||
isLoaded: boolean;
|
||||
lastModified: number;
|
||||
}
|
||||
|
||||
interface Viewport {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
zoom: number;
|
||||
}
|
||||
|
||||
interface CanvasState {
|
||||
// Canvas data
|
||||
canvasId: string;
|
||||
chunks: Map<string, Chunk>; // key: "chunkX,chunkY"
|
||||
|
||||
// Viewport
|
||||
viewport: Viewport;
|
||||
|
||||
// Tools
|
||||
selectedColor: string;
|
||||
selectedTool: 'pixel' | 'fill' | 'eyedropper';
|
||||
brushSize: number;
|
||||
|
||||
// UI state
|
||||
isLoading: boolean;
|
||||
isPanning: boolean;
|
||||
showGrid: boolean;
|
||||
showCursors: boolean;
|
||||
|
||||
// User presence
|
||||
activeUsers: string[];
|
||||
userCursors: Map<string, { x: number; y: number; username: string; color: string }>;
|
||||
|
||||
// Stats
|
||||
totalPixels: number;
|
||||
userPixels: number;
|
||||
|
||||
// Actions
|
||||
setCanvasId: (id: string) => void;
|
||||
setPixel: (x: number, y: number, color: string, userId?: string, username?: string) => void;
|
||||
loadChunk: (chunkX: number, chunkY: number, pixels: PixelData[]) => void;
|
||||
setViewport: (viewport: Partial<Viewport>) => void;
|
||||
setZoom: (zoom: number, centerX?: number, centerY?: number) => void;
|
||||
pan: (deltaX: number, deltaY: number) => void;
|
||||
setSelectedColor: (color: string) => void;
|
||||
setSelectedTool: (tool: 'pixel' | 'fill' | 'eyedropper') => void;
|
||||
setBrushSize: (size: number) => void;
|
||||
setUserCursor: (userId: string, x: number, y: number, username: string, color: string) => void;
|
||||
removeUserCursor: (userId: string) => void;
|
||||
setActiveUsers: (users: string[]) => void;
|
||||
setStats: (totalPixels: number, userPixels?: number) => void;
|
||||
getPixelAt: (x: number, y: number) => string | null;
|
||||
getPixelInfo: (x: number, y: number) => PixelInfo | null;
|
||||
getChunkKey: (chunkX: number, chunkY: number) => string;
|
||||
getPixelKey: (x: number, y: number) => string;
|
||||
getChunkCoordinates: (x: number, y: number) => { chunkX: number; chunkY: number };
|
||||
}
|
||||
|
||||
export const useCanvasStore = create<CanvasState>()(
|
||||
devtools(
|
||||
(set, get) => ({
|
||||
// Initial state
|
||||
canvasId: 'default',
|
||||
chunks: new Map(),
|
||||
viewport: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
zoom: 1,
|
||||
},
|
||||
selectedColor: COLORS.PALETTE[0],
|
||||
selectedTool: 'pixel',
|
||||
brushSize: 1,
|
||||
isLoading: false,
|
||||
isPanning: false,
|
||||
showGrid: true,
|
||||
showCursors: true,
|
||||
activeUsers: [],
|
||||
userCursors: new Map(),
|
||||
totalPixels: 0,
|
||||
userPixels: 0,
|
||||
|
||||
// Actions
|
||||
setCanvasId: (id) => set({ canvasId: id }),
|
||||
|
||||
setPixel: (x, y, color, userId, username) => {
|
||||
const state = get();
|
||||
const { chunkX, chunkY } = state.getChunkCoordinates(x, y);
|
||||
const chunkKey = state.getChunkKey(chunkX, chunkY);
|
||||
const pixelKey = state.getPixelKey(
|
||||
x % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE,
|
||||
y % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE
|
||||
);
|
||||
|
||||
const chunks = new Map(state.chunks);
|
||||
let chunk = chunks.get(chunkKey);
|
||||
|
||||
if (!chunk) {
|
||||
chunk = {
|
||||
chunkX,
|
||||
chunkY,
|
||||
pixels: new Map(),
|
||||
isLoaded: true,
|
||||
lastModified: Date.now(),
|
||||
};
|
||||
}
|
||||
|
||||
const pixelInfo: PixelInfo = {
|
||||
color,
|
||||
userId,
|
||||
username,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
chunk.pixels.set(pixelKey, pixelInfo);
|
||||
chunk.lastModified = Date.now();
|
||||
chunks.set(chunkKey, chunk);
|
||||
|
||||
set({ chunks });
|
||||
},
|
||||
|
||||
loadChunk: (chunkX, chunkY, pixels) => {
|
||||
const state = get();
|
||||
const chunkKey = state.getChunkKey(chunkX, chunkY);
|
||||
const chunks = new Map(state.chunks);
|
||||
|
||||
const pixelMap = new Map<string, PixelInfo>();
|
||||
pixels.forEach((pixel) => {
|
||||
const localX = pixel.x % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE;
|
||||
const localY = pixel.y % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE;
|
||||
const pixelKey = state.getPixelKey(localX, localY);
|
||||
pixelMap.set(pixelKey, {
|
||||
color: pixel.color,
|
||||
userId: pixel.userId,
|
||||
username: pixel.username,
|
||||
timestamp: pixel.timestamp
|
||||
});
|
||||
});
|
||||
|
||||
const chunk: Chunk = {
|
||||
chunkX,
|
||||
chunkY,
|
||||
pixels: pixelMap,
|
||||
isLoaded: true,
|
||||
lastModified: Date.now(),
|
||||
};
|
||||
|
||||
chunks.set(chunkKey, chunk);
|
||||
set({ chunks });
|
||||
},
|
||||
|
||||
setViewport: (newViewport) => {
|
||||
const state = get();
|
||||
set({
|
||||
viewport: { ...state.viewport, ...newViewport },
|
||||
});
|
||||
},
|
||||
|
||||
setZoom: (zoom, centerX, centerY) => {
|
||||
const state = get();
|
||||
const clampedZoom = Math.max(CANVAS_CONFIG.MIN_ZOOM, Math.min(CANVAS_CONFIG.MAX_ZOOM, zoom));
|
||||
|
||||
let newViewport = { ...state.viewport, zoom: clampedZoom };
|
||||
|
||||
// If center point is provided, zoom towards that point
|
||||
if (centerX !== undefined && centerY !== undefined) {
|
||||
const BASE_PIXEL_SIZE = 32;
|
||||
const currentPixelSize = BASE_PIXEL_SIZE * state.viewport.zoom;
|
||||
const newPixelSize = BASE_PIXEL_SIZE * clampedZoom;
|
||||
|
||||
// Calculate what canvas coordinate is at the center point
|
||||
const canvasCenterX = (centerX + state.viewport.x) / currentPixelSize;
|
||||
const canvasCenterY = (centerY + state.viewport.y) / currentPixelSize;
|
||||
|
||||
// Calculate new viewport to keep same canvas point at center
|
||||
newViewport.x = canvasCenterX * newPixelSize - centerX;
|
||||
newViewport.y = canvasCenterY * newPixelSize - centerY;
|
||||
}
|
||||
|
||||
set({ viewport: newViewport });
|
||||
},
|
||||
|
||||
pan: (deltaX, deltaY) => {
|
||||
const state = get();
|
||||
set({
|
||||
viewport: {
|
||||
...state.viewport,
|
||||
x: state.viewport.x + deltaX,
|
||||
y: state.viewport.y + deltaY,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
setSelectedColor: (color) => set({ selectedColor: color }),
|
||||
setSelectedTool: (tool) => set({ selectedTool: tool }),
|
||||
setBrushSize: (size) => set({ brushSize: Math.max(1, Math.min(10, size)) }),
|
||||
|
||||
setUserCursor: (userId, x, y, username, color) => {
|
||||
const state = get();
|
||||
const userCursors = new Map(state.userCursors);
|
||||
userCursors.set(userId, { x, y, username, color });
|
||||
set({ userCursors });
|
||||
},
|
||||
|
||||
removeUserCursor: (userId) => {
|
||||
const state = get();
|
||||
const userCursors = new Map(state.userCursors);
|
||||
userCursors.delete(userId);
|
||||
set({ userCursors });
|
||||
},
|
||||
|
||||
setActiveUsers: (users) => set({ activeUsers: users }),
|
||||
setStats: (totalPixels, userPixels) => set({
|
||||
totalPixels,
|
||||
...(userPixels !== undefined && { userPixels })
|
||||
}),
|
||||
|
||||
getPixelAt: (x, y) => {
|
||||
const state = get();
|
||||
const { chunkX, chunkY } = state.getChunkCoordinates(x, y);
|
||||
const chunkKey = state.getChunkKey(chunkX, chunkY);
|
||||
const chunk = state.chunks.get(chunkKey);
|
||||
|
||||
if (!chunk) return null;
|
||||
|
||||
const localX = x % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE;
|
||||
const localY = y % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE;
|
||||
const pixelKey = state.getPixelKey(localX, localY);
|
||||
|
||||
return chunk.pixels.get(pixelKey)?.color || null;
|
||||
},
|
||||
|
||||
getPixelInfo: (x, y) => {
|
||||
const state = get();
|
||||
const { chunkX, chunkY } = state.getChunkCoordinates(x, y);
|
||||
const chunkKey = state.getChunkKey(chunkX, chunkY);
|
||||
const chunk = state.chunks.get(chunkKey);
|
||||
|
||||
if (!chunk) return null;
|
||||
|
||||
const localX = x % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE;
|
||||
const localY = y % CANVAS_CONFIG.DEFAULT_CHUNK_SIZE;
|
||||
const pixelKey = state.getPixelKey(localX, localY);
|
||||
|
||||
return chunk.pixels.get(pixelKey) || null;
|
||||
},
|
||||
|
||||
getChunkKey: (chunkX, chunkY) => `${chunkX},${chunkY}`,
|
||||
getPixelKey: (x, y) => `${x},${y}`,
|
||||
getChunkCoordinates: (x, y) => ({
|
||||
chunkX: Math.floor(x / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE),
|
||||
chunkY: Math.floor(y / CANVAS_CONFIG.DEFAULT_CHUNK_SIZE),
|
||||
}),
|
||||
}),
|
||||
{ name: 'canvas-store' }
|
||||
)
|
||||
);
|
||||
54
frontend/src/styles/globals.css
Normal file
54
frontend/src/styles/globals.css
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@layer base {
|
||||
html {
|
||||
scroll-behavior: smooth;
|
||||
}
|
||||
|
||||
body {
|
||||
@apply bg-white dark:bg-gray-900 text-gray-900 dark:text-white transition-colors duration-300;
|
||||
}
|
||||
}
|
||||
|
||||
@layer components {
|
||||
.canvas-container {
|
||||
@apply relative overflow-hidden bg-white dark:bg-gray-800 rounded-lg shadow-lg;
|
||||
image-rendering: pixelated;
|
||||
image-rendering: -moz-crisp-edges;
|
||||
image-rendering: crisp-edges;
|
||||
}
|
||||
|
||||
.pixel {
|
||||
@apply absolute cursor-crosshair;
|
||||
image-rendering: pixelated;
|
||||
}
|
||||
|
||||
.color-picker-button {
|
||||
@apply w-8 h-8 rounded-full border-2 border-gray-300 dark:border-gray-600 cursor-pointer transition-all duration-200 hover:scale-110;
|
||||
}
|
||||
|
||||
.color-picker-button.selected {
|
||||
@apply border-4 border-blue-500 scale-110 shadow-lg;
|
||||
}
|
||||
|
||||
.tool-button {
|
||||
@apply p-2 rounded-lg bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors duration-200;
|
||||
}
|
||||
|
||||
.tool-button.active {
|
||||
@apply bg-blue-500 text-white hover:bg-blue-600;
|
||||
}
|
||||
}
|
||||
|
||||
@layer utilities {
|
||||
.scrollbar-hide {
|
||||
-ms-overflow-style: none;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
|
||||
.scrollbar-hide::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
55
frontend/tailwind.config.js
Normal file
55
frontend/tailwind.config.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/** @type {import('tailwindcss').Config} */
|
||||
module.exports = {
|
||||
content: [
|
||||
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
],
|
||||
darkMode: 'class',
|
||||
theme: {
|
||||
extend: {
|
||||
colors: {
|
||||
primary: {
|
||||
50: '#eff6ff',
|
||||
100: '#dbeafe',
|
||||
200: '#bfdbfe',
|
||||
300: '#93c5fd',
|
||||
400: '#60a5fa',
|
||||
500: '#3b82f6',
|
||||
600: '#2563eb',
|
||||
700: '#1d4ed8',
|
||||
800: '#1e40af',
|
||||
900: '#1e3a8a',
|
||||
},
|
||||
gray: {
|
||||
900: '#0f0f0f',
|
||||
800: '#1a1a1a',
|
||||
700: '#2a2a2a',
|
||||
600: '#3a3a3a',
|
||||
500: '#6a6a6a',
|
||||
400: '#9a9a9a',
|
||||
300: '#cacaca',
|
||||
200: '#e0e0e0',
|
||||
100: '#f0f0f0',
|
||||
50: '#fafafa',
|
||||
},
|
||||
},
|
||||
animation: {
|
||||
'fade-in': 'fadeIn 0.5s ease-in-out',
|
||||
'slide-up': 'slideUp 0.3s ease-out',
|
||||
'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite',
|
||||
},
|
||||
keyframes: {
|
||||
fadeIn: {
|
||||
'0%': { opacity: '0' },
|
||||
'100%': { opacity: '1' },
|
||||
},
|
||||
slideUp: {
|
||||
'0%': { transform: 'translateY(10px)', opacity: '0' },
|
||||
'100%': { transform: 'translateY(0)', opacity: '1' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
29
frontend/tsconfig.json
Normal file
29
frontend/tsconfig.json
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"lib": ["dom", "dom.iterable", "es2017"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"exactOptionalPropertyTypes": false,
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
12435
package-lock.json
generated
12435
package-lock.json
generated
File diff suppressed because it is too large
Load diff
62
package.json
62
package.json
|
|
@ -1,18 +1,50 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"cookie-parser": "^1.4.6",
|
||||
"express": "^4.18.2",
|
||||
"fs": "^0.0.1-security",
|
||||
"socket.io": "^4.7.1"
|
||||
},
|
||||
"name": "collaborative-pixel-art",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"name": "gaplace",
|
||||
"version": "2.0.0",
|
||||
"description": "Modern collaborative pixel art platform with real-time collaboration and infinite canvas",
|
||||
"private": true,
|
||||
"workspaces": [
|
||||
"backend",
|
||||
"frontend",
|
||||
"shared"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"dev": "node scripts/start-dev.js",
|
||||
"dev:old": "concurrently \"npm run dev:backend\" \"npm run dev:frontend\"",
|
||||
"dev:backend": "npm run dev --workspace=backend",
|
||||
"dev:frontend": "npm run dev --workspace=frontend",
|
||||
"build": "npm run build --workspace=backend && npm run build --workspace=frontend",
|
||||
"start": "npm run start --workspace=backend",
|
||||
"test": "npm run test --workspaces",
|
||||
"lint": "npm run lint --workspaces",
|
||||
"type-check": "npm run type-check --workspaces",
|
||||
"clean": "npm run clean --workspaces && rm -rf node_modules",
|
||||
"setup": "node scripts/setup.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": ""
|
||||
}
|
||||
"devDependencies": {
|
||||
"concurrently": "^9.1.2",
|
||||
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
||||
"@typescript-eslint/parser": "^8.22.0",
|
||||
"eslint": "^8.57.1",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-prettier": "^5.2.1",
|
||||
"prettier": "^3.4.2",
|
||||
"typescript": "^5.7.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0",
|
||||
"npm": ">=9.0.0"
|
||||
},
|
||||
"keywords": [
|
||||
"pixel-art",
|
||||
"collaborative",
|
||||
"real-time",
|
||||
"canvas",
|
||||
"websocket",
|
||||
"react",
|
||||
"nextjs",
|
||||
"typescript"
|
||||
],
|
||||
"author": "GaPlace Team",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
<!-- index.html -->
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Collaborative Pixel Art</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h1>Collaborative Pixel Art</h1>
|
||||
<div id="pixel-count"></div> <!-- Element to display pixel count -->
|
||||
</div>
|
||||
<div class="container">
|
||||
<div id="canvas">
|
||||
<!-- Canvas will be generated dynamically -->
|
||||
</div>
|
||||
|
||||
<div class="color-selector">
|
||||
<div class="color-option" style="background-color: #000000;"></div>
|
||||
<div class="color-option" style="background-color: #FF0000;"></div>
|
||||
<div class="color-option" style="background-color: #00FF00;"></div>
|
||||
<div class="color-option" style="background-color: #0000FF;"></div>
|
||||
<div class="color-option" style="background-color: #FFFF00;"></div>
|
||||
<div class="color-option" style="background-color: #FF00FF;"></div>
|
||||
<div class="color-option" style="background-color: #00FFFF;"></div>
|
||||
<div class="color-option" style="background-color: #FFFFFF;"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
115
public/script.js
115
public/script.js
|
|
@ -1,115 +0,0 @@
|
|||
// script.js
|
||||
const socket = io();
|
||||
|
||||
let yourPixelsPlaced = 0; // Counter for pixels placed by you
|
||||
|
||||
// Function to set a cookie
|
||||
function setCookie(name, value, days) {
|
||||
const date = new Date();
|
||||
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
||||
const expires = "expires=" + date.toUTCString();
|
||||
document.cookie = name + "=" + value + ";" + expires + ";path=/";
|
||||
}
|
||||
|
||||
// Function to get a cookie
|
||||
function getCookie(name) {
|
||||
const cookieName = name + "=";
|
||||
const decodedCookie = decodeURIComponent(document.cookie);
|
||||
const cookieArray = decodedCookie.split(';');
|
||||
for (let i = 0; i < cookieArray.length; i++) {
|
||||
let cookie = cookieArray[i];
|
||||
while (cookie.charAt(0) === ' ') {
|
||||
cookie = cookie.substring(1);
|
||||
}
|
||||
if (cookie.indexOf(cookieName) === 0) {
|
||||
return cookie.substring(cookieName.length, cookie.length);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
const colorOptions = document.querySelectorAll('.color-option');
|
||||
let currentColor = '#000000'; // Default: Black
|
||||
|
||||
// Set the current color when a color option is clicked
|
||||
colorOptions.forEach(option => {
|
||||
option.addEventListener('click', () => {
|
||||
currentColor = option.style.backgroundColor;
|
||||
// Remove the 'selected' class from all color options
|
||||
colorOptions.forEach(opt => opt.classList.remove('selected'));
|
||||
// Add the 'selected' class to the clicked color option
|
||||
option.classList.add('selected');
|
||||
});
|
||||
});
|
||||
|
||||
// Update the current color of the pixel and place it on the canvas
|
||||
function placePixel(index, color) {
|
||||
socket.emit('placePixel', index, color);
|
||||
}
|
||||
|
||||
// Create the canvas
|
||||
const canvasDiv = document.getElementById('canvas');
|
||||
const canvasWidth = 50;
|
||||
const canvasHeight = 50;
|
||||
|
||||
function createCanvas() {
|
||||
for (let i = 0; i < canvasWidth * canvasHeight; i++) {
|
||||
const pixel = document.createElement('div');
|
||||
pixel.classList.add('pixel');
|
||||
canvasDiv.appendChild(pixel);
|
||||
}
|
||||
}
|
||||
|
||||
// Update a pixel on the canvas
|
||||
function updatePixel(index, color) {
|
||||
const pixel = document.getElementsByClassName('pixel')[index];
|
||||
pixel.style.backgroundColor = color;
|
||||
}
|
||||
|
||||
// Function to update the pixel count display
|
||||
function updatePixelCount() {
|
||||
const pixelCountElement = document.getElementById('pixel-count');
|
||||
pixelCountElement.textContent = `Total Pixels: ${totalPixelsPlaced}, Your Pixels: ${yourPixelsPlaced}`;
|
||||
}
|
||||
|
||||
// Retrieve yourPixelsPlaced value from the cookie
|
||||
const savedPixelCount = parseInt(getCookie('yourPixelsPlaced'));
|
||||
if (!isNaN(savedPixelCount)) {
|
||||
yourPixelsPlaced = savedPixelCount;
|
||||
}
|
||||
|
||||
// Handle initial pixel data from the server
|
||||
socket.on('initPixels', (pixels) => {
|
||||
for (let i = 0; i < pixels.length; i++) {
|
||||
updatePixel(i, pixels[i]);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle pixel placement from the client
|
||||
canvasDiv.addEventListener('click', (event) => {
|
||||
if (event.target.classList.contains('pixel')) {
|
||||
const index = Array.prototype.indexOf.call(canvasDiv.children, event.target);
|
||||
updatePixel(index, currentColor);
|
||||
placePixel(index, currentColor);
|
||||
|
||||
// Increment yourPixelsPlaced when you place a pixel
|
||||
yourPixelsPlaced++;
|
||||
updatePixelCount();
|
||||
// Save yourPixelsPlaced value to the cookie
|
||||
setCookie('yourPixelsPlaced', yourPixelsPlaced, 365); // Cookie expires in 365 days
|
||||
}
|
||||
});
|
||||
|
||||
// Handle updates from other clients
|
||||
socket.on('updatePixel', (index, color) => {
|
||||
updatePixel(index, color);
|
||||
});
|
||||
|
||||
// Receive the total pixels count from the server
|
||||
socket.on('totalPixelsCount', (count) => {
|
||||
totalPixelsPlaced = count;
|
||||
updatePixelCount();
|
||||
});
|
||||
|
||||
createCanvas();
|
||||
updatePixelCount(); // Call to initialize the pixel count display
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
/* styles.css */
|
||||
|
||||
/* General styling for the body and page layout */
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
font-family: Arial, sans-serif; /* Set font family for the entire page */
|
||||
background-color: #f0f0f0; /* Light gray background color */
|
||||
}
|
||||
|
||||
/* Styling for the header section */
|
||||
.header {
|
||||
margin-bottom: 20px; /* Add spacing at the bottom of the header */
|
||||
color: #333; /* Dark text color */
|
||||
font-size: 24px; /* Larger font size for the header */
|
||||
font-weight: bold; /* Make the header text bold */
|
||||
text-align: center; /* Center-align the text within the header */
|
||||
}
|
||||
|
||||
/* Styling for the pixel count display */
|
||||
#pixel-count {
|
||||
font-size: 16px;
|
||||
color: #666;
|
||||
text-align: center;
|
||||
margin-top: 10px; /* Add spacing above the pixel count */
|
||||
}
|
||||
|
||||
/* Styling for the main container that wraps canvas and color selector */
|
||||
.container {
|
||||
display: flex;
|
||||
flex-wrap: wrap; /* Allow the container to wrap on smaller screens */
|
||||
justify-content: center; /* Center the content horizontally */
|
||||
align-items: center; /* Center the content vertically */
|
||||
background-color: #fff; /* White background color */
|
||||
border-radius: 8px; /* Rounded corners for the container */
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow effect */
|
||||
padding: 20px; /* Add padding inside the container */
|
||||
max-width: 800px; /* Set a maximum width for the container */
|
||||
}
|
||||
|
||||
/* Styling for the canvas where pixels will be placed */
|
||||
#canvas {
|
||||
width: 500px;
|
||||
height: 500px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(50, 10px); /* Create 50 columns of 10px each */
|
||||
grid-template-rows: repeat(50, 10px); /* Create 50 rows of 10px each */
|
||||
gap: 0; /* Remove any gap between pixels */
|
||||
}
|
||||
|
||||
/* Styling for individual pixels */
|
||||
.pixel {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
display: inline-block;
|
||||
border: 1px solid #ccc; /* Add a 1px border around each pixel */
|
||||
}
|
||||
|
||||
/* Styling for the color selector section */
|
||||
.color-selector {
|
||||
display: flex;
|
||||
flex-direction: column; /* Color options arranged vertically */
|
||||
align-items: center; /* Center color options horizontally */
|
||||
margin-left: 20px; /* Add some space to the left of the color selector */
|
||||
}
|
||||
|
||||
/* Styling for individual color options */
|
||||
.color-option {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
border-radius: 50%; /* Make the color options circular */
|
||||
margin: 5px; /* Add spacing between color options */
|
||||
cursor: pointer; /* Show pointer cursor on hover */
|
||||
border: 2px solid #ccc; /* Add a 2px border around each color option */
|
||||
/* Color options will have their background color set dynamically */
|
||||
}
|
||||
|
||||
/* Styling for the currently selected color option */
|
||||
.color-option.selected {
|
||||
border-color: #000; /* Change the border color for the selected color */
|
||||
}
|
||||
|
||||
/* Responsive design for phone-friendly UI */
|
||||
@media (max-width: 768px) {
|
||||
.container {
|
||||
flex-direction: column; /* Stack canvas and color selector vertically */
|
||||
}
|
||||
|
||||
#canvas {
|
||||
width: 90%; /* Adjust canvas width to fit smaller screens */
|
||||
height: auto; /* Allow canvas height to adapt based on content */
|
||||
margin: 20px 0; /* Add some spacing around the canvas */
|
||||
}
|
||||
|
||||
.color-selector {
|
||||
flex-direction: row; /* Arrange color options horizontally */
|
||||
justify-content: center; /* Center color options horizontally */
|
||||
margin: 0; /* Remove any margin on smaller screens */
|
||||
margin-top: 20px; /* Add some spacing above the color selector */
|
||||
}
|
||||
|
||||
.color-option {
|
||||
margin: 5px 8px; /* Adjust spacing between color options */
|
||||
}
|
||||
|
||||
/* Adjust pixel count styles for phone-friendly UI */
|
||||
#pixel-count {
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
75
scripts/setup.js
Normal file
75
scripts/setup.js
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
console.log('🎨 Setting up GaPlace...\n');
|
||||
|
||||
// Check if required files exist
|
||||
const requiredFiles = [
|
||||
'backend/.env',
|
||||
'frontend/.env.local'
|
||||
];
|
||||
|
||||
console.log('📋 Checking environment files...');
|
||||
requiredFiles.forEach(file => {
|
||||
if (!fs.existsSync(file)) {
|
||||
console.log(`❌ Missing: ${file}`);
|
||||
process.exit(1);
|
||||
} else {
|
||||
console.log(`✅ Found: ${file}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Build shared package
|
||||
console.log('\n📦 Building shared package...');
|
||||
try {
|
||||
execSync('npm run build', { cwd: 'shared', stdio: 'inherit' });
|
||||
console.log('✅ Shared package built successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to build shared package');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Install dependencies for all workspaces
|
||||
console.log('\n📦 Installing dependencies...');
|
||||
try {
|
||||
execSync('npm install', { stdio: 'inherit' });
|
||||
console.log('✅ Dependencies installed successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to install dependencies');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Build backend
|
||||
console.log('\n🔧 Building backend...');
|
||||
try {
|
||||
execSync('npm run build', { cwd: 'backend', stdio: 'inherit' });
|
||||
console.log('✅ Backend built successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to build backend');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Build frontend
|
||||
console.log('\n🎨 Building frontend...');
|
||||
try {
|
||||
execSync('npm run build', { cwd: 'frontend', stdio: 'inherit' });
|
||||
console.log('✅ Frontend built successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to build frontend');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('\n🚀 Setup complete! To start development:');
|
||||
console.log('');
|
||||
console.log('1. Start databases:');
|
||||
console.log(' docker-compose up redis postgres -d');
|
||||
console.log('');
|
||||
console.log('2. Start development servers:');
|
||||
console.log(' npm run dev');
|
||||
console.log('');
|
||||
console.log('3. Open http://localhost:3000 in your browser');
|
||||
console.log('');
|
||||
console.log('🎨 Welcome to GaPlace! ✨');
|
||||
128
scripts/start-dev.js
Normal file
128
scripts/start-dev.js
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const { spawn, exec } = require('child_process');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
const execPromise = util.promisify(exec);
|
||||
|
||||
console.log('🎨 Starting GaPlace Development Environment...\n');
|
||||
|
||||
// Function to check if port is in use
|
||||
async function isPortInUse(port) {
|
||||
try {
|
||||
const { stdout } = await execPromise(`netstat -ano | findstr :${port}`);
|
||||
return stdout.trim().length > 0;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to kill process on port
|
||||
async function killPort(port) {
|
||||
try {
|
||||
await execPromise(`npx kill-port ${port}`);
|
||||
console.log(`✅ Cleared port ${port}`);
|
||||
} catch (error) {
|
||||
console.log(`ℹ️ Port ${port} was already free`);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to start a process and handle output
|
||||
function startProcess(name, command, args, cwd, color) {
|
||||
console.log(`${color}[${name}]${'\x1b[0m'} Starting: ${command} ${args.join(' ')}`);
|
||||
|
||||
const isWindows = process.platform === 'win32';
|
||||
const proc = spawn(command, args, {
|
||||
cwd,
|
||||
stdio: 'pipe',
|
||||
shell: true,
|
||||
windowsHide: true
|
||||
});
|
||||
|
||||
proc.stdout.on('data', (data) => {
|
||||
const lines = data.toString().split('\n').filter(line => line.trim());
|
||||
lines.forEach(line => {
|
||||
console.log(`${color}[${name}]${'\x1b[0m'} ${line}`);
|
||||
});
|
||||
});
|
||||
|
||||
proc.stderr.on('data', (data) => {
|
||||
const lines = data.toString().split('\n').filter(line => line.trim());
|
||||
lines.forEach(line => {
|
||||
console.log(`${color}[${name}]${'\x1b[0m'} ${line}`);
|
||||
});
|
||||
});
|
||||
|
||||
proc.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.log(`${color}[${name}]${'\x1b[0m'} ❌ Process exited with code ${code}`);
|
||||
}
|
||||
});
|
||||
|
||||
proc.on('error', (error) => {
|
||||
console.log(`${color}[${name}]${'\x1b[0m'} ❌ Error: ${error.message}`);
|
||||
});
|
||||
|
||||
return proc;
|
||||
}
|
||||
|
||||
async function startDevelopment() {
|
||||
try {
|
||||
// Clear ports first
|
||||
console.log('🧹 Clearing ports...');
|
||||
await killPort(3001);
|
||||
await killPort(3000);
|
||||
|
||||
// Wait a moment for ports to be fully cleared
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
console.log('🔧 Starting backend server...');
|
||||
const backend = startProcess(
|
||||
'Backend',
|
||||
'npm',
|
||||
['run', 'dev'],
|
||||
path.join(__dirname, '..', 'backend'),
|
||||
'\x1b[34m' // Blue
|
||||
);
|
||||
|
||||
// Wait for backend to start
|
||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||
|
||||
console.log('🎨 Starting frontend server...');
|
||||
const frontend = startProcess(
|
||||
'Frontend',
|
||||
'npm',
|
||||
['run', 'dev'],
|
||||
path.join(__dirname, '..', 'frontend'),
|
||||
'\x1b[32m' // Green
|
||||
);
|
||||
|
||||
console.log('\n📱 Frontend: http://localhost:3000');
|
||||
console.log('🔌 Backend: http://localhost:3001');
|
||||
console.log('🩺 Health Check: http://localhost:3001/health');
|
||||
console.log('💡 Press Ctrl+C to stop all servers\n');
|
||||
|
||||
// Handle Ctrl+C
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\n🛑 Shutting down development servers...');
|
||||
backend.kill('SIGTERM');
|
||||
frontend.kill('SIGTERM');
|
||||
|
||||
// Wait a moment then force kill if needed
|
||||
setTimeout(() => {
|
||||
backend.kill('SIGKILL');
|
||||
frontend.kill('SIGKILL');
|
||||
process.exit(0);
|
||||
}, 2000);
|
||||
});
|
||||
|
||||
// Keep the script running
|
||||
setInterval(() => {}, 1000);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to start development environment:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
startDevelopment();
|
||||
69
server.js
69
server.js
|
|
@ -1,69 +0,0 @@
|
|||
// server.js
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const http = require('http').createServer(app);
|
||||
const io = require('socket.io')(http);
|
||||
const fs = require('fs');
|
||||
|
||||
const initialPixelColor = '#FFFFFF'; // Default color: White
|
||||
const canvasWidth = 50;
|
||||
const canvasHeight = 50;
|
||||
let pixels = new Array(canvasWidth * canvasHeight).fill(initialPixelColor);
|
||||
let totalPixelsPlaced = 0; // Counter for total pixels placed by everyone
|
||||
|
||||
// Function to save the canvas data to a JSON file
|
||||
function saveCanvasToJSON() {
|
||||
const data = JSON.stringify(pixels);
|
||||
fs.writeFileSync('canvas_data.json', data, 'utf8', (err) => {
|
||||
if (err) {
|
||||
console.error('Error saving canvas data:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Function to load the canvas data from the JSON file
|
||||
function loadCanvasFromJSON() {
|
||||
try {
|
||||
if (fs.existsSync('canvas_data.json')) {
|
||||
const data = fs.readFileSync('canvas_data.json', 'utf8');
|
||||
pixels = JSON.parse(data);
|
||||
totalPixelsPlaced = pixels.filter(color => color !== initialPixelColor).length;
|
||||
} else {
|
||||
// If the file does not exist, create a new one with default pixel data
|
||||
saveCanvasToJSON();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error loading canvas data:', err);
|
||||
}
|
||||
}
|
||||
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
// Send the initial pixel data to the connected client
|
||||
socket.emit('initPixels', pixels);
|
||||
|
||||
// Send the total pixels count to the connected client
|
||||
socket.emit('totalPixelsCount', totalPixelsPlaced);
|
||||
|
||||
socket.on('placePixel', (index, color) => {
|
||||
// Update the pixel color in the array
|
||||
pixels[index] = color;
|
||||
// Broadcast the updated pixel color to all clients
|
||||
io.emit('updatePixel', index, color);
|
||||
|
||||
// Increment the total pixels counter when a pixel is placed
|
||||
totalPixelsPlaced++;
|
||||
// Broadcast the updated total count to all clients
|
||||
io.emit('totalPixelsCount', totalPixelsPlaced);
|
||||
|
||||
// Save the updated canvas data to the JSON file
|
||||
saveCanvasToJSON();
|
||||
});
|
||||
});
|
||||
|
||||
http.listen(3000, () => {
|
||||
console.log('Server started on http://localhost:3000');
|
||||
// Load the canvas data from the JSON file when the server starts
|
||||
loadCanvasFromJSON();
|
||||
});
|
||||
10
shared/.eslintrc.json
Normal file
10
shared/.eslintrc.json
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"extends": ["../.eslintrc.json"],
|
||||
"env": {
|
||||
"browser": true,
|
||||
"node": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
}
|
||||
}
|
||||
20
shared/package.json
Normal file
20
shared/package.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "@gaplace/shared",
|
||||
"version": "1.0.0",
|
||||
"description": "Shared types and utilities for GaPlace",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"dev": "tsc --watch",
|
||||
"clean": "rm -rf dist",
|
||||
"type-check": "tsc --noEmit",
|
||||
"lint": "eslint src --ext .ts,.tsx"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.3.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
28
shared/src/constants/canvas.ts
Normal file
28
shared/src/constants/canvas.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
export const CANVAS_CONFIG = {
|
||||
DEFAULT_CHUNK_SIZE: 64,
|
||||
MAX_CANVAS_SIZE: 10000,
|
||||
MIN_CANVAS_SIZE: 100,
|
||||
DEFAULT_CANVAS_SIZE: 1000,
|
||||
MAX_ZOOM: 32,
|
||||
MIN_ZOOM: 0.1,
|
||||
DEFAULT_ZOOM: 1,
|
||||
PIXEL_SIZE: 1, // Base pixel size in canvas units
|
||||
} as const;
|
||||
|
||||
export const COLORS = {
|
||||
DEFAULT: '#FFFFFF',
|
||||
PALETTE: [
|
||||
'#000000', '#FFFFFF', '#FF0000', '#00FF00', '#0000FF',
|
||||
'#FFFF00', '#FF00FF', '#00FFFF', '#FFA500', '#800080',
|
||||
'#FFC0CB', '#A52A2A', '#808080', '#90EE90', '#FFB6C1',
|
||||
'#87CEEB', '#DDA0DD', '#98FB98', '#F0E68C', '#FF6347',
|
||||
'#40E0D0'
|
||||
]
|
||||
} as const;
|
||||
|
||||
export const RATE_LIMITS = {
|
||||
PIXELS_PER_MINUTE: 60,
|
||||
PIXELS_PER_HOUR: 1000,
|
||||
CURSOR_UPDATES_PER_SECOND: 10,
|
||||
MAX_CONCURRENT_CHUNKS: 100,
|
||||
} as const;
|
||||
13
shared/src/index.ts
Normal file
13
shared/src/index.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// Types
|
||||
export * from './types/canvas';
|
||||
export * from './types/user';
|
||||
export * from './types/websocket';
|
||||
|
||||
// Constants
|
||||
export * from './constants/canvas';
|
||||
|
||||
// Utils
|
||||
export * from './utils/canvas';
|
||||
|
||||
// Re-export specific functions for convenience
|
||||
export { isValidColor } from './utils/canvas';
|
||||
41
shared/src/types/canvas.ts
Normal file
41
shared/src/types/canvas.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
export interface Pixel {
|
||||
x: number;
|
||||
y: number;
|
||||
color: string;
|
||||
timestamp: number;
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
export interface PixelChunk {
|
||||
chunkX: number;
|
||||
chunkY: number;
|
||||
pixels: Map<string, string>; // key: "x,y", value: color
|
||||
lastModified: number;
|
||||
}
|
||||
|
||||
export interface Canvas {
|
||||
id: string;
|
||||
name: string;
|
||||
width: number;
|
||||
height: number;
|
||||
chunkSize: number;
|
||||
isPublic: boolean;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
createdBy: string;
|
||||
}
|
||||
|
||||
export interface Viewport {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
zoom: number;
|
||||
}
|
||||
|
||||
export interface CanvasMetadata {
|
||||
totalPixels: number;
|
||||
activeUsers: number;
|
||||
lastActivity: number;
|
||||
version: number;
|
||||
}
|
||||
34
shared/src/types/user.ts
Normal file
34
shared/src/types/user.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
email?: string;
|
||||
avatar?: string;
|
||||
isGuest: boolean;
|
||||
createdAt: number;
|
||||
lastSeen: number;
|
||||
}
|
||||
|
||||
export interface UserSession {
|
||||
userId: string;
|
||||
sessionId: string;
|
||||
canvasId: string;
|
||||
isActive: boolean;
|
||||
lastActivity: number;
|
||||
cursor?: {
|
||||
x: number;
|
||||
y: number;
|
||||
tool: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface UserPresence {
|
||||
userId: string;
|
||||
username: string;
|
||||
cursor: {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
color: string;
|
||||
tool: string;
|
||||
isActive: boolean;
|
||||
}
|
||||
83
shared/src/types/websocket.ts
Normal file
83
shared/src/types/websocket.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
export enum MessageType {
|
||||
// Canvas operations
|
||||
PLACE_PIXEL = 'PLACE_PIXEL',
|
||||
PIXEL_PLACED = 'PIXEL_PLACED',
|
||||
LOAD_CHUNK = 'LOAD_CHUNK',
|
||||
CHUNK_DATA = 'CHUNK_DATA',
|
||||
|
||||
// User presence
|
||||
USER_JOINED = 'USER_JOINED',
|
||||
USER_LEFT = 'USER_LEFT',
|
||||
CURSOR_MOVE = 'CURSOR_MOVE',
|
||||
USER_LIST = 'USER_LIST',
|
||||
|
||||
// Canvas management
|
||||
CANVAS_INFO = 'CANVAS_INFO',
|
||||
CANVAS_UPDATED = 'CANVAS_UPDATED',
|
||||
|
||||
// System
|
||||
HEARTBEAT = 'HEARTBEAT',
|
||||
ERROR = 'ERROR',
|
||||
RATE_LIMITED = 'RATE_LIMITED'
|
||||
}
|
||||
|
||||
export interface BaseMessage {
|
||||
type: MessageType;
|
||||
timestamp: number;
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
export interface PlacePixelMessage extends BaseMessage {
|
||||
type: MessageType.PLACE_PIXEL;
|
||||
x: number;
|
||||
y: number;
|
||||
color: string;
|
||||
canvasId: string;
|
||||
}
|
||||
|
||||
export interface PixelPlacedMessage extends BaseMessage {
|
||||
type: MessageType.PIXEL_PLACED;
|
||||
x: number;
|
||||
y: number;
|
||||
color: string;
|
||||
userId: string;
|
||||
canvasId: string;
|
||||
}
|
||||
|
||||
export interface LoadChunkMessage extends BaseMessage {
|
||||
type: MessageType.LOAD_CHUNK;
|
||||
chunkX: number;
|
||||
chunkY: number;
|
||||
canvasId: string;
|
||||
}
|
||||
|
||||
export interface ChunkDataMessage extends BaseMessage {
|
||||
type: MessageType.CHUNK_DATA;
|
||||
chunkX: number;
|
||||
chunkY: number;
|
||||
pixels: Array<{ x: number; y: number; color: string }>;
|
||||
canvasId: string;
|
||||
}
|
||||
|
||||
export interface CursorMoveMessage extends BaseMessage {
|
||||
type: MessageType.CURSOR_MOVE;
|
||||
x: number;
|
||||
y: number;
|
||||
tool: string;
|
||||
canvasId: string;
|
||||
}
|
||||
|
||||
export interface ErrorMessage extends BaseMessage {
|
||||
type: MessageType.ERROR;
|
||||
message: string;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export type WebSocketMessage =
|
||||
| PlacePixelMessage
|
||||
| PixelPlacedMessage
|
||||
| LoadChunkMessage
|
||||
| ChunkDataMessage
|
||||
| CursorMoveMessage
|
||||
| ErrorMessage
|
||||
| BaseMessage;
|
||||
44
shared/src/utils/canvas.ts
Normal file
44
shared/src/utils/canvas.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { CANVAS_CONFIG } from '../constants/canvas';
|
||||
|
||||
export function getChunkCoordinates(x: number, y: number, chunkSize = CANVAS_CONFIG.DEFAULT_CHUNK_SIZE) {
|
||||
return {
|
||||
chunkX: Math.floor(x / chunkSize),
|
||||
chunkY: Math.floor(y / chunkSize),
|
||||
};
|
||||
}
|
||||
|
||||
export function getPixelKey(x: number, y: number): string {
|
||||
return `${x},${y}`;
|
||||
}
|
||||
|
||||
export function parsePixelKey(key: string): { x: number; y: number } {
|
||||
const [x, y] = key.split(',').map(Number);
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
export function getChunkKey(chunkX: number, chunkY: number): string {
|
||||
return `${chunkX},${chunkY}`;
|
||||
}
|
||||
|
||||
export function parseChunkKey(key: string): { chunkX: number; chunkY: number } {
|
||||
const [chunkX, chunkY] = key.split(',').map(Number);
|
||||
return { chunkX, chunkY };
|
||||
}
|
||||
|
||||
export function getChunkBounds(chunkX: number, chunkY: number, chunkSize = CANVAS_CONFIG.DEFAULT_CHUNK_SIZE) {
|
||||
return {
|
||||
minX: chunkX * chunkSize,
|
||||
minY: chunkY * chunkSize,
|
||||
maxX: (chunkX + 1) * chunkSize - 1,
|
||||
maxY: (chunkY + 1) * chunkSize - 1,
|
||||
};
|
||||
}
|
||||
|
||||
export function isValidColor(color: string): boolean {
|
||||
const hexRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
|
||||
return hexRegex.test(color);
|
||||
}
|
||||
|
||||
export function clampCoordinate(value: number, min: number, max: number): number {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
19
shared/tsconfig.json
Normal file
19
shared/tsconfig.json
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "CommonJS",
|
||||
"moduleResolution": "node",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"allowJs": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue