forked from vishanurag/Canvas-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edition.html
127 lines (114 loc) · 4.8 KB
/
edition.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draw and Erase Text on Canvas</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
canvas {
width: 100%;
height: 400px;
border: 1px solid #000;
cursor: crosshair; /* Default cursor */
}
.eraser {
cursor: url('https://upload.wikimedia.org/wikipedia/commons/0/0a/Eraser_icon.png'), auto; /* Custom eraser cursor */
}
</style>
</head>
<body>
<div class="container pt-2">
<div class="mt-2 mb-5">
<input type="color" id="colorPicker" value="#000000" class="form-control" style="width: 100px; display: inline-block;"/>
<button class="btn btn-primary" id="clearCanvas">Clear Canvas</button>
<button class="btn btn-danger" id="eraserMode">Eraser Mode</button>
<button class="btn btn-success" id="downloadCanvas">Download Canvas</button>
</div>
<canvas id="mainCanvas"></canvas>
</div>
<script>
const mainCanvas = document.getElementById('mainCanvas');
const ctx = mainCanvas.getContext('2d');
let drawing = false;
let eraserMode = false;
let currentColor = '#000000'; // Default color
// Resize the canvas to fit the container
function resizeCanvas() {
mainCanvas.width = mainCanvas.offsetWidth;
mainCanvas.height = mainCanvas.offsetHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Function to get the mouse or touch position
function getPosition(event) {
const rect = mainCanvas.getBoundingClientRect();
const x = (event.clientX || event.touches[0].clientX) - rect.left;
const y = (event.clientY || event.touches[0].clientY) - rect.top;
return { x, y };
}
// Start drawing or erasing when the mouse or touch is pressed
function startDrawing(event) {
drawing = true;
const { x, y } = getPosition(event);
ctx.beginPath();
ctx.moveTo(x, y);
}
// Draw on the canvas or erase as the mouse moves or touch moves
function draw(event) {
if (drawing) {
const { x, y } = getPosition(event);
if (eraserMode) {
// Erase by clearing a small area
ctx.clearRect(x - 10, y - 10, 20, 20);
} else {
// Set the stroke color and draw
ctx.strokeStyle = currentColor; // Use the selected color
ctx.lineWidth = 5; // Set a line width
ctx.lineTo(x, y);
ctx.stroke();
}
}
event.preventDefault(); // Prevent scrolling when touching the canvas
}
// Stop drawing or erasing when the mouse is released or touch ends
function stopDrawing() {
drawing = false;
ctx.closePath();
}
// Event listeners for mouse interactions
mainCanvas.addEventListener('mousedown', startDrawing);
mainCanvas.addEventListener('mousemove', draw);
mainCanvas.addEventListener('mouseup', stopDrawing);
// Event listeners for touch interactions
mainCanvas.addEventListener('touchstart', (event) => {
startDrawing(event);
});
mainCanvas.addEventListener('touchmove', (event) => {
draw(event);
});
mainCanvas.addEventListener('touchend', stopDrawing);
// Clear the entire canvas
document.getElementById('clearCanvas').addEventListener('click', () => {
ctx.clearRect(0, 0, mainCanvas.width, mainCanvas.height);
});
// Toggle eraser mode
document.getElementById('eraserMode').addEventListener('click', () => {
eraserMode = !eraserMode;
mainCanvas.classList.toggle('eraser', eraserMode);
document.getElementById('eraserMode').textContent = eraserMode ? 'Drawing Mode' : 'Eraser Mode';
});
// Change the current color when the color picker changes
document.getElementById('colorPicker').addEventListener('input', (event) => {
currentColor = event.target.value;
});
// Download the canvas as an image
document.getElementById('downloadCanvas').addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'canvas-drawing.png';
link.href = mainCanvas.toDataURL('image/png');
link.click();
});
</script>
</body>
</html>