Tutorial2 Collaborative Whiteboard
Tutorial2 Collaborative Whiteboard
1. Install Node.js: If you haven't already, download and install Node.js from
nodejs.org.
2. Open Visual Studio Code: Launch Visual Studio Code.
1. Create a New Folder: Create a new folder for your project, e.g., collaborative-
whiteboard.
2. Open the Folder in VS Code: Open this folder in Visual Studio Code.
3. Initialize a Node.js Project:
a. Open the terminal in VS Code (View > Terminal).
b. Run the following command to initialize your project:
npm init -y
app.use(express.static('public'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Collaborative Whiteboard</title>
<style>
body { display: flex; flex-direction: column; align-items:
center; }
canvas { border: 1px solid black; }
video { margin-top: 20px; width: 640px; height: 360px; }
</style>
</head>
<body>
<canvas id="whiteboard" width="640" height="480"></canvas>
<video id="videoPlayer" controls>
<source src="your-video-file.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
const canvas = document.getElementById('whiteboard');
const ctx = canvas.getContext('2d');
const ws = new WebSocket(`ws://${window.location.host}`);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', () => drawing = false);
canvas.addEventListener('mouseout', () => drawing = false);
function draw(e) {
if (!drawing) return;
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
ctx.lineTo(x, y);
ctx.stroke();
const message = { x, y };
ws.send(JSON.stringify(message));
}
ctx.lineWidth = 2;
ctx.strokeStyle = 'black';
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
</script>
</body>
</html>
• Place your video file (e.g., your-video-file.mp4) in the public folder. Make sure
to update the <source src="your-video-file.mp4" type="video/mp4"> line
in index.html with the actual filename.
node server.js
Additional Features
• Color and Brush Size Options: You can add UI elements to choose different colors
and brush sizes.
• Clear Canvas Button: Implement a button that clears the canvas for all users.
• Persistent Data: Consider saving drawings to a database for persistent sessions.