initial commit

This commit is contained in:
Mateusz Pstrucha committed 2024-11-27 18:41:10 +01:00
commit 46d5406a00
8 files changed
+571

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
function drawingToBitstream(drawing) {
const flattened = drawing.flat(),
starting = flattened[0],
rows = drawing.length,
columns = drawing[0].length
let bitstream = [columns, rows, starting]
let prev = starting
count = 1
for (let i = 1; i < flattened.length + 1; i++) {
if (i == flattened.length) {
bitstream.push(count)
break
}
if (flattened[i] == prev) {
count++
} else {
bitstream.push(count)
count = 1
}
prev = flattened[i]
}
return bitstream
}
function bitstreamToString(bitstream) {
const buffer = new Uint8Array(bitstream),
binary = String.fromCharCode(...buffer),
base64 = btoa(binary)
return base64
}
function stringToBitstream(base64) {
const binary = atob(base64)
const bitstream = binary.split('').map((char) => char.charCodeAt(0))
return bitstream
}
function bitstreamToDrawing(bitstream) {
const [x, y, start, ...rest] = bitstream
let flat = [],
prev = start
// for every number in 'rest'
// generate an array of 'prev' repeated i times
// and add it to 'flat'
// then set 'prev' to the opposite of 'prev'
// and repeat
for (let i of rest) {
flat.push(...Array(i).fill(prev))
prev = +!prev
}
// split 'flat' into 'y' arrays of length 'x'
// and return the result
let drawing = []
for (let i = 0; i < y; i++) {
drawing.push(flat.slice(i * x, (i + 1) * x))
}
return drawing
}
+96
View File
@@ -0,0 +1,96 @@
function renderInput(canvas, x, y) {
if (!canvas) return
let boxes = document.createElement('div')
boxes.classList.add('boxes')
canvas.appendChild(boxes)
for (let i = 0; i < y * 5; i++) {
let line = document.createElement('div')
for (let j = 0; j < x * 5; j++) {
let box = document.createElement('box')
box.setAttribute('d', 'empty')
flip = (b) =>
b.setAttribute('d', b.getAttribute('d') == 'full' ? 'empty' : 'full')
box.addEventListener('mouseover', function (e) {
if (e.buttons == 1) {
flip(this)
}
})
box.addEventListener('mousedown', function () {
flip(this)
})
// box.addEventListener('click', () => {
// box.setAttribute(
// 'd',
// box.getAttribute('d') == 'full' ? 'empty' : 'full'
// )
// })
line.appendChild(box)
}
boxes.appendChild(line)
}
}
function canvasToDrawing(canvas) {
if (!canvas) return
const rowLength = canvas.querySelector('.boxes > div').children.length,
rawBoxes = canvas.querySelectorAll('box')
let boxes = Array.from(rawBoxes).reduce((acc, curr, i) => {
if (i % rowLength === 0) acc.push([])
acc[acc.length - 1].push(curr)
return acc
}, [])
let drawing = []
for (let row of boxes) {
let current = []
for (let box of row) {
current.push(box.getAttribute('d') == 'full' ? 1 : 0)
}
drawing.push(current)
}
return drawing
}
function transposeInput(canvas, x, y) {
const boxes = canvas.getElementsByClassName('boxes')[0]
if (x != 0) {
const children = Array.from(boxes.children)
let toMove
if (x > 0) {
toMove = children.slice(-Math.abs(x))
toMove.forEach((child) => boxes.prepend(child))
} else {
toMove = children.slice(0, Math.abs(x))
toMove.forEach((child) => boxes.appendChild(child))
}
}
if (y != 0) {
for (let line of boxes.children) {
const children = Array.from(line.children)
let toMove
if (y > 0) {
toMove = children.slice(-Math.abs(y))
toMove.forEach((child) => line.prepend(child))
} else {
toMove = children.slice(0, Math.abs(y))
toMove.forEach((child) => line.appendChild(child))
}
}
}
}
+5
View File
@@ -0,0 +1,5 @@
+69
View File
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logi designer</title>
<link rel="stylesheet" href="style.css" />
<script src="input.js"></script>
<script src="render.js"></script>
<script src="compression.js"></script>
</head>
<body>
<!-- input size x and y -->
<input type="number" id="input-x" value="1" min="1" max="6" />
<input type="number" id="input-y" value="1" min="1" max="6" />
<button id="make">Make canvas</button>
<div class="canvas" id="canvas-input"></div>
<!-- <span id="string">---</span> -->
<br />
<button id="render">Render</button>
<button id="transpose-down">down</button>
<button id="transpose-up">up</button>
<button id="transpose-left">left</button>
<button id="transpose-right">right</button>
<script>
const canvasIn = document.getElementById('canvas-input')
const btnMake = document.getElementById('make'),
btnRender = document.getElementById('render'),
btnTransposeDown = document.getElementById('transpose-down'),
btnTransposeUp = document.getElementById('transpose-up'),
btnTransposeLeft = document.getElementById('transpose-left'),
btnTransposeRight = document.getElementById('transpose-right'),
string = document.getElementById('string')
btnMake.onclick = () => {
const x = parseInt(document.getElementById('input-x').value),
y = parseInt(document.getElementById('input-y').value)
canvasIn.innerHTML = ''
renderInput(canvasIn, x, y)
}
btnTransposeDown.onclick = () => transposeInput(canvasIn, 1, 0)
btnTransposeUp.onclick = () => transposeInput(canvasIn, -1, 0)
btnTransposeLeft.onclick = () => transposeInput(canvasIn, 0, -1)
btnTransposeRight.onclick = () => transposeInput(canvasIn, 0, 1)
canvasIn.addEventListener('mouseup', (event) => {})
btnRender.onclick = () => {
let drawing = canvasToDrawing(canvasIn),
bitstream = drawingToBitstream(drawing),
base64 = bitstreamToString(bitstream)
window.open(`render.html?s=${base64}`)
}
</script>
</body>
</html>
+49
View File
@@ -0,0 +1,49 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
@page {
size: A4;
margin: 0;
}
html,
body {
width: 210mm;
height: 297mm;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
}
#title {
position: absolute;
top: 0;
width: 100%;
text-align: center;
font-size: 24px;
font-family: Arial, sans-serif;
color: #333;
padding: 10px 0;
border-bottom: 1px solid #ccc;
font-family: monospace;
}
#footer {
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
font-size: 12px;
font-family: Arial, sans-serif;
color: #333;
padding: 10px 0;
border-top: 1px solid #ccc;
user-select: all;
word-break: break-all;
font-family: monospace;
}
+59
View File
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logi genera</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="print.css" />
<script src="render.js"></script>
<script src="compression.js"></script>
</head>
<body>
<h2 id="title"></h2>
<div class="canvas" id="output"></div>
<div id="footer"></div>
<script>
// get base64 from query string
const urlParams = new URLSearchParams(window.location.search),
rawString = urlParams.get('s')
// decode base64
const bitstream = stringToBitstream(rawString),
drawing = bitstreamToDrawing(bitstream),
headers = drawingToHeaders(drawing)
const output = document.getElementById('output'),
footer = document.getElementById('footer'),
title = document.getElementById('title')
footer.innerText = rawString
renderHeaders(output, headers)
renderDrawing(output, drawing)
{
const data = new TextEncoder().encode(rawString),
hashBuffer = crypto.subtle
.digest('SHA-256', data)
.then((hashBuffer) => {
const hashArray = Array.from(new Uint8Array(hashBuffer)),
hashHex = hashArray
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
.slice(0, 8)
let name = `logi_${hashHex}`
document.title = name
title.innerText = name
})
}
</script>
</body>
</html>
+108
View File
@@ -0,0 +1,108 @@
function drawingToHeaders(drawing) {
const flippedDrawing = drawing[0].map((_, i) => drawing.map((row) => row[i]))
var columnValues = [],
rowValues = []
for (let row of drawing) {
let current = []
for (let i = 0; i < row.length; i++) {
if (row[i] == 0) continue
else {
let j = i,
count = 0
while (row[j] == 1 && j < row.length) {
count++
j++
}
i = j
current.unshift(count)
}
}
rowValues.push(current)
}
for (let column of flippedDrawing) {
let current = []
for (let i = 0; i < column.length; i++) {
if (column[i] == 0) continue
else {
let j = i,
count = 0
while (column[j] == 1 && j < column.length) {
count++
j++
}
i = j
current.push(count)
}
}
columnValues.push(current)
}
return { columnValues, rowValues }
}
function renderHeaders(canvas, headers) {
if (!canvas) return
const { columnValues, rowValues } = headers
let columns = document.createElement('div'),
rows = document.createElement('div'),
boxes = document.createElement('div')
columns.classList.add('columns')
rows.classList.add('rows')
boxes.classList.add('boxes')
canvas.innerHTML = ''
canvas.appendChild(columns)
canvas.appendChild(rows)
canvas.appendChild(boxes)
const longestRow = Math.max(
...rowValues.map((row) => row.map((val) => val).length)
)
console.log(longestRow)
canvas.style.setProperty('--headers', `${longestRow * 20}px`)
for (let colVal of columnValues) {
let div = document.createElement('div')
for (let val of colVal) {
let span = document.createElement('span')
span.textContent = val
div.appendChild(span)
}
columns.appendChild(div)
}
for (let rowVal of rowValues) {
let div = document.createElement('div')
for (let val of rowVal) {
let span = document.createElement('span')
span.textContent = val
div.appendChild(span)
}
rows.appendChild(div)
}
}
function renderDrawing(canvas, drawing, filled = false) {
let boxes = canvas.querySelector('.boxes')
for (let row of drawing) {
let line = document.createElement('div')
for (let val of row) {
let box = document.createElement('box'),
d = val ? 'full' : 'empty'
if (filled) box.setAttribute('d', d)
line.appendChild(box)
}
boxes.appendChild(line)
}
}
+116
View File
@@ -0,0 +1,116 @@
.canvas {
/* background-color: white; */
--side: 5mm;
--headers: 6rem;
user-select: none;
}
.boxes > div {
height: var(--side);
}
.boxes > div:nth-child(5n):not(:last-child) > box {
border-bottom-width: 2px;
}
.boxes > div > box:nth-child(5n):not(:last-child) {
border-right-width: 2px;
}
.boxes > div:not(:first-child) {
border-left: none;
}
.boxes box {
width: var(--side);
height: var(--side);
box-sizing: border-box;
border: 1px solid black;
display: inline-block;
}
.boxes box:not(:first-child) {
border-left: none;
}
.boxes > div:not(:first-child) box {
border-top: none;
}
box[d='empty'] {
/* background-color: white; */
}
box[d='full'] {
background-color: black;
}
.columns {
display: flex;
max-width: min-content;
margin-left: var(--headers);
}
.columns div {
width: var(--side);
box-sizing: border-box;
border: 1px solid black;
border-bottom: none;
padding-top: 0.5em;
display: flex;
flex-direction: column;
justify-content: end;
}
.columns > div:nth-child(5n):not(:last-child) {
border-right-width: 2px;
}
.columns div:not(:first-child) {
border-left: none;
}
.rows {
display: flex;
flex-direction: column;
width: var(--headers);
float: left;
}
.rows div {
border: 1px solid black;
border-right: none;
text-align: right;
height: var(--side);
min-width: var(--side);
box-sizing: border-box;
padding-right: 0.5em;
}
.rows > div:nth-child(5n):not(:last-child) {
border-bottom-width: 2px;
}
.rows div:not(:first-child) {
border-top: none;
}
.columns span {
display: block;
text-align: center;
}
.rows span {
display: block;
float: right;
padding-left: 0.5em;
transform: translate(0, -2px);
}
.debug {
font-family: monospace;
/* break when overflows screen */
overflow-wrap: anywhere;
}