-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
57 lines (49 loc) · 1.42 KB
/
js.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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
var rectangle = canvas.getBoundingClientRect();
var currentColorValue = "black", currentStrokeValue = 1;
//fgetting color
function defcolor(c){
currentColorValue = c;
document.getElementById('dispColor').innerHTML = currentColorValue;
}
//getting Stroke
function defstroke(s){
currentStrokeValue = s;
document.getElementById('dispStroke').innerHTML = currentStrokeValue;
}
//resizing
canvas.height = window.innerHeight - 160;
canvas.width = window.innerWidth - 50;
//clear the canvas
let clear = document.getElementById('clear').addEventListener('click', clearCanvas);
function clearCanvas(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
//Checking either the user painting or not
let painting = false;
//start Function
function start(e){
painting = true;
draw(e);
}
//stop function
function stop(){
painting = false;
ctx.beginPath();
}
//drawing function
function draw({clientX: x, clientY: y}){
if(!painting) return;
ctx.lineWidth = currentStrokeValue;
ctx.lineCap = "round";
ctx.strokeStyle = currentColorValue;
ctx.lineTo(x-8 ,y-8); //margin 8 from top and left
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x-8, y-8);
};
//main functin
canvas.addEventListener('mousedown', start);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stop);