-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockwrap.html
More file actions
295 lines (255 loc) · 6.46 KB
/
blockwrap.html
File metadata and controls
295 lines (255 loc) · 6.46 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Broken Stripes</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
#keys{
position: absolute;
bottom: 0;
right:0;
padding: 5px 20px;
background-color: white;
color: black;
font-family: "Courier New", Courier, monospace;
font-size: 14px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="keys" style="display:none">
<p><b>F</b> fullscreen</p>
<p><b>D</b> direction</p>
<p><b>H</b> orientation</p>
<p><b>R</b> regenerate</p>
<p><b>I</b> invert</p>
<p><b>A</b> <b>S</b> Speed</p>
<p><b>Z</b> <b>X</b> Num stripes</p>
<p><b>N</b> <b>M</b> Num blocks</p>
<p><b>Q</b> <b>W</b> Thickness</p>
</div>
<script>
const keysEl = document.getElementById('keys');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
var stripeWidth = 64;
var numStripes = 8;
var stripeMargin;
var blockHeight = 32; // Height of each block in the stripe
var gapHeight = 32;
var speed = 2;
var offset = 0;
var direction = -1;
var isHorizontal = false; // Flag for orientation
var stripes = []; // Array to store stripe data
var white = 'hsl(0,0%,100%)';
var black = 'hsl(0,0%,0%)';
var backg = black;
var frontg = white;
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if (isHorizontal) {
stripeMargin = (canvas.width - (numStripes * stripeWidth)) / numStripes;
} else {
stripeMargin = (canvas.width - (numStripes * stripeWidth)) / numStripes;
}
generateStripes();
drawStripes();
}
function generateStripes() {
stripes = []; // Clear existing stripes
for (let i = 0; i < numStripes; i++) {
let blocks = [];
let stripePos = i * (stripeWidth + stripeMargin);
let currentY = -canvas.height; // Start off-screen
while (currentY < canvas.height) {
let blockLength = Math.floor(Math.random() * 4) + 1.0; // 1 to 3 blocks long
if (Math.random() > 0.35) { // 50% chance to create a block
blocks.push({
start: currentY,
length: blockLength
});
}
currentY += blockLength * blockHeight + (((Math.random() * 2)+ 0.2) * gapHeight); // Add gap
}
stripes.push({
position: stripePos,
blocks: blocks
});
}
}
function drawStripes() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = backg;//'hsl(0,0%,0%)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = frontg;//'hsl(0,0%,100%)';
for (let stripe of stripes) {
for (let block of stripe.blocks) {
let x = stripe.position;
let y = block.start - (offset * direction);
let w = stripeWidth;
let h = block.length * blockHeight;
// Start off-screen and wrap-around
if (y + h < 0) {
block.start += canvas.height * 2 + h; // Move to bottom if off-screen at top
} else if (y > canvas.height) {
block.start -= canvas.height * 2 + h; // Move to top if off-screen at bottom
}
y = block.start - (offset * direction); // Recalculate y after adjustment
ctx.fillRect(x, y, w, h);
// Duplicate on other side for continuous wrap-around
if (isHorizontal) {
ctx.fillRect(x + canvas.width, y, w, h);
ctx.fillRect(x - canvas.width, y, w, h);
}
}
}
}
function animate() {
offset += speed;
drawStripes();
requestAnimationFrame(animate);
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
animate();
document.addEventListener(
"keydown",
(e) => {
if (e.key === "f") {
toggleFullScreen();
}
if (e.key === "a") {
speedDown();
}
if (e.key === "s") {
speedUp();
}
if (e.key === "z") {
lessStripes();
}
if (e.key === "x") {
moreStripes();
}
if (e.key === "q") {
thinner();
}
if (e.key === "w") {
thicker();
}
if (e.key === "d") {
direction *= -1;
}
if (e.key === "h") {
toggleOrientation();
}
if (e.key === "r") {
generateStripes(); // Regenerate stripes on 'r' key press
}
if(e.key === "i"){
invert()
}
if (e.key === "n") {
lessBlocks();
}
if (e.key === "m") {
moreBlocks();
}
if (e.key === "k") {
toggleKeys();
}
},
false,
);
function toggleOrientation() {
isHorizontal = !isHorizontal;
resizeCanvas();
}
function thinner() {
changeWidth(-1);
}
function thicker() {
changeWidth(1);
}
function changeWidth(widthChange) {
if (stripeWidth <= 64) {
if (widthChange < 0) {
stripeWidth *= 0.5;
} else {
stripeWidth *= 2;
}
} else {
if (widthChange < 0) {
stripeWidth -= 64;
} else {
stripeWidth += 64;
}
}
console.log(stripeWidth);
resizeCanvas();
}
function lessStripes() {
changeStripes(-1);
}
function moreStripes() {
changeStripes(1);
}
function lessBlocks() {
changeBlocks(0.5);
}
function moreBlocks() {
changeBlocks(2);
}
function changeBlocks(blocksChange) {
blockHeight *= blocksChange
resizeCanvas();
}
function changeStripes(stripeChange) {
numStripes += stripeChange;
if (numStripes < 1) {
numStripes = 1;
}
console.log(numStripes);
resizeCanvas();
}
function speedUp() {
changeSpeed(2);
}
function speedDown() {
changeSpeed(0.5);
}
function changeSpeed(multiplier) {
speed *= multiplier;
}
function invert(){
if(frontg == white){
frontg = black;
backg = white;
}else{
frontg = white;
backg = black;
}
}
function toggleFullScreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
} else if (document.exitFullscreen) {
document.exitFullscreen();
}
}
function toggleKeys(){
keysEl.style.display = keysEl.style.display === "none" ? "block" : "none";
}
</script>
</body>
</html>