-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.js
More file actions
312 lines (245 loc) · 9.24 KB
/
Main.js
File metadata and controls
312 lines (245 loc) · 9.24 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Layer defaults
let layers;
let isSelecting = false;
let selectStartGridSquare;
let selectEndGridSquare;
let selectionBox;
let selectedSquares = [];
let undoStack = [];
let redoStack = [];
let topBarHeight = 180;
let bottomBarHeight = 65;
// default layer
let currentLayer;
document.addEventListener('DOMContentLoaded', function () {
initializeToolbars();
initializeGrid();
updateGridLayout();
writeToGrid();
});
loadLayersFromLocalStorage();
resizeGrid();
function initializeGrid() {
Object.keys(layers).forEach(layerId => {
const layer = layers[layerId];
const gridContainer = document.getElementById(layerId);
for (let i = 0; i < 625; i++) {
let gridSquare = document.createElement('div');
gridSquare.classList.add('grid-square');
gridSquare.dataset.layer = layerId;
gridSquare.dataset.index = i;
gridSquare.id = `${layerId}-${i}`;
gridContainer.appendChild(gridSquare);
}
});
// add event listener to grid wrapper
const gridWrapper = document.getElementById('grid-wrapper');
gridWrapper.addEventListener('mousedown', selectStart);
gridWrapper.addEventListener('mouseup', selectEnd);
gridWrapper.addEventListener('mousemove', selectMove);
// add the select box to the grid wrapper
selectionBox = document.createElement('div');
selectionBox.classList.add('selection-box');
selectionBox.style.display = 'none';
selectionBox.tabIndex = 0;
gridWrapper.appendChild(selectionBox);
document.addEventListener('mousedown', function (event) {
// check if the mouse is outside the grid wrapper
if (!event.target.closest('#grid-wrapper')) {
// clear all selected grid squares
selectedSquares.forEach(gridSquare => {
gridSquare.classList.remove('selected');
});
selectedSquares = [];
// hide the selection box
document.getElementsByClassName('selection-box')[0].style.display = 'none';
}
});
// add handler for keyboard input
selectionBox.addEventListener('keydown', handleKeyDown);
document.addEventListener("keyup", handleKeyUp);
}
function updateGridLayout() {
for (let layerId in layers) {
if (layers.hasOwnProperty(layerId)) {
const layer = layers[layerId];
const scale = layer.scale;
const gridContainer = document.getElementById(layerId);
if (gridContainer) {
const gridSize = gridContainer.offsetWidth / scale;
gridContainer.style.gridTemplateColumns = `repeat(25, ${gridSize}px)`;
gridContainer.style.gridTemplateRows = `repeat(25, ${gridSize}px)`;
gridContainer.style.fontSize = `${gridSize * 0.8}px`; // Set font size to 80% of grid size
}
// Set the hue of the grid text
gridContainer.style.color = `hsl(${layer.hue}, 100%, 50%)`;
const layerToolbar = document.getElementById(`toolbar-container-${layerId}`);
if (layerToolbar) {
updateSliderThumbBackground(layerId, layer.hue);
}
// Set the hue of the grid outline
const gridSquares = document.querySelectorAll(`#${layerId} .grid-square`);
gridSquares.forEach(gridSquare => {
gridSquare.style.borderColor = `hsl(${layer.hue}, 100%, 50%, 0.2)`;
});
}
}
}
// Call resizeGrid on window resize
window.addEventListener('resize', resizeGrid);
function resizeGrid() {
// Get the grid wrapper and viewport dimensions
const gridWrapper = document.getElementById('grid-wrapper');
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Get the toolbar heights
let topToolBarheight = document.getElementById('toolbar') ? document.getElementById('toolbar').clientHeight : 134;
let bottomToolBarheight = document.getElementById('bottom-toolbar') ? document.getElementById('bottom-toolbar').clientHeight : 65;
// Set default toolbar heights if they are not found
topToolBarheight = topToolBarheight > 0 ? topToolBarheight : 134;
bottomToolBarheight = bottomToolBarheight > 0 ? bottomToolBarheight + 37 : 65;
// Determine the smaller dimension and calculate 75% of it
const size = Math.min(viewportWidth - 50, (viewportHeight - topToolBarheight - bottomToolBarheight ));
gridWrapper.style.width = size + 'px';
gridWrapper.style.height = size + 'px';
updateGridLayout(); // Update grid layout based on new size
updateSelectionBox(); // Update selection box based on new size
}
function saveLayersToLocalStorage() {
// for each layer, update the layer content
Object.keys(layers).forEach(layerId => {
const layer = layers[layerId];
layer.content = "";
for (let i = 0; i < 625; i++) {
const gridSquare = document.getElementById(`${layerId}-${i}`);
layer.content += gridSquare.innerText ? gridSquare.innerText : ' ';
}
});
const layersData = JSON.stringify(layers);
localStorage.setItem('layersData', layersData);
localStorage.setItem('undoStack', JSON.stringify(undoStack.slice(-500)));
}
function loadLayersFromLocalStorage() {
const layersData = localStorage.getItem('layersData');
const undoStackData = localStorage.getItem('undoStack');
if (undoStackData && undoStackData.length > undoStack.length) {
undoStack = JSON.parse(undoStackData);
}
if (layersData) {
layers = JSON.parse(layersData);
toast('Saved data found. Yay!');
} else {
layers = {
layer1: {
scale: 25,
hue: 0,
content: ' '.repeat(625)
},
layer2: {
scale: 25,
hue: 0,
content: ' '.repeat(625)
},
layer3: {
scale: 25,
hue: 0,
content: ' '.repeat(625)
}
};
}
// Populate the grid with the content from each layer
if (layers[0]) {
writeToGrid();
}
}
function writeToGrid() {
// Populate the grid with the content from each layer
Object.keys(layers).forEach(layerId => {
const layer = layers[layerId];
for (let i = 0; i < 625; i++) {
const gridSquare = document.getElementById(`${layerId}-${i}`);
gridSquare.innerText = layer.content[i];
}
//set the hue number input
const hueNumber = document.getElementById(`hueNumber-${layerId}`);
hueNumber.value = layer.hue;
//set the scale number input
const scaleNumber = document.getElementById(`scaleNumber-${layerId}`);
scaleNumber.value = layer.scale;
//set the scale slider
const scaleSlider = document.getElementById(`scaleSlider-${layerId}`);
scaleSlider.value = layer.scale;
//set the hue slider
const hueSlider = document.getElementById(`hueSlider-${layerId}`);
hueSlider.value = layer.hue;
});
}
function undo() {
// if undo stack is not empty
if (undoStack.length > 0) {
const layersData = undoStack.pop();
pushRedo();
console.log(layers);
layers = JSON.parse(layersData);
writeToGrid();
} else {
toast('Nothing to undo');
}
}
function redo() {
// if redo stack is not empty
if (redoStack.length > 0) {
const layersData = redoStack.pop();
pushUndo();
layers = JSON.parse(layersData);
writeToGrid();
} else {
toast('Nothing to redo');
}
}
function clearRedoStack() {
redoStack = [];
}
function clearUndoStack() {
undoStack = [];
}
function pushUndo() {
const layersData = JSON.stringify(layers);
// check if the current state is different from the last state
if (undoStack.length === 0 || undoStack[undoStack.length - 1] !== layersData) {
undoStack.push(layersData);
}
}
function pushRedo() {
const layersData = JSON.stringify(layers);
// check if the current state is different from the last state
if (redoStack.length === 0 || redoStack[redoStack.length - 1] !== layersData) {
redoStack.push(layersData);
}
}
function toast(message) {
// Create a new toast element
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerText = message;
// Append the toast to the body
document.body.appendChild(toast);
// Get all active toasts to determine stacking
const existingToasts = document.querySelectorAll('.toast');
const offset = 10; // Base offset from the bottom
const spacing = 50; // Spacing between each toast
// Position the new toast dynamically
const position = offset + (existingToasts.length - 1) * spacing;
toast.style.bottom = `${position}px`;
// Trigger the fade-in effect
requestAnimationFrame(() => {
toast.classList.add('show');
});
// Remove the toast after 2 seconds
setTimeout(() => {
toast.classList.remove('show'); // Fade out
setTimeout(() => {
toast.remove(); // Remove from DOM after fade-out
}, 500); // Match the CSS transition duration
}, 2000);
}