-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (116 loc) · 4.57 KB
/
Copy pathscript.js
File metadata and controls
133 lines (116 loc) · 4.57 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
const canvas = document.querySelector("canvas"),
toolBtns = document.querySelectorAll(".tool"),
fillColor = document.querySelector("#fill-color"),
sizeSlider = document.querySelector("#size-slider"),
colorBtns = document.querySelectorAll(".colors .option"),
colorPicker = document.querySelector("#color-picker"),
clearCanvas = document.querySelector(".clear-canvas"),
saveImg = document.querySelector(".save-img"),
ctx = canvas.getContext("2d");
let prevMouseX, prevMouseY, snapshot,
isDrawing = false,
selectedTool = "brush",
brushWidth = 5,
selectedColor = '#000';
const setCanvasBackground = () => {
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = selectedColor;
};
window.addEventListener("load", () => {
// return viewable w and h off el
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
setCanvasBackground();
});
const drawRect = (e) => {
if (!fillColor.checked) {
return ctx.strokeRect(e.offsetX, e.offsetY, prevMouseX - e.offsetX, prevMouseY - e.offsetY);
}
ctx.fillRect(e.offsetX, e.offsetY, prevMouseX - e.offsetX, prevMouseY - e.offsetY);
};
const drawCircle = (e) => {
ctx.beginPath();
// get radius fo circle using pointer (sqrt = square root, pow method returns value of 'x' to power of 'y')
let radius = Math.sqrt(Math.pow((prevMouseX - e.offsetX), 2) + Math.pow((prevMouseY - e.offsetY), 2))
ctx.arc(prevMouseX, prevMouseY, radius, 50, 0, 2 * Math.PI);
ctx.stroke();
fillColor.checked ? ctx.fill() : ctx.stroke();
};
const drawTriangle = (e) => {
ctx.beginPath();
ctx.moveTo(prevMouseX, prevMouseY); // move triangle closer to pointer
ctx.lineTo(e.offsetX, e.offsetY); // first line
ctx.lineTo(prevMouseX * 2 - e.offsetX, e.offsetY); // second (bottom) line
ctx.closePath(); // third line (close path between line 1 and 2)
fillColor.checked ? ctx.fill() : ctx.stroke();
};
// NEW creates a straight line!
const drawLine = (e) => {
ctx.beginPath();
ctx.moveTo(prevMouseX, prevMouseY); // move line closer to pointer
ctx.lineTo(e.offsetX, e.offsetY); // first line
ctx.stroke();
};
const startDraw = (e) => {
isDrawing = true;
prevMouseX = e.offsetX;
prevMouseY = e.offsetY;
ctx.beginPath(); // begin new path when picking up brush
ctx.lineWidth = brushWidth;
ctx.strokeStyle = selectedColor;
ctx.fillStyle = selectedColor;
ctx.canvas.mozOpaque = true; // Add this line for Firefox compatibility
ctx.willReadFrequently = true; // Set willReadFrequently attribute to improve performance
snapshot = ctx.getImageData(0, 0, canvas.width, canvas.height);
};
const drawing = (e) => {
if(!isDrawing) return;
ctx.putImageData(snapshot, 0, 0);
if(selectedTool === "brush" || selectedTool === "eraser") {
ctx.strokeStyle = selectedTool === "eraser" ? "#fff" : selectedColor;
ctx.lineTo(e.offsetX, e.offsetY); // create line with pointer
ctx.stroke(); // fill line with color
} else if (selectedTool === "rectangle") {
drawRect(e);
} else if (selectedTool === "circle") {
drawCircle(e);
} else if (selectedTool === "triangle") {
drawTriangle(e);
} else if (selectedTool === "line") {
drawLine(e);
}
};
toolBtns.forEach(btn =>{
btn.addEventListener("click", () => {
document.querySelector(".options .active").classList.remove("active");
btn.classList.add("active");
selectedTool = btn.id;
console.log(btn.id);
})
});
sizeSlider.addEventListener("change", () => brushWidth = sizeSlider.value);
colorBtns.forEach(btn => {
btn.addEventListener("click", () => {
document.querySelector(".options .selected").classList.remove("selected");
btn.classList.add("selected");
selectedColor = window.getComputedStyle(btn).getPropertyValue("background-color");
})
})
colorPicker.addEventListener("change", () => {
colorPicker.parentElement.style.background = colorPicker.value;
colorPicker.parentElement.click();
});
clearCanvas.addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
setCanvasBackground();
});
saveImg.addEventListener("click", () => {
const link = document.createElement("a"); // create <a> el
link.download = `${Date.now()}.jpg`; // current date link download value
link.href = canvas.toDataURL(); // pass canvas data
link.click(); // click link to download
});
canvas.addEventListener("mousedown", startDraw);
canvas.addEventListener("mousemove", drawing);
canvas.addEventListener("mouseup", () => isDrawing = false);