-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
117 lines (105 loc) · 3.78 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixelflut Pico Interface</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
}
canvas {
border: 1px solid black;
cursor: crosshair;
}
#controls {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Pixelflut Interface</h1>
<canvas id="ledCanvas" width="384" height="384"></canvas>
<div id="controls">
<label for="colorPicker">Choose Color: </label>
<input type="color" id="colorPicker" value="#ff0000">
</div>
<script>
const canvas = document.getElementById('ledCanvas');
const ctx = canvas.getContext('2d');
const colorPicker = document.getElementById('colorPicker');
const gridSize = 128;
const cellSize = 3; // Each cell is 3x3 pixels
let isDrawing = false;
// Draw the grid initially
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
ctx.fillStyle = "#000"; // Default to black
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
// Update pixel on the server with retry mechanism
function setPixel(x, y, color, retries = 3) {
const colorHex = color.replace('#', '');
fetch(`/px?x=${x}&y=${y}&color=${colorHex}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => console.log(data))
.catch(error => {
console.error('Error:', error);
if (retries > 0) {
setTimeout(() => setPixel(x, y, color, retries - 1), 100);
}
});
}
// Handle drawing on the canvas
function drawOnCanvas(x, y) {
// Get the selected color
const color = colorPicker.value;
// Set the pixel on the server
setPixel(x, y, color);
// Update the canvas immediately
ctx.fillStyle = color;
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
// Event listeners for mouse interactions
canvas.addEventListener('mousedown', (event) => {
isDrawing = true;
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
const x = Math.floor(mouseX / cellSize);
const y = Math.floor(mouseY / cellSize);
drawOnCanvas(x, y);
});
canvas.addEventListener('mousemove', (event) => {
if (isDrawing) {
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
const x = Math.floor(mouseX / cellSize);
const y = Math.floor(mouseY / cellSize);
drawOnCanvas(x, y);
}
});
canvas.addEventListener('mouseup', () => {
isDrawing = false;
});
canvas.addEventListener('mouseleave', () => {
isDrawing = false;
});
// Initialize the canvas with the grid
drawGrid();
</script>
</body>
</html>