-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
235 lines (201 loc) · 5.41 KB
/
Copy pathmain.js
File metadata and controls
235 lines (201 loc) · 5.41 KB
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
const version = "0.7";
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const draw_style = "#888";
const draw_width = 3.0;
const erase_style = "#111";
const erase_width = 80.0;
const font_style = "#444";
let prev_x = 0.0;
let prev_y = 0.0;
let curr_x = 0.0;
let curr_y = 0.0;
const offscreen_canvas = document.createElement("canvas");
const offscreen_context = offscreen_canvas.getContext("2d");
let text_x = 0.0;
let text_y = 0.0;
let current_text = "";
let is_pressing_mouse = false;
let is_drawing = false;
let is_first_action = true;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
context.lineJoin = "round";
context.lineCap = "round";
context.font = "30px Arial";
context.textAlign = "center";
canvas.addEventListener("mousemove", function (e) {
prev_x = curr_x;
prev_y = curr_y;
curr_x = e.clientX - canvas.offsetLeft;
curr_y = e.clientY - canvas.offsetTop;
if (is_pressing_mouse) {
if (is_drawing) {
api.draw(prev_x, prev_y, curr_x, curr_y);
api.on_draw(prev_x, prev_y, curr_x, curr_y);
} else {
api.erase(prev_x, prev_y, curr_x, curr_y);
api.on_erase(prev_x, prev_y, curr_x, curr_y);
}
undo_api.add_local_undo_line(prev_x, prev_y, curr_x, curr_y);
}
}, false);
canvas.addEventListener("mousedown", function (e) {
end_write();
is_pressing_mouse = true;
if (e.button == 0) {
is_drawing = true;
} else {
is_drawing = false;
}
}, false);
canvas.addEventListener("mouseup", function (e) {
is_pressing_mouse = false;
undo_api.request_add_undo_command(is_drawing ? command_type_draw : command_type_erase);
text_x = e.clientX;
text_y = e.clientY;
}, false);
canvas.addEventListener("mouseout", function (e) {
if (is_pressing_mouse)
undo_api.request_add_undo_command(is_drawing ? command_type_draw : command_type_erase);
is_pressing_mouse = false;
}, false);
document.onkeydown = function (e) {
// Z
if (e.keyCode == 90 && e.ctrlKey) {
end_write();
if (e.shiftKey) {
undo_api.request_undo(1);
} else {
undo_api.request_undo(-1);
}
}
// ESC
else if (e.keyCode == 27) {
end_write();
api.clear();
api.on_clear();
undo_api.request_add_undo_command(command_type_clear);
}
// Backspace
else if (e.keyCode == 8) {
before_write();
current_text = current_text.slice(0, -1);
api.write(text_x, text_y, current_text);
api.on_write(text_x, text_y, current_text);
undo_api.set_local_undo_text(text_x, text_y, current_text);
}
// Enter
else if (e.keyCode == 13) {
end_write();
}
// Skip
else if (
e.keyCode == 37 || // Left
e.keyCode == 38 || // Up
e.keyCode == 39 || // Right
e.keyCode == 40 || // Down
e.keyCode == 46 || // Delete
e.keyCode == 0
) {
}
// Write
else if (!e.ctrlKey && !e.altKey && !e.metaKey) {
before_write();
current_text += e.key;
api.write(text_x, text_y, current_text);
undo_api.set_local_undo_text(text_x, text_y, current_text);
} else {
return;
}
e.preventDefault();
};
function before_write() {
if (is_first_action) {
api.clear();
}
if (current_text === "") {
offscreen_context.drawImage(canvas, 0, 0);
} else {
context.drawImage(offscreen_canvas, 0, 0);
}
}
function end_write() {
if (current_text === "") {
return;
}
api.on_write(text_x, text_y, current_text);
undo_api.request_add_undo_command(command_type_write);
current_text = "";
}
function trace_line(x0, y0, x1, y1) {
context.beginPath();
context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.stroke();
context.closePath();
}
const api = {
enabled: true,
clear: function () {
context.fillStyle = erase_style;
context.fillRect(0, 0, canvas.width, canvas.height);
},
draw: function (x0, y0, x1, y1) {
if (!this.enabled) {
return;
}
if (is_first_action) {
this.clear();
is_first_action = false;
}
context.lineWidth = draw_width;
context.strokeStyle = draw_style;
trace_line(x0, y0, x1, y1);
},
write: function (x, y, text) {
if (!this.enabled) {
return;
}
if (is_first_action) {
this.clear();
is_first_action = false;
}
context.textAlign = "left";
context.fillStyle = draw_style;
context.fillText(text, x, y);
},
erase: function (x0, y0, x1, y1) {
if (!this.enabled) {
return;
}
context.lineWidth = erase_width;
context.strokeStyle = erase_style;
trace_line(x0, y0, x1, y1);
},
on_clear: function () { },
on_draw: function (_x0, _y0, _x1, _y1) { },
on_write: function (_x, _y, _text) { },
on_erase: function (_x0, _y0, _x1, _y1) { },
draw_background_info: function (extra_info) {
this.clear();
context.textAlign = "center";
context.fillStyle = font_style;
context.fillText("left mouse button: sketch", canvas.width * 0.5, canvas.height * 0.5 - 100);
context.fillText("other mouse buttons: eraser", canvas.width * 0.5, canvas.height * 0.5 - 60);
context.fillText("esc: clear", canvas.width * 0.5, canvas.height * 0.5 - 20);
context.fillText("ctrl+z: undo", canvas.width * 0.5, canvas.height * 0.5 + 20);
context.fillText("ctrl+shift+z: redo", canvas.width * 0.5, canvas.height * 0.5 + 60);
context.fillText(window.location.hostname + "/roomname: collaborate online", canvas.width * 0.5, canvas.height * 0.5 + 100);
context.fillText(extra_info, canvas.width * 0.5, canvas.height - 100);
context.fillText(version, canvas.width - 40, canvas.height - 40);
}
};
function init() {
offscreen_canvas.width = canvas.width;
offscreen_canvas.height = canvas.height;
api.draw_background_info("");
}
window.onload = function () {
init();
}