alpha
Login
or
Join now
flo-bit.dev
/
mandala
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/flo-bit/mandala.
flo-bit.github.io/mandala/
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
load settings with file, small fixes
author
Florian
date
3 years ago
(Mar 27, 2023, 4:53 PM +0200)
commit
c71306d9
c71306d96534ee40c2b9e981f5cdd9141d10eddc
parent
3c4ffcea
3c4ffcead58a89db55033fae3f22853cb5a66f19
+125
-83
1 changed file
Expand all
Collapse all
Unified
Split
script.js
+125
-83
script.js
View file
Reviewed
···
83
83
mandalaDrawer.saveAsPNG();
84
84
}
85
85
86
86
+
if (event.key == "f" && (event.metaKey || event.ctrlKey)) {
87
87
+
openFileModal();
88
88
+
}
89
89
+
90
90
+
if (event.key == "b" && (event.metaKey || event.ctrlKey)) {
91
91
+
openBrushModal();
92
92
+
}
93
93
+
86
94
if (event.key == "+" && (event.metaKey || event.ctrlKey)) {
87
95
event.preventDefault();
88
96
view.zoom *= 1.1;
···
118
126
});
119
127
120
128
let mirrorButton = document.getElementById("mirror-button");
121
121
-
if (mandalaDrawer.mirror) {
122
122
-
mirrorButton.classList.remove("is-light");
123
123
-
} else {
124
124
-
mirrorButton.classList.add("is-light");
125
125
-
}
126
126
-
127
129
mirrorButton.addEventListener("click", function (e) {
128
130
mandalaDrawer.mirror = !mandalaDrawer.mirror;
129
131
if (mandalaDrawer.mirror) {
···
134
136
});
135
137
136
138
let simplifyButton = document.getElementById("simplify-button");
137
137
-
if (mandalaDrawer.simplify) {
138
138
-
simplifyButton.classList.remove("is-light");
139
139
-
} else {
140
140
-
simplifyButton.classList.add("is-light");
141
141
-
}
142
142
-
143
139
simplifyButton.addEventListener("click", function (e) {
144
140
mandalaDrawer.simplify = !mandalaDrawer.simplify;
145
141
if (mandalaDrawer.simplify) {
···
150
146
});
151
147
152
148
let brushColorInput = document.getElementById("brush-color-input");
153
153
-
brushColorInput.value = mandalaDrawer.brushColor;
154
154
-
155
149
brushColorInput.addEventListener("change", function (e) {
156
150
mandalaDrawer.brushColor = brushColorInput.value;
157
151
});
···
197
191
// exportJSONButton.addEventListener("click", mandalaDrawer.saveAsJSON);
198
192
199
193
let backgroundColorInput = document.getElementById("background-color-input");
200
200
-
backgroundColorInput.value = mandalaDrawer.backgroundColor;
201
201
-
202
194
backgroundColorInput.addEventListener("change", function (e) {
203
195
mandalaDrawer.backgroundColor = backgroundColorInput.value;
204
196
···
227
219
updateFilesList(mandalaDrawer);
228
220
}
229
221
222
222
+
function updateUI(mandalaDrawer) {
223
223
+
let brushSizeSlider = document.getElementById("brush-size-bar");
224
224
+
brushSizeSlider.value = mandalaDrawer.brushSizePercentage * 100;
225
225
+
226
226
+
let brushSizeLabel = document.getElementById("brush-size-input");
227
227
+
brushSizeLabel.value = mandalaDrawer.brushSize;
228
228
+
229
229
+
let rotationsSlider = document.getElementById("rotations-bar");
230
230
+
rotationsSlider.value = mandalaDrawer.rotationsPercentage * 100;
231
231
+
232
232
+
let rotationsLabel = document.getElementById("rotations-input");
233
233
+
rotationsLabel.value = mandalaDrawer.rotations;
234
234
+
235
235
+
let brushColorInput = document.getElementById("brush-color-input");
236
236
+
brushColorInput.value = mandalaDrawer.brushColor;
237
237
+
238
238
+
let backgroundColorInput = document.getElementById("background-color-input");
239
239
+
backgroundColorInput.value = mandalaDrawer.backgroundColor;
240
240
+
241
241
+
let mirrorButton = document.getElementById("mirror-button");
242
242
+
if (mandalaDrawer.mirror) {
243
243
+
mirrorButton.classList.remove("is-light");
244
244
+
} else {
245
245
+
mirrorButton.classList.add("is-light");
246
246
+
}
247
247
+
248
248
+
let simplifyButton = document.getElementById("simplify-button");
249
249
+
if (mandalaDrawer.simplify) {
250
250
+
simplifyButton.classList.remove("is-light");
251
251
+
} else {
252
252
+
simplifyButton.classList.add("is-light");
253
253
+
}
254
254
+
}
255
255
+
230
256
function updateFilesList(mandalaDrawer) {
231
257
let filesList = document.getElementById("files-list");
232
258
filesList.innerHTML = "";
233
259
234
234
-
/*
235
235
-
<div class="control columns is-mobile is-vcentered">
236
236
-
<div class="column is-narrow">
237
237
-
label class="label">export</label>
238
238
-
</div>
239
239
-
<div class="column">
240
240
-
<button class="button is-danger" id="save-button">
241
241
-
<span class="icon">
242
242
-
<i class="mdi mdi-file"></i>
243
243
-
</span>
244
244
-
245
245
-
<span>open</span>
246
246
-
</button>
247
247
-
</div>
248
248
-
</div>
249
249
-
*/
250
260
for (let file of mandalaDrawer.files) {
251
261
let columns = document.createElement("div");
252
262
columns.classList.add("columns");
···
317
327
let brushSizeInput = document.getElementById("brush-size-input");
318
328
let brushSizeBar = document.getElementById("brush-size-bar");
319
329
320
320
-
brushSizeBar.value = mandalaDrawer.brushSizePercentage * 100;
321
321
-
brushSizeInput.value = mandalaDrawer.brushSize;
322
322
-
323
330
brushSizeBar.addEventListener("click", function (e) {
324
331
let brushSize = e.offsetX / brushSizeBar.offsetWidth;
325
332
mandalaDrawer.setPercentageBrushSize(brushSize);
···
357
364
let rotationsInput = document.getElementById("rotations-input");
358
365
let rotationsBar = document.getElementById("rotations-bar");
359
366
360
360
-
rotationsBar.value = mandalaDrawer.rotationsPercentage * 100;
361
361
-
rotationsInput.value = mandalaDrawer.rotations;
362
362
-
363
367
rotationsBar.addEventListener("click", function (e) {
364
368
let rotations = e.offsetX / rotationsBar.offsetWidth;
365
369
mandalaDrawer.setPercentageRotations(rotations);
···
407
411
constructor() {
408
412
this.tool = new Tool();
409
413
410
410
-
let json = localStorage.getItem("brush-settings");
411
411
-
let settings = json === null ? {} : JSON.parse(json);
412
412
-
413
414
this.path = undefined;
414
415
this.paths = undefined;
415
416
416
416
-
this._simplify = settings.simplify ?? true;
417
417
-
this._mirror = settings.mirror ?? true;
418
418
-
419
419
-
this._brushColor = settings.brushColor ?? "#FF0000";
420
420
-
421
417
this.minRotations = 0;
422
418
this.maxRotations = 32;
423
419
424
420
this.maxBrushSize = 15;
425
421
this.minBrushSize = 0.2;
426
422
427
427
-
this.files = settings.files ?? [];
428
428
-
429
429
-
this.rotations = settings.rotations ?? 8;
430
430
-
this.brushSize = settings.brushSize ?? 1;
431
431
-
432
432
-
this.backgroundColor = settings.backgroundColor ?? "#FFFFFF";
423
423
+
this.loadFiles();
424
424
+
this.loadFile();
433
425
434
434
-
console.log(this.files);
435
426
this.setupTool();
436
427
}
437
428
···
476
467
this._brushSize = Math.round(brushSize * 10) / 10;
477
468
}
478
469
470
470
+
calculateBrushSizePercentage() {
471
471
+
let percentage =
472
472
+
(this.brushSize - this.minBrushSize) /
473
473
+
(this.maxBrushSize - this.minBrushSize);
474
474
+
percentage = Math.max(0, percentage);
475
475
+
percentage = Math.min(1, percentage);
476
476
+
477
477
+
this.brushSizePercentage = percentage;
478
478
+
}
479
479
+
479
480
setPercentageRotations(percentage) {
480
481
if (isNaN(percentage)) percentage = 0;
481
482
···
490
491
this.rotations = Math.round(rotations);
491
492
}
492
493
494
494
+
calculateRotationsPercentage() {
495
495
+
let percentage =
496
496
+
(this.rotations - this.minRotations) /
497
497
+
(this.maxRotations - this.minRotations);
498
498
+
percentage = Math.max(0, percentage);
499
499
+
percentage = Math.min(1, percentage);
500
500
+
501
501
+
this.rotationsPercentage = percentage;
502
502
+
}
503
503
+
493
504
set backgroundColor(color) {
494
505
this._backgroundColor = color;
495
506
view.element.style.backgroundColor = color;
496
496
-
this.saveSettings();
507
507
+
this.saveFile();
497
508
}
498
509
get backgroundColor() {
499
510
return this._backgroundColor;
···
501
512
502
513
set mirror(mirror) {
503
514
this._mirror = mirror;
504
504
-
this.saveSettings();
515
515
+
this.saveFile();
505
516
}
506
517
get mirror() {
507
518
return this._mirror;
508
519
}
520
520
+
509
521
set rotations(rotations) {
510
522
this._rotations = rotations;
511
511
-
let percentage =
512
512
-
(rotations - this.minRotations) / (this.maxRotations - this.minRotations);
513
513
-
percentage = Math.max(0, percentage);
514
514
-
percentage = Math.min(1, percentage);
515
515
-
516
516
-
this.rotationsPercentage = percentage;
517
517
-
this.saveSettings();
523
523
+
this.calculateRotationsPercentage();
524
524
+
this.saveFile();
518
525
}
519
526
get rotations() {
520
527
return this._rotations;
521
528
}
522
529
set simplify(simplify) {
523
530
this._simplify = simplify;
524
524
-
this.saveSettings();
531
531
+
this.saveFile();
525
532
}
526
533
get simplify() {
527
534
return this._simplify;
528
535
}
529
536
set brushColor(color) {
530
537
this._brushColor = color;
531
531
-
this.saveSettings();
538
538
+
this.saveFile();
532
539
}
533
540
get brushColor() {
534
541
return this._brushColor;
535
542
}
536
543
set brushSize(size) {
537
544
this._brushSize = size;
538
538
-
let percentage =
539
539
-
(size - this.minBrushSize) / (this.maxBrushSize - this.minBrushSize);
540
540
-
percentage = Math.max(0, percentage);
541
541
-
percentage = Math.min(1, percentage);
542
542
-
543
543
-
this.brushSizePercentage = percentage;
544
544
-
this.saveSettings();
545
545
+
this.calculateBrushSizePercentage();
546
546
+
this.saveFile();
545
547
}
546
548
get brushSize() {
547
549
return this._brushSize;
···
650
652
651
653
saveFile(name) {
652
654
let json = paper.project.exportJSON();
653
653
-
localStorage.setItem(name ?? "current-project", json);
655
655
+
let wrapper = {
656
656
+
json: json,
657
657
+
backgroundColor: this.backgroundColor,
658
658
+
brushColor: this.brushColor,
659
659
+
brushSize: this.brushSize,
660
660
+
mirror: this.mirror,
661
661
+
rotations: this.rotations,
662
662
+
simplify: this.simplify,
663
663
+
};
664
664
+
665
665
+
localStorage.setItem(name ?? "current-project", JSON.stringify(wrapper));
654
666
655
667
if (name !== undefined && !this.files.includes(name)) {
656
668
this.files.push(name);
657
657
-
this.saveSettings();
669
669
+
this.saveFiles();
658
670
}
659
671
}
660
672
661
673
loadFile(name) {
662
674
let json = localStorage.getItem(name ?? "current-project");
663
663
-
paper.project.importJSON(json);
675
675
+
let wrapper = JSON.parse(json);
676
676
+
677
677
+
paper.project.importJSON(wrapper.json);
678
678
+
679
679
+
this.setSettings(wrapper);
664
680
}
665
681
666
682
removeFile(name) {
667
683
localStorage.removeItem(name);
668
684
this.files = this.files.filter((f) => f !== name);
669
669
-
this.saveSettings();
685
685
+
this.saveFiles();
670
686
}
671
687
672
688
undo() {
···
677
693
this.saveFile();
678
694
}
679
695
680
680
-
saveSettings() {
696
696
+
setSettings(settings) {
697
697
+
this._simplify = settings.simplify ?? true;
698
698
+
this._mirror = settings.mirror ?? true;
699
699
+
700
700
+
this._brushColor = settings.brushColor ?? "#FF0000";
701
701
+
702
702
+
this._rotations = settings.rotations ?? 8;
703
703
+
this.calculateRotationsPercentage();
704
704
+
this._brushSize = settings.brushSize ?? 1;
705
705
+
this.calculateBrushSizePercentage();
706
706
+
707
707
+
this._backgroundColor = settings.backgroundColor ?? "#FFFFFF";
708
708
+
view.element.style.backgroundColor = this.backgroundColor;
709
709
+
710
710
+
updateUI(this);
711
711
+
}
712
712
+
713
713
+
loadFiles() {
714
714
+
let settings = localStorage.getItem("settings");
715
715
+
if (settings === null) return;
716
716
+
717
717
+
settings = JSON.parse(settings);
718
718
+
this.files = settings.files;
719
719
+
}
720
720
+
saveFiles() {
681
721
let settings = {};
682
682
-
settings.mirror = this.mirror;
683
683
-
settings.simplify = this.simplify;
684
684
-
settings.rotations = this.rotations;
685
685
-
settings.brushSize = this.brushSize;
686
686
-
settings.brushColor = this.brushColor;
687
687
-
settings.backgroundColor = this.backgroundColor;
688
722
settings.files = this.files;
689
723
690
690
-
localStorage.setItem("brush-settings", JSON.stringify(settings));
724
724
+
localStorage.setItem("settings", JSON.stringify(settings));
691
725
}
692
726
}
693
727
···
705
739
706
740
function closeFileModal() {
707
741
closeModal(document.getElementById("file-modal"));
742
742
+
}
743
743
+
744
744
+
function openBrushModal() {
745
745
+
openModal(document.getElementById("brush-modal"));
746
746
+
}
747
747
+
748
748
+
function closeBrushModal() {
749
749
+
closeModal(document.getElementById("brush-modal"));
708
750
}
709
751
710
752
document.addEventListener("DOMContentLoaded", () => {