-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
draw.html
125 lines (115 loc) · 3.24 KB
/
draw.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Screen Drawing</title>
<style>
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
#videoContainer {
position: relative;
width: 100%;
height: 100%;
background-color: black;
}
#video {
width: 100%;
height: 100%;
object-fit: cover;
}
#drawingCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#clearButton {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
padding: 10px;
background-color: white;
border: none;
cursor: pointer;
}
#startButton {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<button id="startButton">Start</button>
<div id="videoContainer" style="display: none;">
<video id="video" autoplay></video>
<canvas id="drawingCanvas"></canvas>
<button id="clearButton">Clear</button>
</div>
<script>
const startButton = document.getElementById('startButton');
const videoContainer = document.getElementById('videoContainer');
const video = document.getElementById('video');
const drawingCanvas = document.getElementById('drawingCanvas');
const clearButton = document.getElementById('clearButton');
const ctx = drawingCanvas.getContext('2d');
let drawing = false;
startButton.addEventListener('click', async () => {
try {
const stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
video.srcObject = stream;
videoContainer.style.display = 'block';
startButton.style.display = 'none';
} catch (err) {
console.error('Error: ' + err);
}
});
function resizeCanvas() {
drawingCanvas.width = window.innerWidth;
drawingCanvas.height = window.innerHeight;
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
drawingCanvas.addEventListener('mousedown', (event) => {
drawing = true;
const pos = getMousePos(drawingCanvas, event);
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
// drawingCanvas.style.pointerEvents = 'auto';
});
drawingCanvas.addEventListener('mouseup', () => {
drawing = false;
// drawingCanvas.style.pointerEvents = 'none';
});
drawingCanvas.addEventListener('mousemove', (event) => {
if (drawing) {
const pos = getMousePos(drawingCanvas, event);
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
}
});
clearButton.addEventListener('click', () => {
ctx.clearRect(0, 0, drawingCanvas.width, drawingCanvas.height);
});
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
</script>
</body>
</html>