-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryPad.html
More file actions
665 lines (619 loc) · 22.4 KB
/
GeometryPad.html
File metadata and controls
665 lines (619 loc) · 22.4 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="htmltoapp:open-with" content="role=editor; files=jpg,jpeg,png,webp; camera=false; microphone=false; location=false">
<title>GeometryPad</title>
<style>
:root {
--bg: #131719;
--panel: rgba(20, 26, 28, 0.84);
--line: rgba(255,255,255,0.08);
--text: #eef3f1;
--muted: #93a2a7;
--accent: #8de0d4;
--accent-2: #f2a55c;
--danger: #ed7f7f;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); font-family: "Avenir Next", "Segoe UI", sans-serif; }
body {
background:
radial-gradient(circle at top left, rgba(141,224,212,0.12), transparent 28%),
radial-gradient(circle at bottom right, rgba(242,165,92,0.12), transparent 30%),
linear-gradient(180deg, #171d1f, #0f1214 65%);
}
.app {
height: 100%;
display: grid;
grid-template-columns: 320px minmax(0, 1fr);
}
.sidebar, .workspace {
background: var(--panel);
backdrop-filter: blur(18px);
}
.sidebar {
border-right: 1px solid var(--line);
display: grid;
grid-template-rows: auto auto auto 1fr;
min-height: 0;
}
.section {
padding: 18px;
border-bottom: 1px solid var(--line);
}
.eyebrow {
font-size: 11px;
letter-spacing: 0.18em;
text-transform: uppercase;
font-weight: 800;
color: var(--accent);
}
h1 {
margin: 10px 0 6px;
font-size: 34px;
letter-spacing: -0.05em;
}
p { margin: 0; color: var(--muted); line-height: 1.5; }
.button-grid, .palette {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 12px;
}
button, input, label {
font: inherit;
}
button {
border: 1px solid var(--line);
background: rgba(255,255,255,0.06);
color: var(--text);
border-radius: 999px;
padding: 9px 12px;
cursor: pointer;
}
button.active { background: linear-gradient(135deg, var(--accent), #59ccb9); color: #0b2622; border-color: transparent; font-weight: 800; }
button.warn { border-color: rgba(237,127,127,0.3); color: #ffd0d0; }
.row {
display: grid;
gap: 8px;
margin-top: 12px;
}
.row label { color: var(--muted); font-size: 13px; }
.assets {
overflow: auto;
padding: 12px;
display: grid;
gap: 10px;
align-content: start;
}
.asset {
width: 100%;
text-align: left;
border-radius: 18px;
padding: 10px;
display: grid;
grid-template-columns: 60px minmax(0, 1fr);
gap: 10px;
align-items: center;
}
.asset.active { border-color: rgba(242,165,92,0.36); background: rgba(242,165,92,0.13); }
.asset img {
width: 60px;
height: 60px;
object-fit: cover;
border-radius: 14px;
background: rgba(255,255,255,0.05);
}
.asset strong { display: block; font-size: 13px; word-break: break-word; }
.asset span { display: block; font-size: 12px; color: var(--muted); margin-top: 4px; word-break: break-word; }
.workspace {
min-width: 0;
display: grid;
grid-template-rows: auto 1fr;
}
.workspace-head {
padding: 18px 22px;
border-bottom: 1px solid var(--line);
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
}
.canvas-wrap {
min-height: 0;
padding: 22px;
}
.canvas-shell {
width: 100%;
height: 100%;
position: relative;
border-radius: 28px;
overflow: hidden;
border: 1px solid var(--line);
background:
linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px),
linear-gradient(rgba(255,255,255,0.04) 1px, transparent 1px),
linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
background-size: 24px 24px, 24px 24px, auto;
}
.canvas-shell img, .canvas-shell canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
.canvas-shell img {
object-fit: contain;
background: rgba(0,0,0,0.12);
pointer-events: none;
}
canvas { cursor: crosshair; }
a.brand-link { color: var(--accent); text-decoration: none; font-weight: 700; }
.drop-overlay {
position: fixed;
inset: 18px;
border-radius: 30px;
border: 2px dashed rgba(141,224,212,0.55);
background: rgba(12, 15, 16, 0.78);
backdrop-filter: blur(12px);
display: grid;
place-items: center;
text-align: center;
padding: 28px;
z-index: 100;
pointer-events: none;
}
.drop-overlay strong {
display: block;
font-size: 28px;
letter-spacing: -0.04em;
margin-bottom: 8px;
}
.drop-overlay span {
color: var(--muted);
max-width: 32rem;
line-height: 1.5;
}
.drop-overlay[hidden] { display: none !important; }
@media (max-width: 1000px) {
.app { grid-template-columns: 1fr; grid-template-rows: 360px 1fr; }
.sidebar { border-right: 0; border-bottom: 1px solid var(--line); }
}
</style>
</head>
<body>
<div class="app">
<aside class="sidebar">
<section class="section">
<div class="eyebrow">HTML to App Demo</div>
<h1>GeometryPad</h1>
<p>Draw precise shapes on a blank board or on top of an image opened from Finder. Export the composition as a PNG.</p>
<p style="margin-top:8px;">Made for <a class="brand-link" href="https://htmltoapp.app">HTML to App</a>.</p>
</section>
<section class="section">
<div class="eyebrow">Tools</div>
<div class="button-grid" id="toolButtons">
<button data-tool="line" class="active">Line</button>
<button data-tool="rect">Rect</button>
<button data-tool="ellipse">Ellipse</button>
<button data-tool="arrow">Arrow</button>
<button data-tool="label">Label</button>
</div>
<div class="row">
<label for="strokeColor">Stroke Color</label>
<input id="strokeColor" type="color" value="#8de0d4">
</div>
<div class="row">
<label for="strokeWidth">Stroke Width</label>
<input id="strokeWidth" type="range" min="1" max="18" value="4">
</div>
</section>
<section class="section">
<div class="eyebrow">Actions</div>
<div class="button-grid">
<button id="undoButton">Undo</button>
<button id="clearButton" class="warn">Clear</button>
<button id="exportButton">Export PNG</button>
</div>
</section>
<section class="assets" id="assets"></section>
</aside>
<section class="workspace">
<div class="workspace-head">
<div>
<div class="eyebrow">Canvas</div>
<div id="status">Blank board ready. Drag an image or image folder here to use it as a background.</div>
</div>
<div id="pathLabel" style="color: var(--muted); font-size: 13px;">No background image selected</div>
</div>
<div class="canvas-wrap">
<div class="canvas-shell" id="canvasShell">
<img id="backgroundImage" alt="" hidden>
<canvas id="canvas"></canvas>
</div>
</div>
</section>
</div>
<div class="drop-overlay" id="dropOverlay" hidden>
<div>
<strong>Drop images or folders</strong>
<span>GeometryPad will import supported image files so you can draw on top of them.</span>
</div>
</div>
<script>
const IMAGE_EXTENSIONS = new Set(["jpg", "jpeg", "png", "gif", "webp", "bmp", "tif", "tiff"]);
const state = {
tool: "line",
shapes: [],
drawing: null,
assets: [],
assetIndex: -1
};
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const backgroundImage = document.getElementById("backgroundImage");
const assetsEl = document.getElementById("assets");
const statusEl = document.getElementById("status");
const pathLabel = document.getElementById("pathLabel");
const toolButtons = document.getElementById("toolButtons");
const strokeColor = document.getElementById("strokeColor");
const strokeWidth = document.getElementById("strokeWidth");
const dropOverlay = document.getElementById("dropOverlay");
let dropDepth = 0;
function extensionOf(name) {
const dot = name.lastIndexOf(".");
return dot === -1 ? "" : name.slice(dot + 1).toLowerCase();
}
function isImageItem(item) {
return !item.isDirectory && IMAGE_EXTENSIONS.has(extensionOf(item.name || item.path || ""));
}
async function listDirectory(item) {
const response = await fetch(item.listingURL || item.baseURL);
const payload = await response.json();
return Array.isArray(payload.entries) ? payload.entries : [];
}
function makeDroppedItem(file, explicitPath = "") {
const rawPath = explicitPath || file.webkitRelativePath || file.name;
return {
name: file.name,
path: rawPath.replace(/^\/+/, "") || file.name,
url: URL.createObjectURL(file),
isDirectory: false
};
}
function readFileEntry(entry) {
return new Promise((resolve, reject) => entry.file(resolve, reject));
}
function readDirectoryBatch(reader) {
return new Promise((resolve, reject) => reader.readEntries(resolve, reject));
}
async function collectDroppedFilesFromEntry(entry) {
if (!entry) return [];
if (entry.isFile) {
const file = await readFileEntry(entry);
return [makeDroppedItem(file, entry.fullPath || file.webkitRelativePath || file.name)];
}
if (!entry.isDirectory) return [];
const files = [];
const reader = entry.createReader();
while (true) {
const batch = await readDirectoryBatch(reader);
if (!batch.length) break;
for (const child of batch) {
const childFiles = await collectDroppedFilesFromEntry(child);
files.push(...childFiles);
}
}
return files;
}
async function collectDroppedFiles(dataTransfer) {
const files = [];
const items = Array.from(dataTransfer.items || []);
if (items.length) {
for (const item of items) {
if (item.kind !== "file") continue;
const entry = typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
if (entry) {
try {
const entryFiles = await collectDroppedFilesFromEntry(entry);
files.push(...entryFiles);
} catch (error) {
console.error("Failed to read dropped entry", error);
}
continue;
}
const file = item.getAsFile ? item.getAsFile() : null;
if (file) files.push(makeDroppedItem(file));
}
} else {
for (const file of Array.from(dataTransfer.files || []))
files.push(makeDroppedItem(file));
}
return files;
}
function dragHasFiles(event) {
return !!(event.dataTransfer && Array.from(event.dataTransfer.types || []).includes("Files"));
}
function showDropOverlay() {
dropOverlay.hidden = false;
}
function hideDropOverlay() {
dropDepth = 0;
dropOverlay.hidden = true;
}
function resizeCanvas() {
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
canvas.width = Math.max(1, Math.floor(rect.width * dpr));
canvas.height = Math.max(1, Math.floor(rect.height * dpr));
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
redraw();
}
function pointFromEvent(event) {
const rect = canvas.getBoundingClientRect();
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
}
function renderShapeOnContext(targetCtx, shape) {
targetCtx.save();
targetCtx.lineWidth = shape.width;
targetCtx.strokeStyle = shape.color;
targetCtx.fillStyle = shape.color;
targetCtx.lineCap = "round";
targetCtx.lineJoin = "round";
if (shape.type === "line" || shape.type === "arrow") {
targetCtx.beginPath();
targetCtx.moveTo(shape.x1, shape.y1);
targetCtx.lineTo(shape.x2, shape.y2);
targetCtx.stroke();
if (shape.type === "arrow") {
const angle = Math.atan2(shape.y2 - shape.y1, shape.x2 - shape.x1);
const head = Math.max(12, shape.width * 3.5);
targetCtx.beginPath();
targetCtx.moveTo(shape.x2, shape.y2);
targetCtx.lineTo(shape.x2 - head * Math.cos(angle - Math.PI / 7), shape.y2 - head * Math.sin(angle - Math.PI / 7));
targetCtx.lineTo(shape.x2 - head * Math.cos(angle + Math.PI / 7), shape.y2 - head * Math.sin(angle + Math.PI / 7));
targetCtx.closePath();
targetCtx.fill();
}
} else if (shape.type === "rect") {
const x = Math.min(shape.x1, shape.x2);
const y = Math.min(shape.y1, shape.y2);
const w = Math.abs(shape.x2 - shape.x1);
const h = Math.abs(shape.y2 - shape.y1);
targetCtx.strokeRect(x, y, w, h);
} else if (shape.type === "ellipse") {
const cx = (shape.x1 + shape.x2) / 2;
const cy = (shape.y1 + shape.y2) / 2;
const rx = Math.abs(shape.x2 - shape.x1) / 2;
const ry = Math.abs(shape.y2 - shape.y1) / 2;
targetCtx.beginPath();
targetCtx.ellipse(cx, cy, rx, ry, 0, 0, Math.PI * 2);
targetCtx.stroke();
} else if (shape.type === "label") {
targetCtx.font = `${Math.max(16, shape.width * 6)}px "Avenir Next", sans-serif`;
targetCtx.fillText(shape.text, shape.x1, shape.y1);
}
targetCtx.restore();
}
function drawShape(shape) {
renderShapeOnContext(ctx, shape);
}
function redraw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
state.shapes.forEach(drawShape);
if (state.drawing) drawShape(state.drawing);
}
function renderAssets() {
assetsEl.innerHTML = "";
if (!state.assets.length) {
assetsEl.innerHTML = `<p>No opened images yet. Launch the generated app with an image file or an image folder.</p>`;
return;
}
state.assets.forEach((asset, index) => {
const button = document.createElement("button");
button.className = "asset" + (index === state.assetIndex ? " active" : "");
button.innerHTML = `<img src="${asset.url}" alt=""><div><strong>${asset.name}</strong><span>${asset.path}</span></div>`;
button.addEventListener("click", () => {
setBackground(index);
});
assetsEl.appendChild(button);
});
}
function setBackground(index) {
state.assetIndex = index;
const asset = state.assets[index];
if (!asset) {
backgroundImage.hidden = true;
backgroundImage.removeAttribute("src");
pathLabel.textContent = "No background image selected";
} else {
backgroundImage.hidden = false;
backgroundImage.src = asset.url;
pathLabel.textContent = asset.path;
}
renderAssets();
}
function applyTool(tool) {
state.tool = tool;
toolButtons.querySelectorAll("button").forEach((button) => button.classList.toggle("active", button.dataset.tool === tool));
statusEl.textContent = `Tool: ${tool}`;
}
function shapeFromPoints(start, end) {
const base = {
type: state.tool,
color: strokeColor.value,
width: Number(strokeWidth.value),
x1: start.x,
y1: start.y,
x2: end.x,
y2: end.y
};
if (state.tool === "label") {
const text = window.prompt("Label text", "GeometryPad");
if (!text) return null;
base.text = text;
base.x2 = start.x;
base.y2 = start.y;
}
return base;
}
canvas.addEventListener("pointerdown", (event) => {
const start = pointFromEvent(event);
const shape = shapeFromPoints(start, start);
if (!shape) return;
state.drawing = shape;
if (shape.type === "label") {
state.shapes.push(shape);
state.drawing = null;
redraw();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!state.drawing) return;
const point = pointFromEvent(event);
state.drawing.x2 = point.x;
state.drawing.y2 = point.y;
redraw();
});
window.addEventListener("pointerup", () => {
if (!state.drawing) return;
state.shapes.push(state.drawing);
state.drawing = null;
redraw();
});
document.getElementById("undoButton").addEventListener("click", () => {
state.shapes.pop();
redraw();
});
document.getElementById("clearButton").addEventListener("click", () => {
state.shapes = [];
redraw();
});
document.getElementById("exportButton").addEventListener("click", () => {
const rect = canvas.getBoundingClientRect();
const exportCanvas = document.createElement("canvas");
exportCanvas.width = Math.max(1, Math.floor(rect.width));
exportCanvas.height = Math.max(1, Math.floor(rect.height));
const exportCtx = exportCanvas.getContext("2d");
exportCtx.fillStyle = "#101517";
exportCtx.fillRect(0, 0, exportCanvas.width, exportCanvas.height);
if (!backgroundImage.hidden && backgroundImage.complete) {
const imageAspect = backgroundImage.naturalWidth / backgroundImage.naturalHeight;
const canvasAspect = exportCanvas.width / exportCanvas.height;
let drawWidth = exportCanvas.width;
let drawHeight = exportCanvas.height;
let offsetX = 0;
let offsetY = 0;
if (imageAspect > canvasAspect) {
drawHeight = exportCanvas.width / imageAspect;
offsetY = (exportCanvas.height - drawHeight) / 2;
} else {
drawWidth = exportCanvas.height * imageAspect;
offsetX = (exportCanvas.width - drawWidth) / 2;
}
exportCtx.drawImage(backgroundImage, offsetX, offsetY, drawWidth, drawHeight);
}
const scaleX = exportCanvas.width / rect.width;
const scaleY = exportCanvas.height / rect.height;
exportCtx.scale(scaleX, scaleY);
const drawOn = exportCtx;
for (const shape of state.shapes) {
renderShapeOnContext(drawOn, shape);
}
exportCanvas.toBlob((blob) => {
if (!blob) {
statusEl.textContent = "Export failed";
return;
}
const objectURL = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = objectURL;
link.download = "geometry-pad-export.png";
document.body.appendChild(link);
link.click();
link.remove();
window.setTimeout(() => URL.revokeObjectURL(objectURL), 1000);
statusEl.textContent = "Exported PNG";
}, "image/png");
});
async function handleLaunchItems(items, replaceExisting = false) {
const assets = [];
for (const item of items) {
if (!item) continue;
if (isImageItem(item)) {
assets.push({ name: item.name, path: item.path, url: item.url });
continue;
}
if (item.isDirectory && (item.listingURL || item.baseURL)) {
try {
const entries = await listDirectory(item);
for (const entry of entries) if (isImageItem(entry)) assets.push({ name: entry.name, path: entry.path, url: entry.url });
} catch (error) {
console.error(error);
}
}
}
state.assets = replaceExisting
? assets
: state.assets.concat(assets.filter((asset) => !state.assets.some((existing) => existing.path === asset.path)));
renderAssets();
if (replaceExisting && state.assets.length) setBackground(0);
if (!replaceExisting && state.assetIndex === -1 && state.assets.length) setBackground(0);
}
toolButtons.addEventListener("click", (event) => {
const button = event.target.closest("button[data-tool]");
if (!button) return;
applyTool(button.dataset.tool);
});
window.addEventListener("resize", resizeCanvas);
window.addEventListener("htmltoapp-open", (event) => {
const detail = event.detail || {};
handleLaunchItems(detail.items || [], !!detail.replaceExisting);
});
window.addEventListener("dragenter", (event) => {
if (!dragHasFiles(event)) return;
event.preventDefault();
dropDepth += 1;
showDropOverlay();
});
window.addEventListener("dragover", (event) => {
if (!dragHasFiles(event)) return;
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
showDropOverlay();
});
window.addEventListener("dragleave", (event) => {
if (!dragHasFiles(event)) return;
dropDepth = Math.max(0, dropDepth - 1);
if (dropDepth === 0) hideDropOverlay();
});
window.addEventListener("drop", async (event) => {
if (!dragHasFiles(event)) return;
event.preventDefault();
hideDropOverlay();
const files = await collectDroppedFiles(event.dataTransfer);
if (files.length) handleLaunchItems(files, true);
});
applyTool("line");
resizeCanvas();
handleLaunchItems((window.HTMLtoApp && window.HTMLtoApp.launchItems) || [], true);
</script>
<script>
(function () {
function showHTMLtoAppBrowserNotice() {
if (window.HTMLtoApp || document.getElementById("htmltoapp-browser-notice")) return;
const notice = document.createElement("aside");
notice.id = "htmltoapp-browser-notice";
notice.setAttribute("role", "note");
notice.style.cssText = "position:fixed;left:16px;right:16px;bottom:16px;z-index:2147483647;padding:14px 16px;border-radius:10px;background:#111827;color:#fff;box-shadow:0 12px 32px rgba(0,0,0,.28);font:14px/1.45 -apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif";
notice.innerHTML = '<strong style="display:block;margin-bottom:2px;">HTML to App example</strong><span>These examples are made to work in HTML to App only. Visit <a href="https://htmltoapp.app" style="color:#93c5fd;font-weight:700;">https://htmltoapp.app</a> for more information.</span>';
document.body.appendChild(notice);
}
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", showHTMLtoAppBrowserNotice);
else showHTMLtoAppBrowserNotice();
}());
</script>
</body>
</html>