CanvasToVirtualVideo is a demo project showcasing how to stream HTML5 canvas content from a browser to a virtual video feed using WebSockets. The canvas displays a counter incrementing every 50ms, and its content is streamed as JPEG images at approximately 30 FPS to a WebSocket server. This project demonstrates the potential to capture browser-based canvas animations as a virtual video feed, which can be read and integrated with custom processing pipelines (e.g., video analysis, streaming, or recording).
This project serves as a proof-of-concept for:
- Streaming real-time canvas content from a browser to a server.
- Treating canvas output as a virtual video feed for further processing.
- Enabling integration with external systems for video processing, such as computer vision, live streaming, or video encoding.
- Displays a counter on a canvas, incrementing every 50ms.
- Streams canvas content as JPEG images over a WebSocket connection at ~30 FPS.
- Randomly changes the canvas background color for visual effect.
- Includes a simple Node.js WebSocket server that logs client connections and outputs received binary data to stdout, simulating a virtual video feed.
- Node.js (v14 or higher recommended)
- A modern web browser (e.g., Chrome, Firefox)
- Clone or download the project repository.
- Navigate to the project directory:
cd CanvasToVirtualVideo - Install dependencies:
npm install
-
Start the WebSocket server:
node server.js
The server will start on
ws://localhost:8080and log a message:WebSocket server started on port 8080. -
Open
index.htmlin a web browser. You can either:- Serve
index.htmlusing a local web server (e.g.,npx http-server). - Open
index.htmldirectly in the browser (e.g.,file:///path/to/index.html).
- Serve
-
The canvas will display a counter incrementing every 50ms with a randomly colored background. The canvas content is streamed to the WebSocket server as a virtual video feed, and the server outputs the received binary data to the console.
index.html: The front-end HTML file containing the canvas and JavaScript for rendering the counter and streaming to the WebSocket server.server.js: The Node.js WebSocket server that handles client connections and logs received canvas frames as a virtual video feed.package.json: Specifies project dependencies, including thewsWebSocket library.
ws: WebSocket library for Node.js (version 8.18.3 or higher).
This demo can be extended to integrate with various processing pipelines, such as:
- Video Processing: Pipe the received frames into tools like FFmpeg for encoding or streaming.
- Computer Vision: Feed the frames into libraries like OpenCV for real-time analysis.
- Live Streaming: Forward the virtual video feed to platforms like RTMP servers for broadcasting.
To integrate with custom processing, modify server.js to handle the received binary data (JPEG frames) according to your needs, such as saving them as a video file or passing them to a processing library.
- The counter increments every 50ms, and the canvas is streamed as JPEG images with 0.9 quality to balance performance and image clarity.
- The WebSocket server currently outputs binary frame data to stdout. Modify
server.jsto process or save the frames as part of a virtual video pipeline. - Ensure the WebSocket server is running before opening
index.htmlto establish the connection.
- If the WebSocket connection fails, verify that the server is running on
ws://localhost:8080and that no other process is using port 8080. - If
index.htmlis opened directly (file://), some browsers may restrict WebSocket connections due to security policies. Use a local web server to avoid this issue.
- Add server-side processing to reconstruct the virtual video feed (e.g., saving as MP4).
- Implement frame buffering to handle high frame rates or network latency.
- Enhance the canvas with more complex animations or real-time user interactions.
- Integrate with a video processing library for real-time analysis or streaming.
This project is licensed under the MIT License.