A real-time collaborative cursor tracking application that allows multiple users to see each other's mouse cursors in real-time across different browser sessions.
- Real-time cursor synchronization - See other users' cursors move in real-time
- Random user identification - Each user gets a randomly generated username (e.g., "happy_dog", "sleepy_cat")
- Color-coded cursors - Each user has a unique colored cursor for easy identification
- Room-based sessions - Users can join specific rooms to collaborate
- Smooth cursor animations - Cursors move smoothly with CSS transitions
- WebSocket communication - Low-latency real-time updates
- TypeScript - Type-safe JavaScript development
- Vite - Fast build tool and development server
- WebSocket API - Real-time communication
- CSS3 - Smooth animations and styling
- Node.js - Server runtime
- ws - WebSocket library for Node.js
- HTTP Server - Basic HTTP server for WebSocket upgrades
GhostCursor/
βββ src/
β βββ main.ts # Main application logic
β βββ websocketProvider.ts # WebSocket connection management
β βββ mouse.ts # Mouse tracking and reactive coordinates
β βββ style.css # Application styles
β βββ vite-env.d.ts # TypeScript environment definitions
βββ example/
β βββ server.cjs # WebSocket server implementation
βββ public/
β βββ vite.svg # Vite logo
βββ index.html # Main HTML file
βββ package.json # Dependencies and scripts
βββ tsconfig.json # TypeScript configuration
- Node.js (v16 or higher)
- npm or bun package manager
- Clone the repository:
git clone https://github.com/FxOmar/GhostCursor.git
cd GhostCursor- Install dependencies:
npm install
# or
bun install- Start the WebSocket server:
node example/server.cjsThe server will start on http://localhost:8080
- Start the frontend development server:
npm run dev
# or
bun run devThe frontend will be available at http://localhost:5173
- Open multiple browser tabs/windows to see the real-time cursor synchronization in action!
-
Mouse Tracking (
mouse.ts):- Uses a Proxy-based reactive system to track mouse coordinates
- Automatically triggers callbacks when mouse position changes
- Provides smooth, efficient mouse position updates
-
WebSocket Provider (
websocketProvider.ts):- Manages WebSocket connections and reconnection logic
- Handles room joining/leaving functionality
- Provides message handling and broadcasting capabilities
-
Main Application (
main.ts):- Generates random usernames for users
- Creates and manages cursor elements in the DOM
- Coordinates between mouse tracking and WebSocket communication
- Handles cursor rendering and animations
The server (example/server.cjs) provides:
- Room Management: Users can join specific rooms for isolated sessions
- Client Tracking: Maintains active connections and user states
- Message Broadcasting: Efficiently broadcasts cursor updates to room members
- Connection Handling: Manages client connections, disconnections, and cleanup
The application uses JSON messages over WebSocket:
// Join a room
{
type: 'join',
roomId: 'default',
username: 'happy_dog',
x: 100,
y: 200
}
// Send cursor position
{
type: 'message',
data: {
username: 'happy_dog',
x: 150,
y: 250
}
}
// Receive cursor updates
{
type: 'message',
data: [
{ username: 'happy_dog', x: 150, y: 250, color: '#ff0000' },
{ username: 'sleepy_cat', x: 300, y: 400, color: '#00ff00' }
]
}npm run dev- Start development server with hot reloadnpm run build- Build the application for productionnpm run preview- Preview the production build locally
Modify the createCursorSVG() function in main.ts to customize cursor shapes:
function createCursorSVG(color: string) {
return `
<svg width="16" height="16" viewBox="0 0 16 16">
<!-- Your custom cursor SVG here -->
</svg>
`;
}The modular architecture makes it easy to extend:
- Add new message types in the WebSocket protocol
- Implement cursor trails or effects in the CSS
- Add user authentication or persistent rooms
- Include chat functionality alongside cursor tracking
- Modern browsers with WebSocket support
- Chrome 16+, Firefox 11+, Safari 7+, Edge 12+
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Built with Vite for fast development experience
- Uses modern TypeScript features for type safety
- Implements efficient WebSocket communication patterns