-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackboardCanvas.html
148 lines (118 loc) · 4 KB
/
blackboardCanvas.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html lang="fa-IR" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>تخته سیاه</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #ccc;
}
.toolbar {
background: #ccc;
padding: 5px;
width: 350px;
}
input[type="color"],
button {
width: 90%;
margin: 0 auto;
display: block;
}
input[type="range"] {
width: 70%;
}
span {
position: relative;
bottom: 5px;
}
.output,
button,
label {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-weight: 550;
line-height: 2;
width: 9rem;
}
.output {
width: fit-content;
}
.flex {
display: flex;
}
</style>
</head>
<body>
<div class="toolbar">
<div class="flex">
<label for="color">رنگ گچتخته</label>
<input type="color" id="color" aria-label="رنگ گچ تخته را انتخاب کن" value="#ff0000">
</div>
<div class="flex">
<label for="penSize">ضخامت گچتخته</label>
<input type="range" id="penSize" min="2" max="50" value="30" aria-label="ضخامت گچتخته را انتخاب کن"><span
class="output">۳۰</span>
</div>
<button>تخته پاککن</button>
</div>
<canvas class="myCanvas">
<p>Add suitable fallback here.</p>
</canvas>
<script>
const canvas = document.querySelector('.myCanvas');
const width = canvas.width = window.innerWidth;
const height = canvas.height = window.innerHeight - 85;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgb(0,0,0)';
ctx.fillRect(0, 0, width, height);
const colorPicker = document.querySelector('input[type="color"]');
const sizePicker = document.querySelector('input[type="range"]');
const output = document.querySelector('.output');
const clearBtn = document.querySelector('button');
// covert degrees to radians
function degToRad(degrees) {
return degrees * Math.PI / 180;
};
// update sizepicker output value
function toArabicNumeral(en) {
return ("" + en).replace(/[0-9]/g, function (t) {
return "۰۱۲۳۴۵۶۷۸۹".substr(+t, 1);
});
}
sizePicker.oninput = function () {
output.textContent = toArabicNumeral(sizePicker.value);
}
// store mouse/touch/pen pointer coordinates, and whether the button is pressed
let curX;
let curY;
let pressed = false;
// update mouse/touch/pen pointer coordinates
document.onpointermove = function (e) {
curX = (window.Event) ? e.pageX : e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
curY = (window.Event) ? e.pageY : e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}
canvas.onpointerdown = function () {
pressed = true;
};
canvas.onpointerup = function () {
pressed = false;
}
clearBtn.onclick = function () {
ctx.fillStyle = 'rgb(0,0,0)';
ctx.fillRect(0, 0, width, height);
}
function draw() {
if (pressed) {
ctx.fillStyle = colorPicker.value;
ctx.beginPath();
ctx.arc(curX, curY - 85, sizePicker.value, degToRad(0), degToRad(360), false);
ctx.fill();
}
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>