Skip to content

Instantly share code, notes, and snippets.

@catindev
Created December 9, 2024 17:28
Show Gist options
  • Select an option

  • Save catindev/a63ecf6995d46f45b6e40d2881016c77 to your computer and use it in GitHub Desktop.

Select an option

Save catindev/a63ecf6995d46f45b6e40d2881016c77 to your computer and use it in GitHub Desktop.
Procedural Invaders
function drawTo(context, n, dx, dy) {
const pattern = [0, 5, 10, 5, 0]; // Определение паттерна
for (let y = 0; y <= 4; y++) {
for (let x = 0; x <= 4; x++) {
const bit = pattern[x] + y;
if ((n & (1 << bit)) !== 0) {
context.fillRect(dx + x, dy + y, 1, 1); // Рисуем пиксель
}
}
}
}
function generate(cols, rows) {
const canvas = document.createElement('canvas');
const width = 7 * cols;
const height = 7 * rows;
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
context.fillStyle = 'black';
const rnd = () => Math.floor(Math.random() * (1 << 16)); // Генератор случайных чисел
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const n = rnd();
drawTo(context, n, 7 * x + 1, 7 * y + 1);
}
}
document.body.appendChild(canvas); // Добавляем canvas на страницу
}
// Генерация изображения
generate(80, 60);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment