better data handling

This commit is contained in:
2025-01-06 16:57:47 +01:00
parent 21a563e57a
commit 112fae2600
3 changed files with 25 additions and 30 deletions
+3 -7
View File
@@ -75,12 +75,8 @@
</tr> </tr>
</table> </table>
<br /> <br />
<h3>Data (scroll to see all):</h3> <h3>Data:</h3>
<div class="data"> <textarea v-model="data" @input="draw"></textarea>
<template v-for="line, index in data" :key="index">
<input v-model="data[index]" @input="draw" />
</template>
</div>
<br /> <br />
<small <small
><a href="https://github.com/clement-gouin" target="_blank" ><a href="https://github.com/clement-gouin" target="_blank"
@@ -93,7 +89,7 @@
- 2025</small - 2025</small
> >
</main> </main>
<canvas width="400" height="400" ref="canvas"></canvas> <canvas ref="canvas"></canvas>
<h1 id="too_small">Window is too small</h1> <h1 id="too_small">Window is too small</h1>
</div> </div>
</body> </body>
+20 -17
View File
@@ -125,14 +125,13 @@ let app = {
title: "", title: "",
color: utils.randomColor(), color: utils.randomColor(),
}, },
data: [], data: "",
}; };
}, },
computed: {}, computed: {},
watch: { watch: {
config: { config: {
handler() { handler() {
this.makeData();
this.draw(); this.draw();
}, },
deep: true, deep: true,
@@ -151,17 +150,6 @@ let app = {
this.config.color = utils.randomColor(); this.config.color = utils.randomColor();
this.draw(); this.draw();
}, },
makeData() {
const targetSize =
parseInt(this.config.grid) * parseInt(this.config.grid);
if (this.data.length > targetSize) {
this.data = this.data.slice(0, targetSize);
} else if (this.data.length < targetSize) {
this.data = this.data.concat(
Array(targetSize - this.data.length).fill("")
);
}
},
draw() { draw() {
const ctx = this.$refs.canvas?.getContext("2d"); const ctx = this.$refs.canvas?.getContext("2d");
if (!ctx) { if (!ctx) {
@@ -227,12 +215,28 @@ let app = {
); );
} }
if (this.data.length) { if (this.data.trim().length) {
const data = utils.shuffleSeeded(this.data, parseInt(this.config.seed)); const rawData = this.data.trim().split("\n");
const targetSize =
parseInt(this.config.grid) * parseInt(this.config.grid);
const data = utils.shuffleSeeded(
rawData.concat(
Array(Math.max(0, targetSize - rawData.length)).fill("")
),
parseInt(this.config.seed)
);
const cellSize = gridSize / columns; const cellSize = gridSize / columns;
const cellMargin = cellSize * 0.05; const cellMargin = cellSize * 0.05;
for (let index = 0; index < data.length; index++) { console.log(data.length);
for (
let index = 0;
index < Math.min(targetSize, data.length);
index++
) {
const element = data[index]; const element = data[index];
if (element) { if (element) {
const x = index % columns; const x = index % columns;
@@ -262,7 +266,6 @@ let app = {
}, },
mounted: function () { mounted: function () {
console.log("app mounted"); console.log("app mounted");
this.makeData();
setTimeout(this.showApp); setTimeout(this.showApp);
addEventListener("resize", this.draw); addEventListener("resize", this.draw);
}, },
+2 -6
View File
@@ -186,13 +186,9 @@ a {
color: inherit; color: inherit;
} }
.data { textarea {
width: 100%; width: 100%;
border: 1px solid black; height: 20rem;
max-height: 20rem;
overflow-y: scroll;
display: flex;
flex-direction: column;
} }
main, main,