v2
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, watch } from "vue";
|
||||
import LucideIcon from "./components/LucideIcon.vue";
|
||||
import OutputCanvas from "./components/OutputCanvas.vue";
|
||||
import ConfigTable from "./components/ConfigTable.vue";
|
||||
import type { Config } from "./interfaces";
|
||||
import { DEFAULT_CONFIG } from "./constants";
|
||||
import { getCookie, setCookie } from "./lib/cookies";
|
||||
|
||||
const visible = ref<boolean>(false);
|
||||
const config = ref<Config>(DEFAULT_CONFIG);
|
||||
const data = ref<string>("");
|
||||
|
||||
function saveData() {
|
||||
setCookie("bingo-data", data.value);
|
||||
}
|
||||
|
||||
function loadData() {
|
||||
data.value = getCookie("bingo-data", "");
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadData();
|
||||
});
|
||||
|
||||
watch(data, saveData);
|
||||
|
||||
onMounted(() => {
|
||||
setTimeout(() => {
|
||||
visible.value = true;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main :style="{ display: visible ? 'inherit' : 'none' }">
|
||||
<div class="left">
|
||||
<h1>
|
||||
<LucideIcon name="factory" />
|
||||
Bingo Factory
|
||||
</h1>
|
||||
<ConfigTable v-model="config" />
|
||||
<h3><LucideIcon name="notebook-pen" /> Data:</h3>
|
||||
<textarea v-model="data"></textarea>
|
||||
<br />
|
||||
<small>
|
||||
<a href="https://github.com/clement-gouin" target="_blank"
|
||||
>@clement-gouin</a
|
||||
>
|
||||
-
|
||||
<a href="https://github.com/clement-gouin/bingo" target="_blank"
|
||||
>Github Repository</a
|
||||
>
|
||||
- 2026
|
||||
</small>
|
||||
</div>
|
||||
<OutputCanvas class="right" :config="config" :data="data" />
|
||||
<h1 id="too_small">Window is too small</h1>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
main {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.left {
|
||||
padding: 1.5rem;
|
||||
background-color: var(--background-primary);
|
||||
min-width: 24rem;
|
||||
}
|
||||
|
||||
.right {
|
||||
border: 1px dashed;
|
||||
width: min-content;
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.left,
|
||||
.right {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#too_small {
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
textarea {
|
||||
width: 100%;
|
||||
height: 20rem;
|
||||
}
|
||||
|
||||
@media only screen and ((min-width: 1152px) and (min-height: 726px)) {
|
||||
.left,
|
||||
.right {
|
||||
display: block;
|
||||
}
|
||||
|
||||
#too_small {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,111 @@
|
||||
<script setup lang="ts">
|
||||
import { DEFAULT_CONFIG } from "@/constants";
|
||||
import type { Config } from "@/interfaces";
|
||||
import { getDataCookie, setDataCookie } from "@/lib/cookies";
|
||||
import { randomColor, randomSeed } from "@/lib/random";
|
||||
import { onMounted, watch } from "vue";
|
||||
|
||||
const config = defineModel<Config>({ required: true });
|
||||
|
||||
function newSeed() {
|
||||
config.value.seed = randomSeed().toFixed(0);
|
||||
}
|
||||
|
||||
function newColor() {
|
||||
config.value.color = randomColor();
|
||||
}
|
||||
|
||||
function saveConfig() {
|
||||
setDataCookie("bingo-config", config.value);
|
||||
}
|
||||
|
||||
function loadConfig() {
|
||||
config.value = getDataCookie("bingo-config", DEFAULT_CONFIG);
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadConfig();
|
||||
});
|
||||
|
||||
watch(config, saveConfig, { deep: true });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<table class="config">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Ratio</td>
|
||||
<td>
|
||||
<select v-model="config.ratio">
|
||||
<option value="1">square (1:1)</option>
|
||||
<option value="1.414">portrait (1:1.414)</option>
|
||||
<option value="0.707">landscape (1.414:1)</option>
|
||||
<option value="1.25">phone (4:5)</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Margin</td>
|
||||
<td>
|
||||
<select v-model="config.margin">
|
||||
<option value="0.05">5%</option>
|
||||
<option value="0.1">10%</option>
|
||||
<option value="0.15">15%</option>
|
||||
<option value="0.2">20%</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Grid</td>
|
||||
<td>
|
||||
<select v-model="config.grid">
|
||||
<option value="3">3x3</option>
|
||||
<option value="4">4x4</option>
|
||||
<option value="5">5x5</option>
|
||||
<option value="6">6x6</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Seed</td>
|
||||
<td>
|
||||
<div class="withButton">
|
||||
<input v-model="config.seed" />
|
||||
<button @click="newSeed">new</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Title</td>
|
||||
<td>
|
||||
<input v-model="config.title" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Color</td>
|
||||
<td>
|
||||
<div class="withButton">
|
||||
<input v-model="config.color" />
|
||||
<button @click="newColor">new</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.config td:first-child {
|
||||
text-align: right;
|
||||
padding-right: 1em;
|
||||
}
|
||||
|
||||
.config td select,
|
||||
.config td input {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.withButton {
|
||||
display: flex;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,39 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import * as icons from "lucide-vue-next";
|
||||
|
||||
interface Props {
|
||||
name: string;
|
||||
color?: string;
|
||||
strokeWidth?: string;
|
||||
defaultClass?: string;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
color: "currentColor",
|
||||
strokeWidth: "2",
|
||||
defaultClass: "lucide",
|
||||
});
|
||||
|
||||
function kebab2camel(kebab: string): string {
|
||||
return kebab
|
||||
.split("-")
|
||||
.map(
|
||||
(item) =>
|
||||
item.charAt(0).toUpperCase() + item.slice(1).toLowerCase(),
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
// @ts-expect-error: cannot infer type of all exported data
|
||||
const icon = computed(() => icons[kebab2camel(props.name)]);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<component
|
||||
:is="icon"
|
||||
:color="color"
|
||||
:stroke-width="strokeWidth"
|
||||
:default-class="defaultClass"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,125 @@
|
||||
<script setup lang="ts">
|
||||
import type { Config } from "@/interfaces";
|
||||
import { fitText, strokeGrid } from "@/lib/canvas";
|
||||
import { shuffleSeeded } from "@/lib/random";
|
||||
import { onMounted, useTemplateRef, watch } from "vue";
|
||||
|
||||
interface Props {
|
||||
config: Config;
|
||||
data: string;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const canvas = useTemplateRef<HTMLCanvasElement>("canvas");
|
||||
|
||||
function draw() {
|
||||
const ctx = canvas.value?.getContext("2d");
|
||||
if (!canvas.value || !ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
const height = window.innerHeight;
|
||||
const width = height / parseFloat(props.config.ratio);
|
||||
|
||||
canvas.value.height = height;
|
||||
canvas.value.width = width;
|
||||
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
ctx.fillStyle = "white";
|
||||
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
|
||||
ctx.fillStyle = props.config.color + "33";
|
||||
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
|
||||
const margin = parseFloat(props.config.margin);
|
||||
const gridSize = Math.min(
|
||||
(1 - margin * 2) * width,
|
||||
(1 - margin * 2) * height,
|
||||
);
|
||||
|
||||
ctx.lineJoin = "miter";
|
||||
|
||||
ctx.strokeStyle = props.config.color;
|
||||
|
||||
ctx.lineWidth = gridSize * 0.005;
|
||||
|
||||
const startX = (width - gridSize) / 2;
|
||||
const startY = (height - gridSize) * (props.config.title ? 2 / 3 : 1 / 2);
|
||||
|
||||
const columns = parseInt(props.config.grid);
|
||||
|
||||
strokeGrid(ctx, startX, startY, gridSize, gridSize, columns, columns);
|
||||
|
||||
ctx.lineWidth = gridSize * 0.01;
|
||||
|
||||
ctx.strokeRect(startX, startY, gridSize, gridSize);
|
||||
|
||||
ctx.fillStyle = props.config.color;
|
||||
|
||||
if (props.config.title) {
|
||||
fitText(
|
||||
ctx,
|
||||
startX,
|
||||
(height - gridSize) / 4,
|
||||
gridSize,
|
||||
(height - gridSize) / 3,
|
||||
props.config.title,
|
||||
);
|
||||
}
|
||||
|
||||
if (props.data.trim().length) {
|
||||
const rawData = props.data.trim().split("\n");
|
||||
|
||||
const targetSize =
|
||||
parseInt(props.config.grid) * parseInt(props.config.grid);
|
||||
|
||||
const data = shuffleSeeded(
|
||||
rawData.concat(
|
||||
Array(Math.max(0, targetSize - rawData.length)).fill(""),
|
||||
),
|
||||
parseInt(props.config.seed),
|
||||
);
|
||||
const cellSize = gridSize / columns;
|
||||
const cellMargin = cellSize * 0.05;
|
||||
|
||||
for (
|
||||
let index = 0;
|
||||
index < Math.min(targetSize, data.length);
|
||||
index++
|
||||
) {
|
||||
const element = data[index];
|
||||
if (element) {
|
||||
const x = index % columns;
|
||||
const y = Math.floor(index / columns);
|
||||
fitText(
|
||||
ctx,
|
||||
startX + x * cellSize + cellMargin,
|
||||
startY + y * cellSize + cellMargin,
|
||||
cellSize - cellMargin * 2,
|
||||
cellSize - cellMargin * 2,
|
||||
element,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.font = `bold ${(gridSize * 0.02).toString()}px sans-serif`;
|
||||
ctx.textAlign = "right";
|
||||
ctx.textBaseline = "bottom";
|
||||
ctx.fillStyle = props.config.color + "44";
|
||||
ctx.fillText("clement-gouin.github.io/bingo", width * 0.99, height * 0.99);
|
||||
}
|
||||
|
||||
watch(props, draw, { deep: true });
|
||||
|
||||
onMounted(() => {
|
||||
draw();
|
||||
addEventListener("resize", draw);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<canvas ref="canvas"></canvas>
|
||||
</template>
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { Config } from "./interfaces";
|
||||
import { randomColor, randomSeed } from "./lib/random";
|
||||
|
||||
export const DEFAULT_CONFIG: Config = {
|
||||
ratio: "1",
|
||||
margin: "0.1",
|
||||
grid: "4",
|
||||
seed: randomSeed().toFixed(0),
|
||||
title: "",
|
||||
color: randomColor(),
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
export interface Config {
|
||||
ratio: string;
|
||||
margin: string;
|
||||
grid: string;
|
||||
seed: string;
|
||||
title: string;
|
||||
color: string;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
export function strokeGrid(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
cols: number,
|
||||
rows: number,
|
||||
): void {
|
||||
ctx.beginPath();
|
||||
for (let c = 1; c < cols; c++) {
|
||||
ctx.moveTo(Math.round(x + (w / cols) * c), y);
|
||||
ctx.lineTo(Math.round(x + (w / cols) * c), y + h);
|
||||
}
|
||||
for (let r = 1; r < rows; r++) {
|
||||
ctx.moveTo(x, Math.round(y + (h / rows) * r));
|
||||
ctx.lineTo(x + w, Math.round(y + (h / rows) * r));
|
||||
}
|
||||
ctx.rect(x, y, w, h);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
export function fitText(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
txt: string,
|
||||
): void {
|
||||
ctx.textAlign = "center";
|
||||
ctx.textBaseline = "top";
|
||||
ctx.font = "bold 100px serif";
|
||||
|
||||
let totalSize = 0;
|
||||
const spaceSize = ctx.measureText(" ").width;
|
||||
|
||||
const words = txt.split(" ").map((word) => {
|
||||
const size = ctx.measureText(word).width;
|
||||
if (totalSize > 0) {
|
||||
totalSize += spaceSize;
|
||||
}
|
||||
totalSize += size;
|
||||
return {
|
||||
text: word,
|
||||
size: size,
|
||||
};
|
||||
});
|
||||
|
||||
const lines = words.map((word, i) => {
|
||||
const targetLineCount = i + 1;
|
||||
const targetSize = totalSize / targetLineCount;
|
||||
const text: string[] = [];
|
||||
let lineSize = 0;
|
||||
let maxSize = 0;
|
||||
let lineCount = 0;
|
||||
words.forEach((word) => {
|
||||
if (
|
||||
lineSize == 0 ||
|
||||
lineSize + spaceSize + word.size > targetSize
|
||||
) {
|
||||
lineCount += 1;
|
||||
text.push(word.text);
|
||||
lineSize = word.size;
|
||||
} else {
|
||||
text[text.length - 1] =
|
||||
(text[text.length - 1] ?? "") + " " + word.text;
|
||||
lineSize += spaceSize + word.size;
|
||||
}
|
||||
maxSize = Math.max(maxSize, lineSize);
|
||||
});
|
||||
return {
|
||||
lineCount: lineCount,
|
||||
text: text,
|
||||
width: maxSize,
|
||||
height: 110 * text.length,
|
||||
scale: 1 / Math.max(maxSize / w, (110 * text.length) / h),
|
||||
};
|
||||
});
|
||||
|
||||
lines.sort((w1, w2) => w2.scale - w1.scale);
|
||||
|
||||
if (!lines[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const targetSplit = lines[0];
|
||||
const targetFontSize = 100 * targetSplit.scale;
|
||||
const targetLineHeight = targetFontSize * 1.1;
|
||||
|
||||
ctx.font = `bold ${targetFontSize.toString()}px serif`;
|
||||
|
||||
targetSplit.text.forEach((line, i) => {
|
||||
ctx.fillText(
|
||||
line,
|
||||
x + w / 2,
|
||||
y +
|
||||
(h - targetSplit.height * targetSplit.scale) / 2 +
|
||||
targetLineHeight * i +
|
||||
(targetLineHeight - targetFontSize) / 2,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
export function setCookie(name: string, value: string, days = 30): void {
|
||||
const date = new Date();
|
||||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||
const expires = `expires=${date.toUTCString()}`;
|
||||
document.cookie = `${name}=${value}; path=/; ${expires}`;
|
||||
}
|
||||
|
||||
export function setDataCookie(name: string, data: object, days = 30): void {
|
||||
setCookie(name, JSON.stringify(data), days);
|
||||
}
|
||||
|
||||
export function getCookie(name: string, defaultValue: string): string {
|
||||
const prefix = `${name}=`;
|
||||
const decodedCookie = decodeURIComponent(document.cookie);
|
||||
const cookies = decodedCookie.split(";");
|
||||
for (const cookie of cookies) {
|
||||
if (cookie.trim().startsWith(prefix)) {
|
||||
return cookie.trim().substring(prefix.length, cookie.length);
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export function getDataCookie<T extends object>(
|
||||
name: string,
|
||||
defaultValue: T,
|
||||
): T {
|
||||
const rawCookie = getCookie(name, "");
|
||||
if (rawCookie.length) {
|
||||
try {
|
||||
const parsedConfig = JSON.parse(rawCookie) as T;
|
||||
return { ...defaultValue, ...parsedConfig };
|
||||
} catch {
|
||||
/* Empty */
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
export function randomSeed(): number {
|
||||
return (Math.random() * 2 ** 32) >>> 0;
|
||||
}
|
||||
|
||||
export function splitmix32(seed: number): () => number {
|
||||
let localSeed = seed;
|
||||
return function () {
|
||||
localSeed |= 0;
|
||||
localSeed = (localSeed + 0x9e3779b9) | 0;
|
||||
let tmp = localSeed ^ (localSeed >>> 16);
|
||||
tmp = Math.imul(tmp, 0x21f0aaad);
|
||||
tmp ^= tmp >>> 15;
|
||||
tmp = Math.imul(tmp, 0x735a2d97);
|
||||
return ((tmp ^ (tmp >>> 15)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
export function randomInt(max: number, seed: number) {
|
||||
const prng = splitmix32(seed);
|
||||
return Math.floor(prng() * max);
|
||||
}
|
||||
|
||||
export function randomElement<T>(array: T[], seed: number): T {
|
||||
return array[randomInt(array.length, seed)] as T;
|
||||
}
|
||||
|
||||
export function shuffleSeeded<T>(array: T[], seed: number): T[] {
|
||||
const output = array.slice();
|
||||
const prng = splitmix32(seed);
|
||||
for (let iteration = 0; iteration < array.length * 4; iteration += 1) {
|
||||
const i1 = Math.floor(prng() * array.length);
|
||||
const i2 = Math.floor(prng() * array.length);
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const tmp = output[i2]!;
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
output[i2] = output[i1]!;
|
||||
output[i1] = tmp;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
export function randomColor(): string {
|
||||
return ("#" + Math.floor(Math.random() * 16777215).toString(16)).padEnd(
|
||||
7,
|
||||
"0",
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createApp } from "vue";
|
||||
import App from "./App.vue";
|
||||
|
||||
createApp(App).mount("#app");
|
||||
Reference in New Issue
Block a user