-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
325 lines (262 loc) · 8.5 KB
/
script.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
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
'use strict';
let board = document.getElementById('canvas');
let ctx = board.getContext('2d');
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
let pencil_dropdown_el = document.getElementById('pencil_dropdown');
let pencil_color_el = document.getElementById('color');
let pencil_width_el = document.getElementById('pencil_width');
let eraser_el = document.getElementById('eraser_dropdown');
let eraser_width_el = document.getElementById('eraser_width');
let trash_el = document.getElementById('trash');
let file_el = document.getElementById('file');
let download_el = document.querySelector('.fa-download');
let text_submit_btn = document.getElementById('submit_content_btn');
let active = '';
let isdrawing = false;
let working =false;
let initial_x;
let initial_y;
let eraserInitial_x;
let eraserInitial_y;
let isErasing = false;
board.width = window.innerWidth - 10;
board.height = window.innerHeight - 10;
//default width and color of pencil
let pencil_width = pencil_width_el.value;
let pencil_color = pencil_color_el.value;
//default width of eraser
let eraser_width = eraser_width_el.value;
//change pencil color
pencil_color_el.oninput = (e)=>{
pencil_color = e.target.value;
}
//change pencil width
pencil_width_el.oninput = (e)=>{
pencil_width =e.target.value
}
//change eraser width
eraser_width_el.oninput = (e)=>{
eraser_width = e.target.value;
}
//click on pencil
pencil_dropdown_el.addEventListener('click', activePencil);
//clicking on eraser
eraser_el.addEventListener('click', activeEraser)
// active pencil function
function activePencil(e){
if(active !='' && active === 'eraser' || trash_el.firstElementChild.firstElementChild.classList.contains('active_delete')){
eraser_el.classList.remove('active');
trash_el.firstElementChild.firstElementChild.classList.remove('active_delete');
active = '';
working =false;
}
working =true
if (working === true) {
pencil_dropdown_el.classList.add('active');
active = 'pencil';
}else {
pencil_dropdown_el.classList.remove('active');
active='';
}
}
// active eraser function
function activeEraser(e){
if(active !=''&& active === 'pencil' || trash_el.firstElementChild.firstElementChild.classList.contains('active_delete')){
pencil_dropdown_el.classList.remove('active');
trash_el.firstElementChild.firstElementChild.classList.remove('active_delete')
active ='';
working =false;
}
working =true;
if(working ==true){
active ='eraser';
eraser_el.classList.add('active');
}else{
eraser_el.classList.remove('active');
active ='';
}
}
//drawing
board.addEventListener('mousedown', initialXandY);
board.addEventListener('mousemove', lineto);
board.addEventListener('mouseup', stopDrawing);
function initialXandY(e){
if (pencil_dropdown_el.classList.contains('active')) {
isdrawing =true;
initial_x = e.offsetX;
initial_y = e.offsetY;
}
}
function lineto(e){
if (pencil_dropdown_el.classList.contains('active')) {
if(isdrawing==true){
drawingLine(initial_x, initial_y, e.offsetX, e.offsetY, pencil_color, pencil_width);
initial_x = e.offsetX;
initial_y = e.offsetY;
}
}
}
function stopDrawing(e){
if(isdrawing==true){
drawingLine(initial_x, initial_y, e.offsetX, e.offsetY, pencil_color, pencil_width);
initial_x =0;
initial_y =0;
isdrawing =false;
}
}
function drawingLine(x, y, X, Y, color, width){
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(X, Y);
ctx.strokeStyle =color;
ctx.lineWidth = width;
ctx.stroke();
ctx.closePath();
}
//erasing the drawing on the canvas content
board.addEventListener('mousedown', initialEraserXandY);
board.addEventListener('mousemove', EraserLineTo);
board.addEventListener('mouseup', stopErase);
function initialEraserXandY(e){
if (eraser_el.classList.contains('active')) {
isErasing =true;
eraserInitial_x = e.offsetX;
eraserInitial_y = e.offsetY;
ctx.strokeStyle = 'white';
}
}
function EraserLineTo(e){
if (eraser_el.classList.contains('active')) {
if (isErasing ===true) {
eraseCanvasContent(eraserInitial_x, eraserInitial_y, e.offsetX, e.offsetY, 'white',eraser_width);
eraserInitial_x = e.offsetX;
eraserInitial_y = e.offsetY;
}
}
}
function stopErase(e){
if (eraser_el.classList.contains('active')) {
if (isErasing ==true) {
eraseCanvasContent(eraserInitial_x, eraserInitial_y, e.offsetX, e.offsetY, 'white', eraser_width);
isErasing =false;
}
}
}
function eraseCanvasContent(x1,y1,x2,y2, color, eraserWidth){
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.lineWidth =eraserWidth;
ctx.stroke();
ctx.closePath();
}
//display textarea on clicking the text icon in the navbar
let textarea_btn = document.querySelector('.fa-commenting');
textarea_btn.onclick =()=>{
if ( document.querySelector('#mydiv').style.display === 'none') {
textarea_btn.classList.add('active');
document.querySelector('#mydiv').style.display = 'block';
}else{
textarea_btn.classList.remove('active');
document.querySelector('#mydiv').style.display = 'none';
}
}
//delete canvas content on clicking the trash icon
trash_el.onclick = ()=>{
if (pencil_dropdown_el.classList.contains('active') || eraser_el.classList.contains('active')) {
pencil_dropdown_el.classList.remove('active');
eraser_el.classList.remove('active');
}
let trashchild = trash_el.firstElementChild.firstElementChild;
trashchild.classList.add('active_delete')
ctx.clearRect(0,0, board.width, board.height);
}
//Make the content text element section draggagle:
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
//handling the text area content
text_submit_btn.onclick=()=>{
let content_el = document.getElementById('content').value;
var maxWidth = 400;
var lineHeight = 25;
var x = (board.width - maxWidth) / 10;
var y = 100;
ctx.font = '16pt Calibri';
ctx.fillStyle = '#333';
wrapText(ctx, content_el, x, y, maxWidth, lineHeight);
}
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
//uploading file to canvas
file_el.onchange=(e)=>{
let main_file_obj = e.target.files[0];
let img_url = URL.createObjectURL(main_file_obj);
let img_string = document.createElement('img');
img_string.src = img_url;
img_string.onload =()=>{
imageToCanvas(img_string, board.width/2, board.height/9, 250, 250);
}
}
//download canvas content
download_el.onclick =()=>{
// e.preventDefault();
let canvas_url =board.toDataURL('image/png');
document.getElementById('download').href=canvas_url;
}
function imageToCanvas(img, x,y,width, height){
ctx.drawImage(img, x , y, width, height );
}