-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (109 loc) · 5.44 KB
/
index.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
var canvas = document.getElementById("draw");
var ctx = canvas.getContext("2d");
let color = "#000";
let offsetX = canvas.offsetLeft;
let offsetY = canvas.offsetTop;
let brushthickness = 7;
const erase = () => (ctx.globalCompositeOperation = "destination-out");
//set current color
document.querySelector(".color-btn div").style.backgroundColor = color;
resize();
function sizeList() {
document.querySelector(".size-list").classList.toggle("show-list");
brushSize();
}
//*************************************************************************************************
//*************************************** SET BRUSH SIZE ******************************************
//*************************************************************************************************
function brushSize() {
var brushSet = document.getElementsByClassName("size");
Array.prototype.forEach.call(brushSet, function (element) {
element.addEventListener("click", function () {
brushthickness = element.getAttribute("style").substr(11, 2);
console.log(brushthickness);
});
});
}
//*************************************************************************************************
//**************************************** SET COLOR TO PALETTE ***********************************
//*************************************************************************************************
function setActiveColor() {
document.querySelector(".color-btn div").style.backgroundColor = color;
ctx.strokeStyle = color;
ctx.globalCompositeOperation = "source-over";
}
//*************************************************************************************************
//**************************************** SET COLOR TO BRUSH *************************************
//*************************************************************************************************
function setColor() {
var palette = document.getElementsByClassName("color");
Array.prototype.forEach.call(palette, function (element) {
element.addEventListener("click", function () {
color = element.getAttribute("style").split("--set-color:")[1];
setActiveColor();
});
});
}
//*************************************************************************************************
//****************************************** COLOR PICKER *****************************************
//*************************************************************************************************
function colorPick() {
color = document.getElementById("color-picker").value;
setActiveColor();
}
//*************************************************************************************************
//******************************************* RESIZE CANVAS ***************************************
//*************************************************************************************************
function resize() {
ctx.canvas.width = window.innerWidth - 20;
ctx.canvas.height = window.innerHeight;
}
//*************************************************************************************************
//**************************************** SET CURSOR POSITION *************************************
//*************************************************************************************************
// initialize position as 0,0
var pos = { x: 0, y: 0 };
// new position from mouse events
function setPosition(e) {
pos.x = parseInt(e.clientX - offsetX);
pos.y = parseInt(e.clientY - offsetY);
}
//*************************************************************************************************
//********************************************** DRAW *********************************************
//*************************************************************************************************
function draw(e) {
if (e.buttons !== 1) return; // if mouse is not clicked, do not go further
ctx.beginPath(); // begin the drawing path
ctx.lineWidth = brushthickness; // width of line
ctx.lineCap = "round"; // rounded end cap
ctx.strokeStyle = color; // hex color of line
ctx.moveTo(pos.x, pos.y); // from position
setPosition(e);
ctx.lineTo(pos.x, pos.y); // to position
ctx.closePath();
ctx.stroke(); // draw it!
}
//*************************************************************************************************
//************************************** DOWNLOAD CANVAS ******************************************
//*************************************************************************************************
function onSave() {
const link = document.createElement('a');
link.download = 'sketch.png';
link.href = canvas.toDataURL();
link.click();
link.delete;
}
//*************************************************************************************************
//***************************************** EVENT LISTENERS ***************************************
//*************************************************************************************************
// add window event listener to trigger when window is resized
window.addEventListener("resize", resize);
// add event listeners to trigger on different mouse events
document.addEventListener("mousemove", draw);
document.addEventListener("mousedown", setPosition);
document.addEventListener("mouseenter", setPosition);
document.addEventListener("touchmove", draw);
document.addEventListener("touchend", setPosition);
document.addEventListener("touchstart", setPosition);
document.getElementById("color-picker").addEventListener("change", colorPick);
setColor();