-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
109 lines (87 loc) · 3.45 KB
/
main.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
var mouseEvent = "empty";
var last_position_of_x, last_position_of_y;
canvas = document.getElementById('myCanvas');
ctx = canvas.getContext("2d");
color = "black";
width_of_line = 2;
canvas.addEventListener("mousedown", my_mousedown);
function my_mousedown(e)
{
//Addictonal Activity start
color = document.getElementById("color").value;
width_of_line = document.getElementById("width").value;
//Addictonal Activity ends
mouseEvent = "mouseDown";
}
canvas.addEventListener("mouseup", my_mouseup);
function my_mouseup(e)
{
mouseEvent = "mouseUP";
}
canvas.addEventListener("mouseleave", my_mouseleave);
function my_mouseleave(e)
{
mouseEvent = "mouseleave";
}
canvas.addEventListener("mousemove", my_mousemove);
function my_mousemove(e)
{
current_position_of_mouse_x = e.clientX - canvas.offsetLeft;
current_position_of_mouse_y = e.clientY - canvas.offsetTop;
if (mouseEvent == "mouseDown") {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = width_of_line;
console.log("Last position of x and y coordinates = ");
console.log("x = " + last_position_of_x + "y = " + last_position_of_y);
ctx.moveTo(last_position_of_x, last_position_of_y);
console.log("Current position of x and y coordinates = ");
console.log("x = " + current_position_of_mouse_x + "y = " + current_position_of_mouse_y);
ctx.lineTo(current_position_of_mouse_x, current_position_of_mouse_y);
ctx.stroke();
}
last_position_of_x = current_position_of_mouse_x;
last_position_of_y = current_position_of_mouse_y;
}
//Touch screen
var width = screen.width;
newWidth = screen.width - 70;
newHeight = screen.height - 300;
if (width < 992){
document.getElementById('myCanvas').width = newWidth;
document.getElementById('myCanvas').height = newHeight;
document.body.style.overflow = "hidden";
}
canvas.addEventListener("touchstart", touch_start);
function touch_start(e){
console.log("Touchstart is used");
color = document.getElementById("color").value;
width_of_line = document.getElementById("width").value;
last_position_of_x = e.touches[0].clientX - canvas.offsetLeft;
last_position_of_y = e.touches[0].clientY - canvas.offsetTop;
}
canvas.addEventListener("touchmove", touch_move);
function touch_move(e){
console.log("Touchmove is used");
current_touch_x = e.touches[0].clientX - canvas.offsetLeft;
current_touch_y = e.touches[0].clientY - canvas.offsetTop;
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = width_of_line;
console.log("X = " + last_position_of_x + "Y = " + last_position_of_y);
ctx.moveTo(last_position_of_x, last_position_of_y);
console.log("X = " + current_touch_x + "Y = " + current_touch_y);
ctx.lineTo(current_touch_x, current_touch_y);
ctx.stroke();
last_position_of_x = current_touch_x;
last_position_of_y = current_touch_y;
}
function clear_area(){
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
console.log("Cleared");
}
function fill_canvas(){
ctx.fillStyle = color;
ctx.fillRect(0,0, ctx.canvas.width, ctx.canvas.height);
console.log("Filled");
}