-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
263 lines (233 loc) · 9.66 KB
/
script.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
document.addEventListener('DOMContentLoaded', function() {
const paletteArray = [
['rgb(109,177,176)', 'rgb(56,93,112)', 'rgb(105,84,127)', 'rgb(244,101,105)', 'rgb(255,160,132)'],
['rgb(11,87,171)', 'rgb(9,46,117)', 'rgb(232,119,59)', 'rgb(114,33,76)', 'rgb(45,21,57)'],
['rgb(182,47,43)', 'rgb(243,152,0)', 'rgb(196,179,3)', 'rgb(139,216,208)', 'rgb(49,103,105)'],
['rgb(246,172,13)', 'rgb(243,232,3)', 'rgb(4,193,225)', 'rgb(117,228,222)', 'rgb(214,235,179)'],
['rgb(79,53,56)', 'rgb(140,106,107)', 'rgb(220,163,143)', 'rgb(130,151,168)', 'rgb(91,130,163)'],
['rgb(69,67,18)', 'rgb(185,179,103)', 'rgb(251,192,194)', 'rgb(185,84,92)', 'rgb(76,4,26)'],
['rgb(166,50,95)', 'rgb(225,149,188)', 'rgb(222,220,225)', 'rgb(134,170,166)', 'rgb(65,108,117)'],
['rgb(254,235,70)', 'rgb(241,182,26)', 'rgb(143,106,149)', 'rgb(78,44,120)', 'rgb(23,2,67)'],
['rgb(34,18,31)', 'rgb(80,7,89)', 'rgb(22,179,230)', 'rgb(84,222,225)', 'rgb(29,108,103)']
];
const patternsArray = [
'images/patterns/bear.png',
'images/patterns/cat.png',
'images/patterns/fox.png',
'images/patterns/lion.png',
'images/patterns/swan.png'
];
// Random
const random = arr => Math.floor(Math.random()*arr.length);
// Add color to button from data-color
const paletteButtons = document.querySelectorAll('.color');
const addPalette = function() {
[...paletteButtons].forEach(button => {
let bgColor = button.dataset.color;
button.style.backgroundColor = bgColor;
});
};
addPalette();
// Remove checked from color buttons
const removeChecked = (arr) => arr.forEach(el => el.classList.remove('checked'));
// Hide instruction box
const close = document.querySelector('.instructionBtn');
const instructionBox = document.querySelector('aside');
const closeInstruction = () => instructionBox.style.display = 'none';
close.addEventListener('click', closeInstruction);
// Draw pattern
const getPattern = function() {
let index = random(patternsArray);
let pattern = patternsArray[index];
return pattern;
};
// Draw palette
const paletteBtn = document.querySelector('.palette');
const getPalette = function() {
let index = random(paletteArray);
let palette = Array.from(paletteArray[index]);
for (let i = 0; i < paletteButtons.length; i++) {
paletteButtons[i].dataset.color = palette[i];
};
removeChecked([...paletteButtons]);
addPalette();
};
paletteBtn.addEventListener('click', getPalette);
// Canvas
const canvasBox = document.querySelector('.drawing_canvas');
class Drawing {
constructor(pattern) {
this.img = new Image();
this.img.addEventListener('load', () => {
this.createCanvas();
this.canDraw = false;
this.lastX = 0;
this.lastY = 0;
this.setInitialDrawValue();
this.setControls();
this.bindControls();
});
this.img.src = 'images/patterns/transparent.png';
this.pattern = pattern
}
createCanvas() {
this.canvas = document.createElement('canvas');
this.canvas.style.backgroundImage = `url(${this.pattern})`;
this.canvas.width = canvasBox.offsetWidth;
// I don't know why canvasBox.offsetWidth = 0, check letter
this.canvas.height = document.documentElement.clientHeight;
canvasBox.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
}
setInitialDrawValue() {
// Set pattern
this.ctx.drawImage(this.img, 0, 0)
// Initial drawing settings
this.ctx.lineWidth = 5;
this.ctx.lineJoin = 'round';
this.ctx.lineCap = 'round';
this.ctx.strokeStyle = 'rgb(246,172,13)';
}
setControls() {
// Buttons - line thickness
this.bntsLine = [...document.querySelectorAll('.line')];
this.bntsLine[0].classList.add('checked');
// Buttons - line color
this.btnsColor = [...document.querySelectorAll('.color')];
this.btnsColor[0].classList.add('checked');
this.colorPicker = document.querySelector('.user_color');
}
bindControls() {
// Controls
this.colorPicker.addEventListener('change', this.setColor1.bind(this));
this.btnsColor.forEach(btn => btn.addEventListener('click', this.setColor2.bind(this)));
this.bntsLine.forEach(btn => btn.addEventListener('click', this.setThickness.bind(this)));
// Canvas with mouse
this.canvas.addEventListener('mousemove', this.draw.bind(this));
this.canvas.addEventListener('mousedown', this.startDraw.bind(this));
this.canvas.addEventListener('mouseup', this.stopDraw.bind(this));
this.canvas.addEventListener('mouseout', this.stopDraw.bind(this));
// Canvas with mouse
this.canvas.addEventListener("touchmove", this.touch.bind(this));
this.canvas.addEventListener("touchstart", this.startTouch.bind(this));
this.canvas.addEventListener("touchend", this.stopTouch.bind(this));
// Add checked to current color & thickness button
this.addChecked(this.bntsLine);
this.addChecked(this.btnsColor);
}
addChecked(arr) {
for (const btn of arr) {
btn.addEventListener('click', (e) => {
e.currentTarget.classList.add('checked');
for (const el of arr) {
if (el !== e.currentTarget) {
el.classList.remove('checked');
}
};
});
};
}
// Set line color with color picker
setColor1(e) {
this.ctx.strokeStyle = e.target.value;
}
// Set line color with palette button
setColor2(e) {
this.ctx.strokeStyle = e.target.dataset.color;
}
// Set line thickness
setThickness(e) {
this.ctx.lineWidth = e.target.dataset.line;
}
// Preventing scrolling on document.body if the target of a touch event is the canvas
scrollingPrevent(e) {
if (e.target == this.canvas) {
e.preventDefault();
}
}
// Start drawing with mouse
startDraw(e) {
this.canDraw = true;
const position = this.getMousePosition(e);
[this.lastX, this.lastY] = [position.x, position.y];
}
// Start drawing with touch
startTouch(e) {
this.scrollingPrevent(e);
this.canDraw = true;
const position = this.getTouchPosition(e);
[this.lastX, this.lastY] = [position.x, position.y];
}
// Stop drawing with mouse
stopDraw() {
this.canDraw = false;
}
// Stop drawing with touch
stopTouch(e) {
this.scrollingPrevent(e);
this.stopDraw();
}
// Drawing with mouse
draw(e) {
if (!this.canDraw) return;
const position = this.getMousePosition(e);
this.ctx.beginPath();
this.ctx.moveTo(this.lastX, this.lastY);
this.ctx.lineTo(position.x, position.y);
this.ctx.stroke();
[this.lastX, this.lastY] = [position.x, position.y];
}
// Drawing with touch
touch(e) {
this.scrollingPrevent(e);
if (!this.canDraw) return;
const position = this.getTouchPosition(e);
this.ctx.beginPath();
this.ctx.moveTo(this.lastX, this.lastY);
this.ctx.lineTo(position.x, position.y);
this.ctx.stroke();
[this.lastX, this.lastY] = [position.x, position.y];
}
getMousePosition(e) {
const rect = this.canvas.getBoundingClientRect();
const scaleX = this.canvas.width / rect.width;
const scaleY = this.canvas.height / rect.height;
const X = (e.clientX - rect.left)*scaleX;
const Y = (e.clientY - rect.top)*scaleY;
return {
x: X,
y: Y
};
}
getTouchPosition(e) {
const rect = this.canvas.getBoundingClientRect();
const scaleX = this.canvas.width / rect.width;
const scaleY = this.canvas.height / rect.height;
const X = (e.touches[0].clientX - rect.left)*scaleX;
const Y = (e.touches[0].clientY - rect.top)*scaleY;
return {
x: X,
y: Y
}
}
};
// Add first canvas
const drawing = new Drawing(getPattern());
// Clear Canvas, get new pattern
const patternBtn = document.querySelector('.pattern');
const newPattern = function() {
// Add iniltial settings
removeChecked([...document.querySelectorAll('.line')]);
removeChecked([...paletteButtons]);
for (let i = 0; i < [...paletteButtons].length; i++) {
[...paletteButtons][i].dataset.color = paletteArray[3][i];
};
addPalette();
// Delete old canvas
let oldCanvas = document.querySelector('canvas');
canvasBox.removeChild(oldCanvas);
// Add new canvas
const drawing = new Drawing(getPattern());
};
patternBtn.addEventListener('click', newPattern);
});