Lots of new features
See release for more info
This commit is contained in:
+189
-89
@@ -38,7 +38,6 @@ header {
|
||||
|
||||
header p {
|
||||
font-size: 1.2em;
|
||||
color: #00ff99;
|
||||
}
|
||||
|
||||
/* ===== Main Layout ===== */
|
||||
@@ -53,26 +52,26 @@ main {
|
||||
gap: 30px;
|
||||
}
|
||||
|
||||
/* ===== Section Card Styling ===== */
|
||||
/* ===== Card Styling ===== */
|
||||
.card {
|
||||
background-color: #1e1e1e;
|
||||
padding: 25px;
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 0 15px rgba(0, 255, 153, 0.4);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* ===== Uniform Form Inputs ===== */
|
||||
/* ===== Form Group Styling ===== */
|
||||
.form-group {
|
||||
display: flex;
|
||||
display: flex !important;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
gap: 0px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* ===== Inputs, Textareas, Selects ===== */
|
||||
input,
|
||||
textarea,
|
||||
select,
|
||||
@@ -85,7 +84,9 @@ input[type="file"] {
|
||||
background-color: #2c2f33;
|
||||
color: #00ff99;
|
||||
font-size: 1em;
|
||||
text-align: center;
|
||||
transition: 0.3s;
|
||||
margin:10px auto;
|
||||
}
|
||||
|
||||
textarea {
|
||||
@@ -93,15 +94,13 @@ textarea {
|
||||
resize: none;
|
||||
}
|
||||
|
||||
input[type="password"],
|
||||
#password {
|
||||
input[type="password"] {
|
||||
min-height: 50px;
|
||||
}
|
||||
|
||||
input[type="file"] {
|
||||
border: 2px dashed #00ff99;
|
||||
cursor: pointer;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
input[type="file"]::file-selector-button {
|
||||
@@ -118,6 +117,7 @@ input[type="file"] {
|
||||
background-color: #00cc77;
|
||||
}
|
||||
|
||||
/* ===== Focus Effects ===== */
|
||||
input:focus,
|
||||
textarea:focus,
|
||||
select:focus {
|
||||
@@ -125,29 +125,27 @@ select:focus {
|
||||
box-shadow: 0 0 8px rgba(0, 255, 153, 0.8);
|
||||
}
|
||||
|
||||
/* ===== Match input and output sizes ===== */
|
||||
/* ===== Textareas Specific Widths ===== */
|
||||
#input-text,
|
||||
#output-text {
|
||||
width: 80%;
|
||||
max-width: 500px;
|
||||
height: 140px;
|
||||
box-sizing: border-box;
|
||||
resize: none;
|
||||
}
|
||||
|
||||
/* ===== Buttons ===== */
|
||||
/* ===== Button Group Styling ===== */
|
||||
.button-group {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
gap: 15px;
|
||||
margin-top: 15px;
|
||||
margin: 10px auto;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 10px 20px;
|
||||
border: 2px solid #00ff99;
|
||||
border: 0px solid #00ff99;
|
||||
border-radius: 8px;
|
||||
background-color: #2c2f33;
|
||||
color: #00ff99;
|
||||
@@ -156,6 +154,7 @@ button {
|
||||
transition: 0.3s;
|
||||
width: 100%;
|
||||
max-width: 200px;
|
||||
/* margin: 10px auto; */
|
||||
}
|
||||
|
||||
button:hover {
|
||||
@@ -163,69 +162,86 @@ button {
|
||||
color: #121212;
|
||||
}
|
||||
|
||||
/* ===== Toggle Buttons (Encode/Decode, Encrypt/Decrypt) ===== */
|
||||
.radio-group {
|
||||
/* ===== Toggle Switch Styling ===== */
|
||||
.toggle-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
margin-top: 8px;
|
||||
gap: 12px;
|
||||
margin-top: 10px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.radio-button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 1px 1px;
|
||||
border: 2px solid #00ff99;
|
||||
border-radius: 8px;
|
||||
background-color: #2c2f33;
|
||||
color: #00ff99;
|
||||
cursor: pointer;
|
||||
transition: 0.3s;
|
||||
/* Make sure the switch aligns well */
|
||||
.switch {
|
||||
position: relative;
|
||||
box-shadow: none;
|
||||
display: flex;
|
||||
align-items: center; /* <-- Ensures vertical centering */
|
||||
justify-content: center;
|
||||
width: 70px;
|
||||
height: 34px;
|
||||
}
|
||||
|
||||
.radio-button:hover {
|
||||
background-color: #00ff99;
|
||||
color: #121212;
|
||||
/* Hide the checkbox */
|
||||
.switch input {
|
||||
opacity: 0;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
|
||||
/* Hide the actual radio input */
|
||||
.radio-button input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* When selected, make the ENTIRE BUTTON glow */
|
||||
.radio-button input:checked + span {
|
||||
background-color: #2c2f33;
|
||||
color: #00ff99;
|
||||
box-shadow: 0 0 15px rgba(0, 255, 153, 0.7);
|
||||
border-radius: 8px;
|
||||
padding: 8px 18px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
|
||||
/* ===== Remove File Button ===== */
|
||||
#remove-file-btn {
|
||||
display: none;
|
||||
margin-top: 8px;
|
||||
padding: 8px 16px;
|
||||
border: 2px solid #ff5555;
|
||||
/* The slider */
|
||||
.slider {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-color: #2c2f33;
|
||||
color: #ff5555;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: 0.3s;
|
||||
border: 2px solid #00ff99;
|
||||
border-radius: 34px;
|
||||
transition: .4s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
#remove-file-btn:hover {
|
||||
background-color: #ff5555;
|
||||
color: #2c2f33;
|
||||
/* The circle knob */
|
||||
.slider::before {
|
||||
content: "";
|
||||
height: 26px;
|
||||
width: 26px;
|
||||
background-color: #00ff99;
|
||||
border-radius: 50%;
|
||||
transition: .4s;
|
||||
transform: translateX(4px);
|
||||
position: absolute;
|
||||
left: 0px;
|
||||
bottom: 2.5px;
|
||||
}
|
||||
|
||||
input:checked + .slider::before {
|
||||
transform: translateX(36px);
|
||||
}
|
||||
|
||||
/* Toggle Labels */
|
||||
.labels {
|
||||
position: relative;
|
||||
width: 100px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
font-size: 0.9em;
|
||||
color: #00ff99;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.labels::before,
|
||||
.labels::after {
|
||||
content: attr(data-on);
|
||||
width: 50%;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.labels::after {
|
||||
content: attr(data-off);
|
||||
}
|
||||
|
||||
/* ===== Toast Notifications ===== */
|
||||
@@ -244,11 +260,11 @@ button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
animation: fadein 0.5s, fadeout 0.5s 2.5s;
|
||||
}
|
||||
|
||||
.toast.show {
|
||||
visibility: visible;
|
||||
animation: fadein 0.5s, fadeout 0.5s 2.5s;
|
||||
}
|
||||
|
||||
@keyframes fadein {
|
||||
@@ -271,21 +287,6 @@ button {
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== Pacman Canvas ===== */
|
||||
.pacman-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
#pacmanCanvas {
|
||||
background-color: black;
|
||||
border: 2px solid #00ff99;
|
||||
border-radius: 10px;
|
||||
width: 800px;
|
||||
height: 600px;
|
||||
}
|
||||
|
||||
/* ===== Footer ===== */
|
||||
footer {
|
||||
text-align: center;
|
||||
@@ -310,15 +311,114 @@ footer {
|
||||
|
||||
/* ===== Responsive Tweaks ===== */
|
||||
@media (max-width: 600px) {
|
||||
input,
|
||||
textarea,
|
||||
select,
|
||||
#input-text,
|
||||
#output-text,
|
||||
#password-field,
|
||||
#password,
|
||||
#file-password {
|
||||
input, textarea, select, #input-text, #output-text {
|
||||
width: 100%;
|
||||
max-width: 90%;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== Copy Feedback Message ===== */
|
||||
.copy-feedback {
|
||||
background-color: #2a2a2a;
|
||||
border: 1px solid #00ff99;
|
||||
padding: 6px 12px;
|
||||
margin-top: 6px;
|
||||
border-radius: 6px;
|
||||
color: #00ff99;
|
||||
font-size: 0.9em;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s ease;
|
||||
text-align: center;
|
||||
max-width: 300px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
||||
.copy-feedback.show {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
form {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
form input,
|
||||
form button {
|
||||
width: 80%;
|
||||
max-width: 500px;
|
||||
margin-bottom: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
section.card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.copy-feedback.show {
|
||||
display: block;
|
||||
width: fit-content;
|
||||
margin-top: 10px;
|
||||
padding: 6px 12px;
|
||||
background-color: #2a2a2a;
|
||||
color: #00ff99;
|
||||
border: 1px solid #00ff99;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
#logContainer {
|
||||
white-space: pre-wrap; /* Wrap long lines */
|
||||
word-wrap: break-word; /* Break long words if needed */
|
||||
overflow-wrap: anywhere; /* Ensures long strings don't overflow */
|
||||
background: black;
|
||||
color: lime;
|
||||
padding: 10px;
|
||||
border-radius: 8px;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#pacmanCanvas {
|
||||
background-color: black;
|
||||
display: block;
|
||||
margin: auto;
|
||||
border: 2px solid #00ff99;
|
||||
border-radius: 12px;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
#pacman-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 20px;
|
||||
padding: 20px;
|
||||
display: block;
|
||||
margin: auto;
|
||||
border: 2px solid #00ff99;
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.pacman-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
||||
/* ===== Utility: Hidden Class ===== */
|
||||
.hidden {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
@@ -0,0 +1,86 @@
|
||||
// encryption.js
|
||||
|
||||
/**
|
||||
* Derives an AES-GCM key from a password using PBKDF2.
|
||||
* @param {string} password - User-supplied password.
|
||||
* @param {Uint8Array} salt - Randomly generated salt.
|
||||
* @returns {Promise<CryptoKey>}
|
||||
*/
|
||||
export async function deriveKey(password, salt) {
|
||||
const encoder = new TextEncoder();
|
||||
const keyMaterial = await crypto.subtle.importKey(
|
||||
'raw',
|
||||
encoder.encode(password),
|
||||
{ name: 'PBKDF2' },
|
||||
false,
|
||||
['deriveKey']
|
||||
);
|
||||
|
||||
return crypto.subtle.deriveKey(
|
||||
{
|
||||
name: 'PBKDF2',
|
||||
salt,
|
||||
iterations: 200_000,
|
||||
hash: 'SHA-256'
|
||||
},
|
||||
keyMaterial,
|
||||
{ name: 'AES-GCM', length: 256 },
|
||||
false,
|
||||
['encrypt', 'decrypt']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypts a message using AES-GCM with a derived key.
|
||||
* @param {string} message - Plaintext message to encrypt.
|
||||
* @param {string} password - User password for key derivation.
|
||||
* @returns {Promise<string>} - Base64-encoded encrypted string.
|
||||
*/
|
||||
export async function encryptAdvanced(message, password) {
|
||||
const encoder = new TextEncoder();
|
||||
const salt = crypto.getRandomValues(new Uint8Array(16));
|
||||
const iv = crypto.getRandomValues(new Uint8Array(12));
|
||||
const key = await deriveKey(password, salt);
|
||||
const encoded = encoder.encode(message);
|
||||
|
||||
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, encoded);
|
||||
|
||||
const output = new Uint8Array(salt.length + iv.length + ciphertext.byteLength);
|
||||
output.set(salt);
|
||||
output.set(iv, salt.length);
|
||||
output.set(new Uint8Array(ciphertext), salt.length + iv.length);
|
||||
|
||||
return btoa(String.fromCharCode(...output));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts an AES-GCM encrypted string.
|
||||
* @param {string} encryptedData - Base64-encoded ciphertext.
|
||||
* @param {string} password - Password used to derive the decryption key.
|
||||
* @returns {Promise<string>} - Decrypted plaintext.
|
||||
*/
|
||||
export async function decryptAdvanced(encryptedData, password) {
|
||||
const encrypted = new Uint8Array(
|
||||
atob(encryptedData).split('').map(c => c.charCodeAt(0))
|
||||
);
|
||||
|
||||
const salt = encrypted.slice(0, 16);
|
||||
const iv = encrypted.slice(16, 28);
|
||||
const ciphertext = encrypted.slice(28);
|
||||
const key = await deriveKey(password, salt);
|
||||
|
||||
const decrypted = await crypto.subtle.decrypt(
|
||||
{ name: 'AES-GCM', iv },
|
||||
key,
|
||||
ciphertext
|
||||
);
|
||||
|
||||
return new TextDecoder().decode(decrypted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional init logging for module diagnostics.
|
||||
*/
|
||||
export function setupEncryption() {
|
||||
console.log('[Encryption] Module loaded');
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// fileops.js
|
||||
|
||||
import { encryptAdvanced, decryptAdvanced } from './encryption.js';
|
||||
|
||||
/**
|
||||
* Encrypts the selected file and triggers download of the encrypted version.
|
||||
* @param {HTMLInputElement} fileInput - The input element of type 'file'.
|
||||
* @param {string} password - Password for encryption.
|
||||
*/
|
||||
export function encryptFile(fileInput, password) {
|
||||
if (!fileInput.files.length) {
|
||||
alert("Please select a file!");
|
||||
return;
|
||||
}
|
||||
|
||||
const file = fileInput.files[0];
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = async (e) => {
|
||||
const rawBytes = new Uint8Array(e.target.result);
|
||||
const base64 = btoa(String.fromCharCode(...rawBytes));
|
||||
const encrypted = await encryptAdvanced(base64, password);
|
||||
downloadFile(encrypted, file.name + ".enc");
|
||||
};
|
||||
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts the selected encrypted file and triggers download of the original.
|
||||
* @param {HTMLInputElement} fileInput - The input element of type 'file'.
|
||||
* @param {string} password - Password for decryption.
|
||||
*/
|
||||
export function decryptFile(fileInput, password) {
|
||||
if (!fileInput.files.length) {
|
||||
alert("Please select a file!");
|
||||
return;
|
||||
}
|
||||
|
||||
const file = fileInput.files[0];
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = async (e) => {
|
||||
try {
|
||||
const encryptedText = e.target.result;
|
||||
const base64Decrypted = await decryptAdvanced(encryptedText, password);
|
||||
const byteArray = new Uint8Array(
|
||||
[...atob(base64Decrypted)].map(c => c.charCodeAt(0))
|
||||
);
|
||||
downloadFileBinary(byteArray, file.name.replace(/\.enc$/, ''));
|
||||
} catch (err) {
|
||||
console.error("[Decryption Error]", err);
|
||||
alert("Decryption failed: wrong password or corrupted file.");
|
||||
}
|
||||
};
|
||||
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a text-based file (encrypted string).
|
||||
* @param {string} content - The file content to download.
|
||||
* @param {string} filename - Desired name for the downloaded file.
|
||||
*/
|
||||
function downloadFile(content, filename) {
|
||||
const blob = new Blob([content], { type: "application/octet-stream" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
a.click();
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a binary file (Uint8Array).
|
||||
* @param {Uint8Array} byteArray - The binary content.
|
||||
* @param {string} filename - Desired name for the downloaded file.
|
||||
*/
|
||||
function downloadFileBinary(byteArray, filename) {
|
||||
const blob = new Blob([byteArray], { type: "application/octet-stream" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
a.click();
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// main.js
|
||||
|
||||
import { setupUI } from './ui.js';
|
||||
import { setupGame } from './pacman.js';
|
||||
|
||||
/**
|
||||
* Initialize UI and game once the DOM is fully loaded.
|
||||
*/
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
setupUI();
|
||||
setupGame();
|
||||
});
|
||||
@@ -0,0 +1,262 @@
|
||||
// pacman.js
|
||||
|
||||
export function setupGame() {
|
||||
console.log('[PacMan] Game module loaded.');
|
||||
|
||||
window.startPacman = startPacman;
|
||||
window.exitGame = exitGame;
|
||||
}
|
||||
|
||||
// ====== Game Constants & State ======
|
||||
let canvas, ctx, pacman, enemy, walls, dots, score;
|
||||
let pacmanSpeed = 40,
|
||||
enemySpeed = 20,
|
||||
cellSize = 40,
|
||||
dotSize = 5,
|
||||
cols, rows, randSeed, gameInterval;
|
||||
|
||||
// ====== Game Initialization ======
|
||||
|
||||
export function startPacman() {
|
||||
canvas = document.getElementById("pacmanCanvas");
|
||||
ctx = canvas.getContext("2d");
|
||||
|
||||
cols = Math.floor(canvas.width / cellSize);
|
||||
rows = Math.floor(canvas.height / cellSize);
|
||||
walls = [];
|
||||
dots = [];
|
||||
score = 0;
|
||||
|
||||
clearInterval(gameInterval);
|
||||
const seedSource = document.getElementById("password")?.value || "pacman";
|
||||
randSeed = [...seedSource].reduce((s, c) => s + c.charCodeAt(0), 0);
|
||||
|
||||
|
||||
generateWalls();
|
||||
generateDots();
|
||||
|
||||
pacman = spawn();
|
||||
do { enemy = spawn(); } while (enemy.x === pacman.x && enemy.y === pacman.y);
|
||||
|
||||
pacman.dx = pacman.dy = 0;
|
||||
document.addEventListener("keydown", movePacman);
|
||||
|
||||
gameInterval = setInterval(gameLoop, 150);
|
||||
}
|
||||
|
||||
export function stopPacman() {
|
||||
clearInterval(gameInterval);
|
||||
if (ctx) ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
}
|
||||
|
||||
export function resetGame() {
|
||||
stopPacman();
|
||||
startPacman();
|
||||
}
|
||||
|
||||
export function exitGame() {
|
||||
stopPacman();
|
||||
document.getElementById("input-text").value = "";
|
||||
document.getElementById("pacman-section").style.display = "none";
|
||||
document.getElementById("encoding-section").style.display = "block";
|
||||
}
|
||||
|
||||
// ====== Game Setup Helpers ======
|
||||
|
||||
function spawn() {
|
||||
const options = [];
|
||||
for (let c = 1; c < cols - 1; c++) {
|
||||
for (let r = 1; r < rows - 1; r++) {
|
||||
if (!walls.some(w => w.c === c && w.r === r)) {
|
||||
const neighbors = [
|
||||
{ c: c + 1, r }, { c: c - 1, r },
|
||||
{ c, r: r + 1 }, { c, r: r - 1 }
|
||||
];
|
||||
if (neighbors.some(n => !walls.some(w => w.c === n.c && w.r === n.r))) {
|
||||
options.push({ c, r });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const s = options[Math.floor(rand() * options.length)];
|
||||
return {
|
||||
x: s.c * cellSize + cellSize / 2,
|
||||
y: s.r * cellSize + cellSize / 2,
|
||||
size: cellSize / 2 - 5,
|
||||
dx: 0,
|
||||
dy: 0
|
||||
};
|
||||
}
|
||||
|
||||
function rand() {
|
||||
const x = Math.sin(randSeed++) * 10000;
|
||||
return x - Math.floor(x);
|
||||
}
|
||||
|
||||
function generateWalls() {
|
||||
for (let c = 0; c < cols; c++) {
|
||||
for (let r = 0; r < rows; r++) {
|
||||
if (c === 0 || r === 0 || c === cols - 1 || r === rows - 1 || rand() < 0.2) {
|
||||
walls.push({ c, r });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function generateDots() {
|
||||
dots = [];
|
||||
for (let c = 1; c < cols - 1; c++) {
|
||||
for (let r = 1; r < rows - 1; r++) {
|
||||
if (walls.some(w => w.c === c && w.r === r)) continue;
|
||||
|
||||
const isEnclosed =
|
||||
walls.some(w => w.c === c + 1 && w.r === r) &&
|
||||
walls.some(w => w.c === c - 1 && w.r === r) &&
|
||||
walls.some(w => w.c === c && w.r === r + 1) &&
|
||||
walls.some(w => w.c === c && w.r === r - 1);
|
||||
|
||||
if (!isEnclosed) dots.push({ c, r });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ====== Game Loop & Drawing ======
|
||||
|
||||
function gameLoop() {
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
drawWalls();
|
||||
moveChar(pacman);
|
||||
moveEnemy();
|
||||
drawChar(pacman, "yellow");
|
||||
drawChar(enemy, "red");
|
||||
eatDots();
|
||||
drawScore();
|
||||
checkGameOver();
|
||||
}
|
||||
|
||||
function drawWalls() {
|
||||
ctx.fillStyle = "blue";
|
||||
walls.forEach(w => {
|
||||
ctx.fillRect(w.c * cellSize, w.r * cellSize, cellSize, cellSize);
|
||||
});
|
||||
}
|
||||
|
||||
function drawChar(ch, color) {
|
||||
ctx.beginPath();
|
||||
ctx.arc(ch.x, ch.y, ch.size, 0, Math.PI * 2);
|
||||
ctx.fillStyle = color;
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
function drawScore() {
|
||||
ctx.fillStyle = "white";
|
||||
ctx.font = "20px Poppins";
|
||||
ctx.fillText("Score: " + score, 10, 25);
|
||||
}
|
||||
|
||||
function checkGameOver() {
|
||||
if (
|
||||
Math.abs(pacman.x - enemy.x) < pacman.size &&
|
||||
Math.abs(pacman.y - enemy.y) < pacman.size
|
||||
) {
|
||||
ctx.fillStyle = "#00ff99";
|
||||
ctx.font = "40px Poppins";
|
||||
ctx.textAlign = "center";
|
||||
ctx.fillText("Game Over!", canvas.width / 2, canvas.height / 2);
|
||||
clearInterval(gameInterval);
|
||||
}
|
||||
}
|
||||
|
||||
// ====== Movement Logic ======
|
||||
|
||||
function movePacman(e) {
|
||||
const k = e.key;
|
||||
if (!["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(k)) return;
|
||||
e.preventDefault();
|
||||
|
||||
if (k === "ArrowUp") { pacman.dx = 0; pacman.dy = -pacmanSpeed; }
|
||||
if (k === "ArrowDown") { pacman.dx = 0; pacman.dy = pacmanSpeed; }
|
||||
if (k === "ArrowLeft") { pacman.dx = -pacmanSpeed; pacman.dy = 0; }
|
||||
if (k === "ArrowRight") { pacman.dx = pacmanSpeed; pacman.dy = 0; }
|
||||
}
|
||||
|
||||
function moveChar(ch) {
|
||||
const nx = ch.x + ch.dx;
|
||||
const ny = ch.y + ch.dy;
|
||||
if (!willCollide(nx, ny, ch.size)) {
|
||||
ch.x = nx;
|
||||
ch.y = ny;
|
||||
}
|
||||
}
|
||||
|
||||
function moveEnemy() {
|
||||
const options = [];
|
||||
const moves = [[enemySpeed, 0], [-enemySpeed, 0], [0, enemySpeed], [0, -enemySpeed]];
|
||||
|
||||
moves.forEach(([dx, dy]) => {
|
||||
const nx = enemy.x + dx;
|
||||
const ny = enemy.y + dy;
|
||||
if (!willCollide(nx, ny, enemy.size)) options.push({ dx, dy });
|
||||
});
|
||||
|
||||
if (!options.length) return;
|
||||
|
||||
let best = options[0];
|
||||
let bestDist = dist(enemy.x + best.dx, enemy.y + best.dy, pacman.x, pacman.y);
|
||||
|
||||
for (const opt of options) {
|
||||
const d = dist(enemy.x + opt.dx, enemy.y + opt.dy, pacman.x, pacman.y);
|
||||
if (d < bestDist) {
|
||||
best = opt;
|
||||
bestDist = d;
|
||||
}
|
||||
}
|
||||
|
||||
enemy.x += best.dx;
|
||||
enemy.y += best.dy;
|
||||
}
|
||||
|
||||
function dist(x1, y1, x2, y2) {
|
||||
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
|
||||
}
|
||||
|
||||
function willCollide(x, y, size) {
|
||||
const left = x - size, right = x + size;
|
||||
const top = y - size, bottom = y + size;
|
||||
|
||||
return walls.some(w => {
|
||||
const wx1 = w.c * cellSize, wy1 = w.r * cellSize;
|
||||
const wx2 = wx1 + cellSize, wy2 = wy1 + cellSize;
|
||||
return right > wx1 && left < wx2 && bottom > wy1 && top < wy2;
|
||||
});
|
||||
}
|
||||
|
||||
function eatDots() {
|
||||
const chompSound = document.getElementById("chomp-sound");
|
||||
|
||||
dots = dots.filter(d => {
|
||||
const dx = d.c * cellSize + cellSize / 2;
|
||||
const dy = d.r * cellSize + cellSize / 2;
|
||||
|
||||
if (Math.abs(pacman.x - dx) < pacman.size && Math.abs(pacman.y - dy) < pacman.size) {
|
||||
score++;
|
||||
if (chompSound) {
|
||||
chompSound.currentTime = 0;
|
||||
chompSound.volume = 0.4;
|
||||
chompSound.play();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
ctx.fillStyle = "white";
|
||||
dots.forEach(d => {
|
||||
ctx.beginPath();
|
||||
ctx.arc(d.c * cellSize + cellSize / 2, d.r * cellSize + cellSize / 2, dotSize, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
});
|
||||
}
|
||||
|
||||
window.resetGame = resetGame;
|
||||
window.exitGame = exitGame;
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
// ui.js
|
||||
import { encryptFile, decryptFile } from './fileops.js';
|
||||
|
||||
/**
|
||||
* Initialize all UI functionality after DOM is loaded
|
||||
*/
|
||||
export function setupUI() {
|
||||
toggleEncryptionOptions();
|
||||
toggleInputMode();
|
||||
|
||||
const encryptionTypeEl = document.getElementById("encryption-type");
|
||||
const inputTextEl = document.getElementById("input-text");
|
||||
const formEl = document.getElementById("crypto-form");
|
||||
const removeFileBtn = document.getElementById("remove-file-btn");
|
||||
const clearAllBtn = document.getElementById("clear-all-btn");
|
||||
const generateBtn = document.getElementById("generate-btn");
|
||||
const copyPasswordBtn = document.getElementById("copy-btn");
|
||||
const copyOutputBtn = document.getElementById("copy-output-btn");
|
||||
const toggleSwitch = document.getElementById("operation-toggle");
|
||||
const copyShareBtn = document.getElementById("copy-share-btn");
|
||||
const shareLink = document.getElementById("share-link");
|
||||
|
||||
if (
|
||||
encryptionTypeEl && inputTextEl && formEl && removeFileBtn &&
|
||||
clearAllBtn && generateBtn && copyPasswordBtn && toggleSwitch
|
||||
) {
|
||||
encryptionTypeEl.addEventListener("change", toggleEncryptionOptions);
|
||||
inputTextEl.addEventListener("input", () => {
|
||||
toggleInputMode();
|
||||
checkForPacman();
|
||||
});
|
||||
formEl.addEventListener("submit", handleSubmit);
|
||||
removeFileBtn.addEventListener("click", removeFile);
|
||||
clearAllBtn.addEventListener("click", clearAll);
|
||||
generateBtn.addEventListener("click", generateRandomPassword);
|
||||
copyPasswordBtn.addEventListener("click", () => copyToClipboard("generated-password", "password-copy-feedback"));
|
||||
copyOutputBtn?.addEventListener("click", () => copyToClipboard("output-text", "output-copy-feedback"));
|
||||
toggleSwitch.addEventListener("change", updateToggleLabels);
|
||||
|
||||
const copySharedLinkBtn = document.getElementById("copy-shared-link");
|
||||
const sharedLinkEl = document.getElementById("shared-link");
|
||||
|
||||
if (copySharedLinkBtn && sharedLinkEl) {
|
||||
copySharedLinkBtn.addEventListener("click", () => {
|
||||
navigator.clipboard.writeText(sharedLinkEl.textContent.trim()).then(() => {
|
||||
const feedback = document.getElementById("shared-link-feedback");
|
||||
if (feedback) {
|
||||
feedback.classList.remove("hidden");
|
||||
feedback.classList.add("show");
|
||||
|
||||
setTimeout(() => {
|
||||
feedback.classList.remove("show");
|
||||
feedback.classList.add("hidden");
|
||||
}, 3000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
sharedLinkEl.scrollIntoView({ behavior: "smooth" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function toggleEncryptionOptions() {
|
||||
const type = document.getElementById("encryption-type").value.trim().toLowerCase();
|
||||
const passwordInputWrapper = document.getElementById("password-input");
|
||||
const isAdvanced = type.includes("advanced");
|
||||
|
||||
if (passwordInputWrapper) {
|
||||
if (isAdvanced) {
|
||||
passwordInputWrapper.classList.remove("hidden");
|
||||
} else {
|
||||
passwordInputWrapper.classList.add("hidden");
|
||||
}
|
||||
}
|
||||
|
||||
updateToggleLabels();
|
||||
toggleInputMode();
|
||||
}
|
||||
|
||||
|
||||
function updateToggleLabels() {
|
||||
const type = document.getElementById("encryption-type")?.value;
|
||||
const leftLabel = document.getElementById("toggle-left-label");
|
||||
const rightLabel = document.getElementById("toggle-right-label");
|
||||
|
||||
if (!type || !leftLabel || !rightLabel) return;
|
||||
|
||||
const isAdvanced = type.toLowerCase().includes("advanced");
|
||||
leftLabel.textContent = isAdvanced ? "Encrypt" : "Encode";
|
||||
rightLabel.textContent = isAdvanced ? "Decrypt" : "Decode";
|
||||
}
|
||||
|
||||
function toggleInputMode() {
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const textValue = document.getElementById("input-text")?.value.trim();
|
||||
const isAdvanced = document.getElementById("encryption-type")?.value === "advanced";
|
||||
|
||||
const textSection = document.getElementById("text-section");
|
||||
const fileSection = document.getElementById("file-section");
|
||||
const removeBtn = document.getElementById("remove-file-btn");
|
||||
|
||||
if (!fileInput || !textSection || !fileSection || !removeBtn) return;
|
||||
|
||||
const fileSelected = fileInput.files.length > 0;
|
||||
|
||||
textSection.style.display = fileSelected ? "none" : "flex";
|
||||
fileSection.style.display = (isAdvanced && !textValue) ? "flex" : "none";
|
||||
removeBtn.style.display = fileSelected ? "inline-block" : "none";
|
||||
}
|
||||
|
||||
async function handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const encryptionType = document.getElementById("encryption-type")?.value;
|
||||
const password = document.getElementById("password")?.value;
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const isDecrypt = document.getElementById("operation-toggle").checked;
|
||||
const operation = isDecrypt ? "decrypt" : "encrypt";
|
||||
|
||||
if (!encryptionType || !fileInput) return;
|
||||
|
||||
if (encryptionType === "advanced" && !password) {
|
||||
return alert("Password is required for advanced encryption.");
|
||||
}
|
||||
|
||||
if (fileInput.files.length > 0) {
|
||||
return (operation === "encrypt")
|
||||
? encryptFile(fileInput, password)
|
||||
: decryptFile(fileInput, password);
|
||||
}
|
||||
|
||||
const payload = {
|
||||
"encryption-type": encryptionType,
|
||||
operation: operation,
|
||||
message: document.getElementById("input-text")?.value,
|
||||
password: password
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch("/", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
const data = await response.json();
|
||||
document.getElementById("output-text").value = data.result;
|
||||
} catch (err) {
|
||||
alert("Error processing request: " + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
function removeFile() {
|
||||
const fileInput = document.getElementById("file-input");
|
||||
if (fileInput) fileInput.value = "";
|
||||
const removeBtn = document.getElementById("remove-file-btn");
|
||||
if (removeBtn) removeBtn.style.display = 'none';
|
||||
toggleInputMode();
|
||||
}
|
||||
|
||||
function generateRandomPassword() {
|
||||
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+[]{}|;:,.<>?/~";
|
||||
const length = 30;
|
||||
const password = Array.from({ length }, () =>
|
||||
charset.charAt(Math.floor(Math.random() * charset.length))
|
||||
).join("");
|
||||
const passwordField = document.getElementById("generated-password");
|
||||
if (passwordField) passwordField.value = password;
|
||||
}
|
||||
|
||||
function copyToClipboard(elementId, feedbackId) {
|
||||
const el = document.getElementById(elementId);
|
||||
const feedback = document.getElementById(feedbackId);
|
||||
if (!el || !feedback) return;
|
||||
|
||||
navigator.clipboard.writeText(el.textContent || el.value || "").then(() => {
|
||||
feedback.classList.add("show");
|
||||
setTimeout(() => {
|
||||
feedback.classList.remove("show");
|
||||
}, 3000);
|
||||
});
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
const fields = ["input-text", "output-text", "file-input", "password"];
|
||||
fields.forEach(id => {
|
||||
const el = document.getElementById(id);
|
||||
if (el) el.value = "";
|
||||
});
|
||||
removeFile();
|
||||
toggleInputMode();
|
||||
document.getElementById("pacman-section")?.style.setProperty("display", "none");
|
||||
document.getElementById("encoding-section")?.style.setProperty("display", "block");
|
||||
}
|
||||
|
||||
function checkForPacman() {
|
||||
const val = document.getElementById("input-text").value.trim().toLowerCase();
|
||||
const pacSection = document.getElementById("pacman-section");
|
||||
const encSection = document.getElementById("encoding-section");
|
||||
|
||||
if (val.includes("pacman") && pacSection.style.display !== "block") {
|
||||
pacSection.style.display = "block";
|
||||
encSection.style.display = "none";
|
||||
window.startPacman();
|
||||
} else if (pacSection.style.display === "block" && !val.includes("pacman")) {
|
||||
window.exitGame();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function startPacman() { }
|
||||
function exitGame() { }
|
||||
Reference in New Issue
Block a user