Отлично! Ниже — полный рабочий пример файла index.html, который реализует эффект "Матрицы" для загруженных изображений, как мы обсудили. Всё содержится в одном файле: HTML, CSS и JavaScript.
🔽 📄 index.html — Скопируй и открой в браузере:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Эффект Матрицы</title>
<style>
body {
margin: 0;
background: #000;
color: #0f0;
font-family: monospace;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
}
h1 {
margin-top: 20px;
}
canvas {
border: 1px solid #0f0;
margin-top: 20px;
display: block;
}
.controls {
margin-top: 20px;
}
button {
background: #000;
color: #0f0;
border: 1px solid #0f0;
padding: 10px 20px;
font-family: monospace;
cursor: pointer;
margin: 0 10px;
}
button:hover {
background: #0f0;
color: #000;
}
#fileInput {
display: none;
}
label[for="fileInput"] {
background: #000;
color: #0f0;
padding: 10px 20px;
border: 1px solid #0f0;
cursor: pointer;
font-family: monospace;
}
</style>
</head>
<body>
<h1>Эффект Матрицы 🌿</h1>
<input type="file" id="fileInput" accept="image/*"/>
<label for="fileInput">Загрузить изображение</label>
<div class="controls">
<button id="startBtn">Старт</button>
<button id="stopBtn">Стоп</button>
<button id="resetBtn">Сброс</button>
</div>
<canvas id="matrixCanvas"></canvas>
<script>
const canvas = document.getElementById('matrixCanvas');
const ctx = canvas.getContext('2d');
const fileInput = document.getElementById('fileInput');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const resetBtn = document.getElementById('resetBtn');
let image = null;
let animationId = null;
let columns = [];
const fontSize = 14;
const columnsCount = 100;
const characters = 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя0123456789';
// Установка размера canvas
function resizeCanvas() {
canvas.width = window.innerWidth * 0.9;
canvas.height = window.innerHeight * 0.7;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Загрузка изображения
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
image = new Image();
image.onload = () => {
drawImageToCanvas(image);
initMatrix();
};
image.src = event.target.result;
};
reader.readAsDataURL(file);
});
// Рисуем изображение на canvas
function drawImageToCanvas(img) {
// Масштабируем изображение, сохраняя пропорции
const ratio = Math.min(
canvas.width / img.width,
canvas.height / img.height
);
const width = img.width * ratio;
const height = img.height * ratio;
const x = (canvas.width - width) / 2;
const y = (canvas.height - height) / 2;
// Очищаем canvas
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Рисуем изображение
ctx.drawImage(img, x, y, width, height);
}
// Инициализация матричных столбцов
function initMatrix() {
columns = [];
for (let i = 0; i < columnsCount; i++) {
columns.push({
x: i * (canvas.width / columnsCount),
y: 0,
speed: 0.5 + Math.random() * 2,
char: characters[Math.floor(Math.random() * characters.length)]
});
}
}
// Анимация эффекта Матрицы
function animateMatrix() {
// Полупрозрачный фон, чтобы создать следы
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Рисуем изображение снова (поверх следов)
if (image) {
const ratio = Math.min(
canvas.width / image.width,
canvas.height / image.height
);
const width = image.width * ratio;
const height = image.height * ratio;
const x = (canvas.width - width) / 2;
const y = (canvas.height - height) / 2;
ctx.drawImage(image, x, y, width, height);
}
// Рисуем символы
ctx.font = `${fontSize}px monospace`;
ctx.fillStyle = '#0f0';
ctx.textAlign = 'center';
columns.forEach(col => {
const char = col.char;
const x = col.x;
const y = col.y;
ctx.fillText(char, x, y);
// Обновляем позицию
col.y += col.speed;
// Если символ вышел за пределы экрана — сбрасываем
if (y > canvas.height) {
col.y = 0;
col.char = characters[Math.floor(Math.random() * characters.length)];
col.x = x; // можно добавить небольшую случайность: x ± offset
}
});
animationId = requestAnimationFrame(animateMatrix);
}
// Управление
startBtn.addEventListener('click', () => {
if (!image) {
alert('Сначала загрузите изображение!');
return;
}
if (!animationId) {
initMatrix();
animateMatrix();
}
});
stopBtn.addEventListener('click', () => {
cancelAnimationFrame(animationId);
animationId = null;
});
resetBtn.addEventListener('click', () => {
if (animationId) {
cancelAnimationFrame(animationId);
animationId = null;
}
if (image) {
drawImageToCanvas(image);
initMatrix();
} else {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
});
</script>
</body>
</html>