-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
138 lines (114 loc) · 3.33 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
/* CONSTANTS */
var pictureWidth = 60;
var pictureHeight = 40;
var dotDiameter = 10;
var colors = ["red", "blue", "yellow", "orange", "green", "purple", "brown", "black", "white"];
/* STATE */
var selectedColor = colors[0];
var dots = [];
var countNewDots = 0;
/* CANVAS SETUP */
var canvas = document.getElementById("canvas");
canvas.width = pictureWidth * dotDiameter;
canvas.height = pictureHeight * dotDiameter;
document.getElementById("toolbar").style.width = (pictureWidth * dotDiameter) + "px";
var ctx = canvas.getContext("2d");
drawDots();
/* DRAWING */
function drawDot(x, y, color) {
ctx.beginPath();
ctx.fillStyle = color;
ctx.arc(x, y, dotDiameter, 0, 2 * Math.PI);
ctx.fill();
}
function drawDots() {
var oldAlpha = ctx.globalAlpha;
ctx.globalAlpha = 1;
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = 0.75;
for (var i = 0; i < dots.length; i++) {
var dot = dots[i];
drawDot(dot.c * dotDiameter, dot.r * dotDiameter, dot.color);
}
ctx.globalAlpha = oldAlpha;
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
/* PREVIEW DOT */
canvas.addEventListener("mousemove", function(evt) {
var mousePos = getMousePos(canvas, evt);
var col = Math.floor(mousePos.x / dotDiameter);
var row = Math.floor(mousePos.y / dotDiameter);
drawDots();
drawDot(col * dotDiameter, row * dotDiameter, selectedColor);
});
canvas.addEventListener("mouseleave", function(evt) {
drawDots();
});
/* ADD DOT */
canvas.addEventListener("click", function(evt) {
var mousePos = getMousePos(canvas, evt);
var col = Math.floor(mousePos.x / dotDiameter);
var row = Math.floor(mousePos.y / dotDiameter);
if (!dotAtPosition(row, col)) {
dots.push({
c: col,
r: row,
color: selectedColor
});
countNewDots++;
undoButton.removeAttribute("disabled");
drawDots();
}
});
function dotAtPosition(row, col) {
for (var i = 0; i < dots.length; i++) {
var dot = dots[i];
if (dot.r === row && dot.c === col) {
return dot;
}
}
return null;
}
/* UNDO BUTTON */
var undoButton = document.getElementById("undo");
undoButton.addEventListener("click", function() {
if (countNewDots > 0) {
dots.pop();
if (--countNewDots === 0) {
undoButton.setAttribute("disabled", "");
}
}
drawDots();
});
/* COLOR SELECTOR */
var SELECTED_CLASS = "color-option-selected";
function selectColorOption(color) {
return function() {
selectedColor = color;
var selectedColorOptions = document.getElementsByClassName(SELECTED_CLASS);
for (var i = 0; i < selectedColorOptions.length; i++) {
selectedColorOptions[i].classList.remove(SELECTED_CLASS);
}
document.getElementById("option-" + color).classList.add(SELECTED_CLASS);
};
}
var colorSelector = document.getElementById("color-selector");
for (var i = 0; i < colors.length; i++) {
var color = colors[i];
var colorOption = document.createElement("span");
colorOption.id = "option-" + color;
colorOption.classList.add("dot");
if (color === selectedColor) {
colorOption.classList.add(SELECTED_CLASS);
}
colorOption.style.backgroundColor = color;
colorOption.addEventListener("click", selectColorOption(color));
colorSelector.appendChild(colorOption);
}