-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_gym.html
286 lines (248 loc) · 10.6 KB
/
sensor_gym.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
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
<!--
This file is part of the Sensor Object Playground project.
Copyright (C) 2025 Max Muchen Sun
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<!--
Author: Max Muchen Sun ([email protected])
Date: 2025-02-27
Description: Sensor-Object Playground
-->
<!DOCTYPE html>
<html>
<head>
<title>Sensor-Object Playground</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: white;
}
.container {
position: relative;
width: 640px;
height: 640px;
border: 3px solid black;
margin-bottom: 10px;
}
.camera-view {
position: absolute;
border: 3px solid black;
background-color: white;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
.cross {
position: absolute;
width: 8px;
height: 8px;
background-color: red;
border-radius: 50%;
}
.button-container {
display: flex;
flex-direction: row;
gap: 20px;
align-items: center;
}
.button {
padding: 10px;
font-size: 14px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Sensor-Object Playground</h2>
<h3 style="margin-top: 0; margin-bottom: 0.2;">Author: <a href="https://maxsun.io/" target="_blank">Max Muchen Sun</a> | March, 2025</h3>
<div class="container" id="env-container">
<canvas id="offscreenCanvas" width="640" height="640" style="display:none;"></canvas>
<canvas id="envCanvas" width="640" height="640"></canvas>
<div class="camera-view" id="cameraView">
<canvas id="cameraCanvas"></canvas>
<div class="cross"></div>
</div>
</div>
<div class="button-container">
<button class="button" id="randomizeEnv">Randomize Environment</button>
<label for="camSizeSlider">Camera Size:</label>
<input type="range" id="camSizeSlider" min="10" max="640" step="10" value="64">
<span id="camSizeValue">64</span>
</div>
<script>
const offscreenCanvas = document.getElementById("offscreenCanvas");
const offscreenCtx = offscreenCanvas.getContext("2d");
const canvas = document.getElementById("envCanvas");
const ctx = canvas.getContext("2d");
const cameraCanvas = document.getElementById("cameraCanvas");
const cameraCtx = cameraCanvas.getContext("2d");
const cameraView = document.getElementById("cameraView");
const cross = document.querySelector(".cross");
const randomizeButton = document.getElementById("randomizeEnv");
const camSizeSlider = document.getElementById("camSizeSlider");
const camSizeValue = document.getElementById("camSizeValue");
const envSize = 640;
// const camSize = 50;
let camSize = parseInt(camSizeSlider.value);
let cameraX = envSize / 2;
let cameraY = envSize / 2;
let targetX = cameraX;
let targetY = cameraY;
const trackingSpeed = 0.1;
let showGlobalView = false;
let previousShowGlobalView = showGlobalView;
const colors = ["#1F77B4", "#FF7F0E", "#2CA02C"];
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function getNonOverlappingPosition(size, minDistance, existingObjects) {
let x, y;
let attempts = 0;
let overlapping;
do {
if (attempts++ > 1000) {
console.warn("Could not find a suitable position for a shape.");
return null; // Avoid infinite loops
}
x = Math.random() * (envSize - size);
y = Math.random() * (envSize - size);
overlapping = existingObjects.some(obj => {
let dx = obj.x + obj.size / 2 - (x + size / 2);
let dy = obj.y + obj.size / 2 - (y + size / 2);
let distance = Math.sqrt(dx * dx + dy * dy);
return distance < (obj.size / 2 + size / 2 + minDistance);
});
} while (overlapping);
existingObjects.push({ x, y, size });
return { x, y };
}
function drawRotatedRect(ctx, x, y, width, height, angle, color) {
ctx.save();
ctx.translate(x + width / 2, y + height / 2);
ctx.rotate(angle);
ctx.fillStyle = color;
ctx.fillRect(-width / 2, -height / 2, width, height);
ctx.restore();
}
function drawRotatedTriangle(ctx, x, y, size, angle, color) {
ctx.save();
ctx.translate(x + size / 2, y + size / 2);
ctx.rotate(angle);
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(-size / 2, size / 2);
ctx.lineTo(size / 2, size / 2);
ctx.lineTo(0, -size / 2);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function drawEnvironment(ctx) {
ctx.fillStyle = "#F0F0F0";
ctx.fillRect(0, 0, envSize, envSize);
shuffleArray(colors);
const existingObjects = [];
const minDistance = 50; // Minimum distance between shapes
// Randomized sizes
const squareSize = Math.random() * 80 + 150; // Between 120 and 200
const triangleSize = Math.random() * 100 + 180; // Between 150 and 250
const circleRadius = Math.random() * 50 + 80; // Between 80 and 130
// Get non-overlapping positions while keeping a minimum distance
let squarePos = getNonOverlappingPosition(squareSize, minDistance, existingObjects);
let trianglePos = getNonOverlappingPosition(triangleSize, minDistance, existingObjects);
let circlePos = getNonOverlappingPosition(circleRadius * 2, minDistance, existingObjects);
if (!squarePos || !trianglePos || !circlePos) {
console.error("Failed to place all shapes with the given constraints.");
return;
}
// Assign random rotations
const squareAngle = Math.random() * Math.PI * 2;
const triangleAngle = Math.random() * Math.PI * 2;
// Draw square with rotation
drawRotatedRect(ctx, squarePos.x, squarePos.y, squareSize, squareSize, squareAngle, colors[0]);
// Draw triangle with rotation
drawRotatedTriangle(ctx, trianglePos.x, trianglePos.y, triangleSize, triangleAngle, colors[1]);
// Draw circle
ctx.fillStyle = colors[2];
ctx.beginPath();
ctx.arc(circlePos.x + circleRadius, circlePos.y + circleRadius, circleRadius, 0, Math.PI * 2);
ctx.fill();
}
drawEnvironment(offscreenCtx);
function updateCameraView() {
cameraX += (targetX - cameraX) * trackingSpeed;
cameraY += (targetY - cameraY) * trackingSpeed;
let camX = Math.max(0, Math.min(envSize - camSize - 6, cameraX - camSize / 2));
let camY = Math.max(0, Math.min(envSize - camSize - 6, cameraY - camSize / 2));
cameraView.style.width = `${camSize}px`;
cameraView.style.height = `${camSize}px`;
cameraView.style.left = `${camX}px`;
cameraView.style.top = `${camY}px`;
cross.style.left = `${camSize / 2 - 4}px`;
cross.style.top = `${camSize / 2 - 4}px`;
cameraCtx.clearRect(0, 0, camSize, camSize);
cameraCanvas.width = camSize;
cameraCanvas.height = camSize;
cameraCtx.drawImage(offscreenCanvas, camX, camY, camSize, camSize, 0, 0, camSize, camSize);
}
function animate() {
updateCameraView();
requestAnimationFrame(animate);
}
document.getElementById("env-container").addEventListener("mousemove", (event) => {
const rect = document.getElementById("env-container").getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
targetX = mouseX;
targetY = mouseY;
});
document.getElementById("env-container").addEventListener("touchmove", (event) => {
// Prevent default scrolling behavior on mobile devices.
event.preventDefault();
const rect = document.getElementById("env-container").getBoundingClientRect();
const touch = event.touches[0];
const touchX = touch.clientX - rect.left;
const touchY = touch.clientY - rect.top;
targetX = touchX;
targetY = touchY;
}, { passive: false });
randomizeButton.addEventListener("click", () => {
previousShowGlobalView = showGlobalView;
drawEnvironment(offscreenCtx);
if (previousShowGlobalView) {
ctx.drawImage(offscreenCanvas, 0, 0);
} else {
ctx.clearRect(0, 0, envSize, envSize);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, envSize, envSize);
}
updateCameraView();
});
camSizeSlider.addEventListener("input", function () {
camSize = parseInt(this.value);
camSizeValue.textContent = camSize;
updateCameraView();
});
ctx.fillStyle = "white";
ctx.fillRect(0, 0, envSize, envSize);
animate();
</script>
</body>
</html>