-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (55 loc) · 1.52 KB
/
Copy pathscript.js
File metadata and controls
62 lines (55 loc) · 1.52 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
const canvas = document.querySelector("#canvas");
const ctx = canvas.getContext("2d");
const penColor = document.querySelector('input[name="penColor"]');
const penWidth = document.querySelector('input[name="penWidth"]');
const saver = document.querySelector("#saver");
ctx.strokeFill = "#000000";
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.lineWidth = 5;
let pen = {
x: 0,
y: 0,
down: false,
};
saver.addEventListener("click", saveFile);
canvas.addEventListener("mousedown", penDown);
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("mouseup", noDown);
canvas.addEventListener("mouseout", noDown);
function saveFile() {
let image = canvas.toDataURL();
let a = document.createElement("a");
a.setAttribute("download", "image.png");
a.setAttribute(
"href",
canvas.toDataURL("image/png").replace("image/png", "image/octate-stream")
);
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
document.getElementById("myImage").src = image;
console.log("ok");
}
function noDown() {
console.log("out");
pen.down = false;
}
function penDown(e) {
pen.down = true;
[pen.x, pen.y] = [e.offsetX, e.offsetY];
console.log("down");
ctx.strokeStyle = penColor.value;
ctx.lineWidth = penWidth.value;
console.log(pen);
}
function draw(e) {
if (!pen.down) return;
ctx.lineWidth = penWidth.value;
ctx.beginPath();
ctx.moveTo(pen.x, pen.y);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[pen.x, pen.y] = [e.offsetX, e.offsetY];
}