/
hamster75
/
Life
Обзор
Документация
Войти
/
hamster75
/
Life
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
6
CI/CD
Аналитика
Безопасность
master
js/selection.js
66 строк
2 KB
Dmitry Smirnov
advanced edit
27 май 2026, 23:44
27 май 2026, 23:44
2ef514f
Код
Авторство
О чём код?
export class Selection { constructor() { this.active = false; // режим выделения включён this.rect = null; // { x1, y1, x2, y2 } в координатах клеток (нормализован) this._dragStart = null; // { x, y } — клетка начала rubber band } activate() { this.active = true; } deactivate() { this.active = false; this.rect = null; this._dragStart = null; } // ── Rubber band ──────────────────────────────────────────────────────────── startDrag(cell) { this._dragStart = { x: cell.x, y: cell.y }; this.rect = { x1: cell.x, y1: cell.y, x2: cell.x, y2: cell.y }; } updateDrag(cell) { if (!this._dragStart) return; const { x: sx, y: sy } = this._dragStart; this.rect = { x1: Math.min(sx, cell.x), y1: Math.min(sy, cell.y), x2: Math.max(sx, cell.x), y2: Math.max(sy, cell.y), }; } finishDrag() { this._dragStart = null; // Если rect вырожденный (одна клетка) — оставляем, это валидно } clearRect() { this.rect = null; this._dragStart = null; } // ── Утилиты ──────────────────────────────────────────────────────────────── containsCell(cell) { if (!this.rect) return false; const { x1, y1, x2, y2 } = this.rect; return cell.x >= x1 && cell.x <= x2 && cell.y >= y1 && cell.y <= y2; } // Извлечь живые клетки из grid внутри rect. // Возвращает [[dx, dy], ...] относительно { x1, y1 }. extractCells(grid) { if (!this.rect) return []; const { x1, y1, x2, y2 } = this.rect; const out = []; for (let cy = y1; cy <= y2; cy++) for (let cx = x1; cx <= x2; cx++) if (grid.isAlive(cx, cy)) out.push([cx - x1, cy - y1]); return out; } // Удалить все клетки внутри rect из grid. clearCells(grid) { if (!this.rect) return; const { x1, y1, x2, y2 } = this.rect; for (let cy = y1; cy <= y2; cy++) for (let cx = x1; cx <= x2; cx++) grid.set(cx, cy, false); } }