-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (130 loc) · 5.05 KB
/
Copy pathscript.js
File metadata and controls
152 lines (130 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const morseCodeMap = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.'
};
const arabicToMorse = {
'ا': '.-', 'ب': '-...', 'ت': '-', 'ث': '-..', 'ج': '.---', 'ح': '....', 'خ': '--.',
'د': '-..', 'ذ': '--..', 'ر': '.-.', 'ز': '--.', 'س': '...', 'ش': '----',
'ص': '---.', 'ض': '.--.', 'ط': '-.-.', 'ظ': '.-.-', 'ع': '.-..-', 'غ': '--..-',
'ف': '..-.', 'ق': '--.-', 'ك': '-.-', 'ل': '.-..', 'م': '--', 'ن': '-.',
'ه': '....', 'و': '.--', 'ي': '-.--', 'ئ': '.--..', 'ء': '...-', 'أ': '.-.-',
'ؤ': '-.-.', 'إ': '.-..', 'لا': '-..-.-', 'ل': '.-..'
};
const reverseMorseCodeMap = Object.fromEntries(
Object.entries(morseCodeMap).map(([letter, morse]) => [morse, letter])
);
const reverseArabicMorseCodeMap = Object.fromEntries(
Object.entries(arabicToMorse).map(([letter, morse]) => [morse, letter])
);
function encode() {
const text = document.getElementById('textInput').value.toUpperCase();
const morse = text.split('').map(char => {
if (char === ' ') return '/';
return morseCodeMap[char] || arabicToMorse[char] || '';
}).join(' ');
document.getElementById('morseOutput').value = morse;
}
function decode() {
const morse = document.getElementById('morseOutput').value.trim();
const text = morse.split(' ').map(code => {
if (code === '/') return ' ';
return reverseMorseCodeMap[code] || reverseArabicMorseCodeMap[code] || '';
}).join('');
document.getElementById('textInput').value = text;
}
function encodeDecode() {
const textInput = document.getElementById('textInput').value.trim();
const morseOutput = document.getElementById('morseOutput').value.trim();
if ((textInput === "" && morseOutput === "")) {
return;
}
const normalTextVisible = document.getElementById('textInput').style.display === 'block';
if (normalTextVisible) {
encode();
toggleInput('morse');
} else {
decode();
toggleInput('normal');
}
}
function toggleInput(type) {
if (type === 'normal') {
document.getElementById('textInput').style.display = 'block';
document.getElementById('morseOutput').style.display = 'none';
document.getElementById('normalTextToggle').classList.add('active');
document.getElementById('morseCodeToggle').classList.remove('active');
} else {
document.getElementById('textInput').style.display = 'none';
document.getElementById('morseOutput').style.display = 'block';
document.getElementById('morseCodeToggle').classList.add('active');
document.getElementById('normalTextToggle').classList.remove('active');
}
}
const canvas = document.getElementById('background');
const ctx = canvas.getContext('2d');
let width = window.innerWidth;
let height = window.innerHeight;
canvas.width = width;
canvas.height = height;
const points = [];
for (let i = 0; i < 100; i++) {
points.push({
x: Math.random() * width,
y: Math.random() * height,
vx: (Math.random() - 0.5) * 0.5,
vy: (Math.random() - 0.5) * 0.5
});
}
function draw() {
ctx.clearRect(0, 0, width, height);
for (let i = 0; i < points.length; i++) {
const p = points[i];
p.x += p.vx;
p.y += p.vy;
if (p.x < 0 || p.x > width) p.vx *= -1;
if (p.y < 0 || p.y > height) p.vy *= -1;
ctx.beginPath();
ctx.arc(p.x, p.y, 2, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
}
for (let i = 0; i < points.length; i++) {
for (let j = i + 1; j < points.length; j++) {
const dx = points[i].x - points[j].x;
const dy = points[i].y - points[j].y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 100) {
ctx.beginPath();
ctx.moveTo(points[i].x, points[i].y);
ctx.lineTo(points[j].x, points[j].y);
ctx.strokeStyle = `rgba(255, 255, 255, ${1 - dist / 100})`;
ctx.stroke();
}
}
}
requestAnimationFrame(draw);
}
draw();
window.addEventListener('resize', () => {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
});
document.addEventListener('mousemove', (e) => {
points.push({
x: e.clientX,
y: e.clientY,
vx: (Math.random() - 0.5) * 2,
vy: (Math.random() - 0.5) * 2
});
if (points.length > 120) points.shift();
});
toggleInput('normal');