workking poc
This commit is contained in:
@@ -75,6 +75,13 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<br />
|
<br />
|
||||||
|
<h3>Data (scroll to see all):</h3>
|
||||||
|
<div class="data">
|
||||||
|
<template v-for="line, index in data" :key="index">
|
||||||
|
<input v-model="data[index]" @input="draw" />
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
<small
|
<small
|
||||||
><a href="https://github.com/clement-gouin" target="_blank"
|
><a href="https://github.com/clement-gouin" target="_blank"
|
||||||
>@clement-gouin</a
|
>@clement-gouin</a
|
||||||
|
|||||||
@@ -86,6 +86,32 @@ const utils = {
|
|||||||
"0"
|
"0"
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
randomSeed() {
|
||||||
|
return (Math.random() * 2 ** 32) >>> 0;
|
||||||
|
},
|
||||||
|
splitmix32(a) {
|
||||||
|
return function () {
|
||||||
|
a |= 0;
|
||||||
|
a = (a + 0x9e3779b9) | 0;
|
||||||
|
let t = a ^ (a >>> 16);
|
||||||
|
t = Math.imul(t, 0x21f0aaad);
|
||||||
|
t = t ^ (t >>> 15);
|
||||||
|
t = Math.imul(t, 0x735a2d97);
|
||||||
|
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
shuffleSeeded(array, seed) {
|
||||||
|
array = array.slice();
|
||||||
|
const prng = utils.splitmix32(seed);
|
||||||
|
for (let _ = 0; _ < array.length * 4; _++) {
|
||||||
|
const i1 = Math.floor(prng() * array.length);
|
||||||
|
const i2 = Math.floor(prng() * array.length);
|
||||||
|
const tmp = array[i2];
|
||||||
|
array[i2] = array[i1];
|
||||||
|
array[i1] = tmp;
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let app = {
|
let app = {
|
||||||
@@ -95,16 +121,18 @@ let app = {
|
|||||||
ratio: 1,
|
ratio: 1,
|
||||||
margin: 0.1,
|
margin: 0.1,
|
||||||
grid: 4,
|
grid: 4,
|
||||||
seed: (100000000 * Math.random()).toFixed(0),
|
seed: utils.randomSeed(),
|
||||||
title: "Bingo",
|
title: "Bingo",
|
||||||
color: utils.randomColor(),
|
color: utils.randomColor(),
|
||||||
},
|
},
|
||||||
|
data: [],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {},
|
computed: {},
|
||||||
watch: {
|
watch: {
|
||||||
config: {
|
config: {
|
||||||
handler() {
|
handler() {
|
||||||
|
this.makeData();
|
||||||
this.draw();
|
this.draw();
|
||||||
},
|
},
|
||||||
deep: true,
|
deep: true,
|
||||||
@@ -123,6 +151,17 @@ 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) {
|
||||||
@@ -156,24 +195,24 @@ let app = {
|
|||||||
|
|
||||||
ctx.lineWidth = gridSize * 0.005;
|
ctx.lineWidth = gridSize * 0.005;
|
||||||
|
|
||||||
|
const startX = (width - gridSize) / 2;
|
||||||
|
const startY = (height - gridSize) * (this.config.title ? 2 / 3 : 1 / 2);
|
||||||
|
|
||||||
|
const columns = parseInt(this.config.grid);
|
||||||
|
|
||||||
utils.strokeGrid(
|
utils.strokeGrid(
|
||||||
ctx,
|
ctx,
|
||||||
(width - gridSize) / 2,
|
startX,
|
||||||
(height - gridSize) * (this.config.title ? 2 / 3 : 1 / 2),
|
startY,
|
||||||
gridSize,
|
gridSize,
|
||||||
gridSize,
|
gridSize,
|
||||||
parseInt(this.config.grid),
|
columns,
|
||||||
parseInt(this.config.grid)
|
columns
|
||||||
);
|
);
|
||||||
|
|
||||||
ctx.lineWidth = gridSize * 0.01;
|
ctx.lineWidth = gridSize * 0.01;
|
||||||
|
|
||||||
ctx.strokeRect(
|
ctx.strokeRect(startX, startY, gridSize, gridSize);
|
||||||
(width - gridSize) / 2,
|
|
||||||
(height - gridSize) * (this.config.title ? 2 / 3 : 1 / 2),
|
|
||||||
gridSize,
|
|
||||||
gridSize
|
|
||||||
);
|
|
||||||
|
|
||||||
if (this.config.title) {
|
if (this.config.title) {
|
||||||
ctx.font = `bold ${gridSize * 0.1}px serif`;
|
ctx.font = `bold ${gridSize * 0.1}px serif`;
|
||||||
@@ -183,6 +222,28 @@ let app = {
|
|||||||
ctx.fillText(this.config.title, width / 2, (height - gridSize) / 3);
|
ctx.fillText(this.config.title, width / 2, (height - gridSize) / 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.data.length) {
|
||||||
|
const data = utils.shuffleSeeded(this.data, parseInt(this.config.seed));
|
||||||
|
const cellSize = gridSize / columns;
|
||||||
|
const cellMargin = cellSize * 0.05;
|
||||||
|
|
||||||
|
for (let index = 0; index < data.length; index++) {
|
||||||
|
const element = data[index];
|
||||||
|
if (element) {
|
||||||
|
const x = index % columns;
|
||||||
|
const y = Math.floor(index / columns);
|
||||||
|
utils.fitText(
|
||||||
|
ctx,
|
||||||
|
startX + x * cellSize + cellMargin,
|
||||||
|
startY + y * cellSize + cellMargin,
|
||||||
|
cellSize - cellMargin * 2,
|
||||||
|
cellSize - cellMargin * 2,
|
||||||
|
element
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.font = `bold ${gridSize * 0.02}px sans-serif`;
|
ctx.font = `bold ${gridSize * 0.02}px sans-serif`;
|
||||||
ctx.textAlign = "right";
|
ctx.textAlign = "right";
|
||||||
ctx.textBaseline = "bottom";
|
ctx.textBaseline = "bottom";
|
||||||
@@ -196,6 +257,7 @@ 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);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user