Here's a basic clone of Excalidraw using HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excalidraw Clone</title>
<style>
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
overflow: hidden;
}
.app {
display: flex;
flex-direction: column;
height: 100vh;
background-color: #f5f5f5;
}
.toolbar {
display: flex;
padding: 10px;
background-color: white;
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
z-index: 10;
}
.tool-button {
margin: 0 5px;
padding: 8px 12px;
background-color: transparent;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
display: flex;
align-items: center;
justify-content: center;
}
.tool-button:hover {
background-color: #f0f0f0;
}
.tool-button.active {
background-color: #e0e0e0;
}
.canvas-container {
flex: 1;
position: relative;
overflow: hidden;
}
#canvas {
position: absolute;
top: 0;
left: 0;
cursor: crosshair;
}
.side-panel {
position: absolute;
top: 70px;
left: 10px;
background-color: white;
border-radius: 6px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 10px;
display: flex;
flex-direction: column;
}
.color-picker {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 5px;
margin-bottom: 10px;
}
.color-option {
width: 20px;
height: 20px;
border-radius: 4px;
cursor: pointer;
border: 1px solid rgba(0, 0, 0, 0.1);
}
.color-option.active {
border: 2px solid #000;
}
.stroke-width {
margin-top: 10px;
}
.menu-bar {
display: flex;
padding: 5px 10px;
background-color: white;
border-bottom: 1px solid #e0e0e0;
}
.menu-item {
margin-right: 15px;
cursor: pointer;
font-size: 14px;
}
.menu-item:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="app">
<div class="menu-bar">
<div class="menu-item">File</div>
<div class="menu-item">Edit</div>
<div class="menu-item">View</div>
<div class="menu-item">Help</div>
</div>
<div class="toolbar">
<button class="tool-button active" data-tool="select">Select</button>
<button class="tool-button" data-tool="rectangle">Rectangle</button>
<button class="tool-button" data-tool="diamond">Diamond</button>
<button class="tool-button" data-tool="ellipse">Ellipse</button>
<button class="tool-button" data-tool="arrow">Arrow</button>
<button class="tool-button" data-tool="line">Line</button>
<button class="tool-button" data-tool="pencil">Pencil</button>
<button class="tool-button" data-tool="text">Text</button>
</div>
<div class="canvas-container">
<canvas id="canvas"></canvas>
</div>
<div class="side-panel">
<div>
<h4 style="margin: 0 0 5px 0; font-size: 14px;">Colors</h4>
<div class="color-picker">
<div class="color-option active" style="background-color: #000000;" data-color="#000000"></div>
<div class="color-option" style="background-color: #d83931;" data-color="#d83931"></div>
<div class="color-option" style="background-color: #f7b32b;" data-color="#f7b32b"></div>
<div class="color-option" style="background-color: #00a2ff;" data-color="#00a2ff"></div>
<div class="color-option" style="background-color: #5f6368;" data-color="#5f6368"></div>
<div class="color-option" style="background-color: #e67700;" data-color="#e67700"></div>
<div class="color-option" style="background-color: #0b7285;" data-color="#0b7285"></div>
<div class="color-option" style="background-color: #087f5b;" data-color="#087f5b"></div>
</div>
</div>
<div class="stroke-width">
<h4 style="margin: 0 0 5px 0; font-size: 14px;">Stroke Width</h4>
<input type="range" min="1" max="20" value="2" id="stroke-width">
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const toolButtons = document.querySelectorAll('.tool-button');
const colorOptions = document.querySelectorAll('.color-option');
const strokeWidthInput = document.getElementById('stroke-width');
// Canvas state
let currentTool = 'select';
let currentColor = '#000000';
let currentStrokeWidth = 2;
let isDrawing = false;
let startX, startY;
// Shapes array to store all drawn elements
const shapes = [];
let selectedShape = null;
// Resize canvas to fit window
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
redrawCanvas();
}
// Initialize canvas
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Tool selection
toolButtons.forEach(button => {
button.addEventListener('click', function() {
toolButtons.forEach(btn => btn.classList.remove('active'));
this.classList.add('active');
currentTool = this.getAttribute('data-tool');
});
});
// Color selection
colorOptions.forEach(option => {
option.addEventListener('click', function() {
colorOptions.forEach(opt => opt.classList.remove('active'));
this.classList.add('active');
currentColor = this.getAttribute('data-color');
});
});
// Stroke width
strokeWidthInput.addEventListener('input', function() {
currentStrokeWidth = parseInt(this.value);
});
// Mouse events
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
function startDrawing(e) {
isDrawing = true;
startX = e.clientX - canvas.offsetLeft;
startY = e.clientY - canvas.offsetTop;
if (currentTool === 'select') {
// Check if clicking on a shape
for (let i = shapes.length - 1; i >= 0; i--) {
const shape = shapes[i];
if (isPointInShape(startX, startY, shape)) {
selectedShape = shape;
break;
}
}
} else if (currentTool === 'pencil') {
const newShape = {
type: 'pencil',
color: currentColor,
strokeWidth: currentStrokeWidth,
points: [{x: startX, y: startY}]
};
shapes.push(newShape);
}
}
function draw(e) {
if (!isDrawing) return;
const x = e.clientX - canvas.offsetLeft;
const y = e.clientY - canvas.offsetTop;
if (currentTool === 'select' && selectedShape) {
// Move the selected shape
const dx = x - startX;
const dy = y - startY;
if (selectedShape.type === 'pencil') {
selectedShape.points.forEach(point => {
point.x += dx;
point.y += dy;
});
} else {
selectedShape.x += dx;
selectedShape.y += dy;
selectedShape.width = selectedShape.originalWidth;
selectedShape.height = selectedShape.originalHeight;
}
startX = x;
startY = y;
redrawCanvas();
} else if (currentTool === 'pencil') {
const currentShape = shapes[shapes.length - 1];
currentShape.points.push({x: x, y: y});
// Draw the current stroke
ctx.strokeStyle = currentColor;
ctx.lineWidth = currentStrokeWidth;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.beginPath();
const lastPoint = currentShape.points[currentShape.points.length - 2];
ctx.moveTo(lastPoint.x, lastPoint.y);
ctx.lineTo(x, y);
ctx.stroke();
} else {
// Preview for other shapes
redrawCanvas();
ctx.strokeStyle = currentColor;
ctx.lineWidth = currentStrokeWidth;
const width = x - startX;
const height = y - startY;
switch(currentTool) {
case 'rectangle':
ctx.strokeRect(startX, startY, width, height);
break;
case 'ellipse':
ctx.beginPath();
ctx.ellipse(
startX + width/2,
startY + height/2,
Math.abs(width/2),
Math.abs(height/2),
0, 0, Math.PI * 2
);
ctx.stroke();
break;
case 'line':
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(x, y);
ctx.stroke();
break;
case 'arrow':
drawArrow(ctx, startX, startY, x, y);
break;
case 'diamond':
drawDiamond(ctx, startX, startY, width, height);
break;
}
}
}
function stopDrawing(e) {
if (!isDrawing) return;
isDrawing = false;
if (currentTool !== 'select' && currentTool !== 'pencil') {
const x = e.clientX - canvas.offsetLeft;
const y = e.clientY - canvas.offsetTop;
const width = x - startX;
const height = y - startY;
// Add the shape to our shapes array
const newShape = {
type: currentTool,
x: startX,
y: startY,
width: width,
height: height,
originalWidth: width,
originalHeight: height,
color: currentColor,
strokeWidth: currentStrokeWidth
};
shapes.push(newShape);
}
selectedShape = null;
}
function redrawCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw all shapes
shapes.forEach(shape => {
ctx.strokeStyle = shape.color;
ctx.lineWidth = shape.strokeWidth;
switch(shape.type) {
case 'rectangle':
ctx.strokeRect(shape.x, shape.y, shape.width, shape.height);
break;
case 'ellipse':
ctx.beginPath();
ctx.ellipse(
shape.x + shape.width/2,
shape.y + shape.height/2,
Math.abs(shape.width/2),
Math.abs(shape.height/2),
0, 0, Math.PI * 2
);
ctx.stroke();
break;
case 'line':
ctx.beginPath();
ctx.moveTo(shape.x, shape.y);
ctx.lineTo(shape.x + shape.width, shape.y + shape.height);
ctx.stroke();
break;
case 'arrow':
drawArrow(ctx, shape.x, shape.y, shape.x + shape.width, shape.y + shape.height);
break;
case 'diamond':
drawDiamond(ctx, shape.x, shape.y, shape.width, shape.height);
break;
case 'pencil':
if (shape.points.length > 1) {
ctx.strokeStyle = shape.color;
ctx.lineWidth = shape.strokeWidth;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.beginPath();
ctx.moveTo(shape.points[0].x, shape.points[0].y);
for (let i = 1; i < shape.points.length; i++) {
ctx.lineTo(shape.points[i].x, shape.points[i].y);
}
ctx.stroke();
}
break;
}
// Draw selection box if this shape is selected
if (shape === selectedShape) {
ctx.setLineDash([5, 5]);
ctx.strokeStyle = '#0a84ff';
ctx.lineWidth = 1;
if (shape.type === 'pencil') {
// Find bounding box for pencil
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
shape.points.forEach(point => {
minX = Math.min(minX, point