-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (125 loc) · 4.18 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
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
const canvas = document.getElementById("paint_canvas");
const ctx = canvas.getContext("2d");
let brush_size = 10;
let color = "red";
let erase = false
let fake_cursor = document.getElementById("circularcursor")
let mouseEvent = "empty"
let lastX
let lastY
let body_margins = [16, 16]
canvas.addEventListener("mouseenter", custom_mouse_enter);
canvas.addEventListener("mousedown", custom_mouse_down);
canvas.addEventListener("mouseleave", custom_mouse_leave);
canvas.addEventListener("mouseup", custom_mouse_up);
canvas.addEventListener("mousemove", custom_mouse_move);
canvas.addEventListener("touchstart", custom_touch_start);
canvas.addEventListener("touchmove", custom_touch_move);
canvas.addEventListener("touchend", custom_touch_end);
function elementScale(element) {
return element.offsetWidth === 0 ? 0 : (element.width / element.offsetWidth);
}
function custom_mouse_down(event) {
color = document.getElementById("color").value;
brush_size = document.getElementById("width").value;
mouseEvent = "mouseDown";
}
function custom_mouse_leave(event) {
mouseEvent = "mouseleave";
}
function custom_mouse_up(event) {
mouseEvent = "mouseup";
}
function custom_mouse_enter(event) {
document.getElementById("circularcursor").style.display = "block";
mouse_x = (event.pageX - canvas.offsetLeft);
mouse_y = (event.pageY - canvas.offsetTop);
fake_cursor.style.left = event.x - brush_size / 2 + "px";
fake_cursor.style.top = event.y - brush_size / 2 + "px";
}
function custom_mouse_move(event) {
mouse_x = (event.pageX - canvas.offsetLeft - body_margins[1]);
mouse_y = (event.pageY - canvas.offsetTop - body_margins[0]);
fake_cursor.style.left = event.x - brush_size / 2 + "px";
fake_cursor.style.top = event.y - brush_size / 2 + "px";
if (mouseEvent == "mouseDown") {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = brush_size;
ctx.lineJoin = "round";
ctx.moveTo(lastX, lastY);
ctx.lineTo(mouse_x, mouse_y);
ctx.closePath();
ctx.stroke();
}
lastY = mouse_y
lastX = mouse_x
}
// touch
function custom_touch_start(event) {
document.getElementById("circularcursor").style.display = "none";
color = document.getElementById("color").value;
brush_size = document.getElementById("width").value;
lastX = event.touches[0].clientX - canvas.offsetLeft;
lastY = event.touches[0].clientY - canvas.offsetTop;
}
function custom_touch_move(event) {
touch_pos_x = event.touches[0].clientX - canvas.offsetLeft - body_margins[1];
touch_pos_y = event.touches[0].clientY - canvas.offsetTop - body_margins[0];
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = brush_size;
ctx.lineJoin = "round";
ctx.moveTo(lastX, lastY);
ctx.lineTo(touch_pos_x, touch_pos_y);
ctx.closePath();
ctx.stroke();
lastX = touch_pos_x;
lastY = touch_pos_y;
}
function custom_touch_end(event) {
ctx.closePath();
ctx.stroke();
}
// tools
function setErase() {
const elem = document.getElementById("eraser")
erase = elem.checked
console.log(erase)
ctx.globalCompositeOperation = erase ? 'destination-out' : "source-over";
}
function clearArea() {
let text = "Clear all?";
if (confirm(text) == true) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
}
function updateCurSize(size) {
document.getElementById("brush_size").innerText = size
brush_size = size
fake_cursor.style.width = size + "px"
fake_cursor.style.height = size + "px"
}
// resize canvas on small screens
function resizeWindow() {
let wd = window.innerWidth
if (wd < 600) {
document.getElementById("paint_canvas").height = 300;
document.getElementById("paint_canvas").width = 300;
document.body.style.overflow = "hidden";
body_margins = [16, 16]
}
else {
document.getElementById("paint_canvas").height = 400;
document.getElementById("paint_canvas").width = 400;
document.body.style.overflow = "hidden";
body_margins = [0, 0]
}
}
function setup() {
console.log("setup")
updateCurSize(10)
window.addEventListener("resize", resizeWindow);
resizeWindow()
}
setup()