-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathcanvas.js
45 lines (39 loc) · 1.16 KB
/
canvas.js
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
const canvas = document.getElementById('drawingCanvas');
const context = canvas.getContext('2d');
let drawing = false;
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mousemove', draw);
function startDrawing(e) {
drawing = true;
draw(e);
}
function stopDrawing() {
drawing = false;
context.beginPath();
sendCanvasData();
}
function draw(e) {
if (!drawing) return;
context.lineWidth = 5;
context.lineCap = 'round';
context.strokeStyle = 'black';
context.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
context.stroke();
context.beginPath();
context.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
}
function sendCanvasData() {
canvas.toBlob((blob) => {
const formData = new FormData();
formData.append('image', blob, 'canvas.png');
fetch('/process-canvas', {
method: 'POST',
body: formData,
}).then(response => response.json())
.then(data => {
document.getElementById('caption').innerHTML = data.caption;
console.log(data);})
.catch(error => console.error('Error:', error));
});
}