2026-04-20 20:13:58 +02:00
|
|
|
---
|
|
|
|
|
title: Squares²
|
|
|
|
|
tags:
|
|
|
|
|
- demoArt
|
|
|
|
|
---
|
|
|
|
|
{% layout 'hippie/simple.liquid' %}
|
|
|
|
|
|
|
|
|
|
{% block style %}
|
|
|
|
|
<style>
|
|
|
|
|
canvas {
|
|
|
|
|
display: block;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
{% block body %}{% endblock %}
|
|
|
|
|
|
|
|
|
|
{% block script %}
|
|
|
|
|
<script>
|
|
|
|
|
function fillCanvasWithSquares(maxSquareSize = 128, minSquareSize = 8, largeSquarePercentage = 40) {
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
|
canvas.width = window.innerWidth;
|
|
|
|
|
canvas.height = window.innerHeight;
|
|
|
|
|
document.body.appendChild(canvas);
|
|
|
|
|
|
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
const colors = [
|
|
|
|
|
'#fad803', '#d30a51', '#273f8b',
|
|
|
|
|
'#b7e0f0', '#52bed1', '#0c85ff'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function isPowerOfTwo(n) {
|
|
|
|
|
return n > 0 && (n & (n - 1)) === 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
if (!isPowerOfTwo(maxSquareSize) || !isPowerOfTwo(minSquareSize)) {
|
|
|
|
|
console.error('Both sizes must be powers of 2');
|
|
|
|
|
return;
|
2026-04-20 20:13:58 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
let grid = Array(Math.ceil(canvas.height / minSquareSize))
|
|
|
|
|
.fill(null)
|
|
|
|
|
.map(() => Array(Math.ceil(canvas.width / minSquareSize)).fill(false));
|
2026-04-20 20:13:58 +02:00
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
function canPlace(gridX, gridY, sizeInCells) {
|
|
|
|
|
if (gridY + sizeInCells > grid.length || gridX + sizeInCells > grid[0].length) return false;
|
2026-04-20 20:13:58 +02:00
|
|
|
for (let y = gridY; y < gridY + sizeInCells; y++) {
|
|
|
|
|
for (let x = gridX; x < gridX + sizeInCells; x++) {
|
|
|
|
|
if (grid[y][x]) return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
function markOccupied(gridX, gridY, sizeInCells) {
|
2026-04-20 20:13:58 +02:00
|
|
|
for (let y = gridY; y < gridY + sizeInCells; y++) {
|
|
|
|
|
for (let x = gridX; x < gridX + sizeInCells; x++) {
|
|
|
|
|
grid[y][x] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
function draw(gridX, gridY, sizeInPixels) {
|
|
|
|
|
const x = gridX * minSquareSize;
|
|
|
|
|
const y = gridY * minSquareSize;
|
|
|
|
|
ctx.fillStyle = colors[Math.floor(Math.random() * colors.length)];
|
|
|
|
|
ctx.fillRect(x, y, sizeInPixels, sizeInPixels);
|
2026-04-20 20:13:58 +02:00
|
|
|
ctx.strokeStyle = '#333';
|
|
|
|
|
ctx.lineWidth = 1;
|
2026-04-20 20:39:35 +02:00
|
|
|
ctx.strokeRect(x, y, sizeInPixels, sizeInPixels);
|
2026-04-20 20:13:58 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
function fill() {
|
|
|
|
|
const largestSizeInCells = maxSquareSize / minSquareSize;
|
2026-04-20 20:13:58 +02:00
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
// Collect and shuffle positions for largest squares
|
|
|
|
|
const positions = [];
|
2026-04-20 20:13:58 +02:00
|
|
|
for (let gridY = 0; gridY < grid.length; gridY++) {
|
|
|
|
|
for (let gridX = 0; gridX < grid[0].length; gridX++) {
|
2026-04-20 20:39:35 +02:00
|
|
|
if (canPlace(gridX, gridY, largestSizeInCells)) {
|
|
|
|
|
positions.push({ gridX, gridY });
|
2026-04-20 20:13:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-20 20:39:35 +02:00
|
|
|
positions.sort(() => Math.random() - 0.5);
|
|
|
|
|
|
|
|
|
|
// Place largest squares up to percentage
|
|
|
|
|
const target = Math.floor(positions.length * largeSquarePercentage / 100);
|
|
|
|
|
for (let i = 0; i < target; i++) {
|
|
|
|
|
const { gridX, gridY } = positions[i];
|
|
|
|
|
if (canPlace(gridX, gridY, largestSizeInCells)) {
|
|
|
|
|
markOccupied(gridX, gridY, largestSizeInCells);
|
|
|
|
|
draw(gridX, gridY, maxSquareSize);
|
2026-04-20 20:13:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
// Fill remaining space with smaller sizes
|
|
|
|
|
for (let size = maxSquareSize / 2; size >= minSquareSize; size /= 2) {
|
|
|
|
|
const cellSize = size / minSquareSize;
|
2026-04-20 20:13:58 +02:00
|
|
|
for (let gridY = 0; gridY < grid.length; gridY++) {
|
|
|
|
|
for (let gridX = 0; gridX < grid[0].length; gridX++) {
|
2026-04-20 20:39:35 +02:00
|
|
|
if (canPlace(gridX, gridY, cellSize)) {
|
|
|
|
|
markOccupied(gridX, gridY, cellSize);
|
|
|
|
|
draw(gridX, gridY, size);
|
2026-04-20 20:13:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
fill();
|
2026-04-20 20:13:58 +02:00
|
|
|
|
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
|
canvas.width = window.innerWidth;
|
|
|
|
|
canvas.height = window.innerHeight;
|
2026-04-20 20:39:35 +02:00
|
|
|
grid = Array(Math.ceil(canvas.height / minSquareSize))
|
|
|
|
|
.fill(null)
|
|
|
|
|
.map(() => Array(Math.ceil(canvas.width / minSquareSize)).fill(false));
|
2026-04-20 20:13:58 +02:00
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
2026-04-20 20:39:35 +02:00
|
|
|
fill();
|
2026-04-20 20:13:58 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:39:35 +02:00
|
|
|
fillCanvasWithSquares(128, 8, 10);
|
2026-04-20 20:13:58 +02:00
|
|
|
</script>
|
|
|
|
|
{% endblock %}
|