-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.html
342 lines (282 loc) · 9.69 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Title -->
<title>Covikind</title>
<!-- PaperCSS -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/paper.min.css">
<!-- FA CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.css" rel="stylesheet">
<!-- WOW CSS -->
<link rel="stylesheet" href="css/animate.css">
<!-- Favicon -->
<link rel="shortcut icon" href="img/logo.png" type="image/x-icon">
<!-- Custom stylesheet -->
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<!-- Navbar -->
<nav class="border fixed split-nav" style="background-color: #e7e1fb;">
<div class="nav-brand">
<a href="#"><img src="img/nav-logo.png" style="height: 56px; border: none;"/></a>
</div>
<div class="collapsible">
<input id="collapsible1" type="checkbox" name="collapsible1">
<label for="collapsible1">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</label>
<div class="collapsible-body">
<ul class="inline">
<li><a href="index.html" class="active-nav">Home</a></li>
<li><a href="play.html">Play</a></li>
<li class="nav-item-active"><a href="/draw.html">Draw</a></li>
<li><a href="journal.html">Journal</a></li>
<li><a href="selfcare.html">Self Care</a></li>
<li><a href="https://github.com/Swatilekha-Roy/Covikind"><i class="fab fa-github" style="font-size: 40px"></i></a></li>
</ul>
</div>
</div>
<!-- Lights -->
<ul class="lightrope">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</nav>
<div id="container_div" style="margin-top: 4%;">
<div id="palette_div">
<div id="slider_track"></div>
<div id="slider">
<i id="slider_icon" class="fas fa-paint-brush"></i>
<div id="slider_mask"></div>
</div>
<div class="color" id="red" style="background:red"></div>
<div class="color" id="purple" style="background:purple"></div>
<div class="color" id="blue" style="background:blue"></div>
<div class="color" id="green" style="background:green"></div>
<div class="color" id="yellow" style="background:yellow"></div>
<div class="color" id="orange" style="background:orange"></div>
<div class="color" id="black" style="background:black"></div>
<div class="color" id="gray" style="background:gray"></div>
<div class="color" id="white" style="background:white"></div>
</div>
<div id="canvas_div">
<canvas id="sheet"></canvas>
</div>
<div id="footer_div" style="margin-bottom: 8%;">
<a id="clear" class="buttons">
<i class="fas fa-trash-alt footer_icons"></i>
</a>
<a id="save" class="buttons">
<i class="far fa-save footer_icons"></i>
</a>
</div>
</div>
<!-- Footer -->
<div class="footer-con row flex-top flex-center" style="position: fixed; left: 0; bottom: 0; margin-top: 5%; width: 30%;">
<div class="col-12">
<footer class="border shadow">
<p style="margin-left: 3%">Made with <i class="fas fa-heart"></i> by Swatilekha Roy.
</footer>
</div>
</div>
<!-- JQuery JS-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script>
//primary variables
var container = document.getElementById("container_div");
var sliderTrack = document.getElementById("slider_track");
var slider = document.getElementById("slider");
var canvas = document.getElementById("sheet");
var ctx = canvas.getContext("2d");
//scale to window size
resizeForWindow();
var canvasPositionLeft = canvas.getBoundingClientRect().left + window.scrollX;
var canvasPositionTop = canvas.getBoundingClientRect().top + window.scrollY;
//prep slider
var mouseX;
var sliderLeft = $("#slider").position().left;
var sliderLeftMin = sliderLeft;
var sliderLeftMax = sliderLeft + $("#slider_track").width()*.965;
var currentlySliding = false;
//prep canvas for downloads with a white background
clearCanvas();
//prep markers
var markerWidth = 2; // min = 2, max = 100
ctx.lineWidth = markerWidth;
//colors
var colors = document.getElementsByClassName("color"); //an HTMLCollection of .class elements
var currentColor = "black";
//prep drawing
var currentlyDrawing = false;
///---FUNCTIONS---///
//scaling
function resizeForWindow() {
if (window.innerWidth > window.innerHeight) {
container.style.height = window.innerHeight*.8+"px";
container.style.width = container.style.height;
} else {
container.style.width = window.innerWidth*.8+"px";
container.style.height = container.style.width;
}
canvas.width = document.getElementById("canvas_div").clientWidth;
canvas.height = document.getElementById("canvas_div").clientHeight;
canvasPositionLeft = canvas.getBoundingClientRect().left + window.scrollX;
canvasPositionTop = canvas.getBoundingClientRect().top + window.scrollY;
clearCanvas();
slider.style.left = "2%";
sliderLeft = $("#slider").position().left;
sliderLeftMin = sliderLeft;
sliderLeftMax = sliderLeft + $("#slider_track").width()*.965;
}
//slider
function grab_slider(e) {
mouseX = e.pageX;
currentlySliding = true;
}
function drag_slider(e) {
if (currentlySliding) {
var mouseXChange = e.pageX - mouseX;
if (sliderLeft + mouseXChange < sliderLeftMin) {
slider.style.left = sliderLeftMin + "px";
} else if (sliderLeft + mouseXChange > sliderLeftMax) {
slider.style.left = sliderLeftMax + "px";
} else {
slider.style.left = (sliderLeft + mouseXChange) + "px";
};
}
}
function drop_slider(e) {
sliderLeft = $("#slider").position().left;
updateMarker();
currentlySliding = false;
}
function updateMarker() {
//converts slider position to a proportional marker width of 2px-100px
markerWidth = (sliderLeft-sliderLeftMin)*98/(sliderLeftMax-sliderLeftMin-sliderLeftMin)+2;
ctx.lineWidth = markerWidth;
}
//colors
function bindColors() {
for (i = 0; i < colors.length; i++) {
colors[i].addEventListener('click', selectColor);
}
}
function selectColor(e) {
currentColor = e.target.style.background;
}
//drawing
function markerDown(e) {
currentlyDrawing = true;
var canvasX = e.pageX - canvasPositionLeft;
var canvasY = e.pageY - canvasPositionTop;
ctx.beginPath();
ctx.arc(canvasX, canvasY, markerWidth/2, 0, Math.PI * 2);
ctx.fillStyle = currentColor;
ctx.fill();
ctx.beginPath();
}
function draw(e) {
var canvasX = e.pageX - canvasPositionLeft;
var canvasY = e.pageY - canvasPositionTop;
if(currentlyDrawing) {
ctx.strokeStyle = currentColor;
ctx.fillStyle = currentColor;
ctx.lineTo(canvasX, canvasY);
ctx.stroke();
ctx.beginPath();
ctx.arc(canvasX, canvasY, markerWidth/2, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.moveTo(canvasX, canvasY);
}
}
function markerUp(e) {
currentlyDrawing = false;
}
//image actions
function saveImage() {
var image = canvas.toDataURL("image/png");
var download = document.getElementById("save");
download.href = image;
download.download = 'drawing.png';
}
function clearCanvas() {
ctx.fillStyle = "white";
ctx.fillRect(0,0,canvas.width, canvas.height);
}
///---EVENTS---///
//window
window.addEventListener('resize', resizeForWindow);
//slider
slider.addEventListener("mousedown", grab_slider);
document.addEventListener("mousemove", drag_slider);
document.addEventListener("mouseup", drop_slider);
slider.addEventListener("touchstart", grab_slider);
document.addEventListener("touchmove", drag_slider);
document.addEventListener("touchend", drop_slider);
//color selection
bindColors();
//drawing
canvas.addEventListener("mousedown", markerDown);
canvas.addEventListener("mousemove", draw);
document.addEventListener("mouseup", markerUp);
canvas.addEventListener("touchstart", markerDown);
canvas.addEventListener("touchmove", draw);
document.addEventListener("touchend", markerUp);
//image actions
document.getElementById("save").addEventListener('click', saveImage);
document.getElementById("clear").addEventListener('click', clearCanvas);
</script>
<!-- WOW script -->
<script src="js/wow.min.js"></script>
<script>
new WOW().init();
</script>
</body>
</html>