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
import { frameHtml } from "https://esm.town/v/moe/frameHtmlRaw"
export function sketch(module: string, title = "P5 Sketch") {
return async function(req: Request): Promise<Response> {
// console.log("module:", module)
const url = new URL(req.url)
// console.log("url:", url)
const isImage = url.pathname.endsWith("/image")
if (isImage) {
const imageUrl = "https://charlypoly-httpapiscreenshotpageexample.web.val.run/?url=" + url.origin
// return Response.redirect(imageUrl, 302)
const res = await fetch(imageUrl)
const blob = await res.blob()
return new Response(blob, { headers: { "Content-Type": "image/png" } })
}
const randomPostfix = r()
const baseUrl = url.origin
const homeFrame = {
image: `/${randomPostfix}/image`,
aspectRatio: "1:1",
buttons: [
{ text: "Randomize", target: `/${r()}` },
],
}
const html = `
<html>
<head>
<title>${title}</title>
<meta property="og:image" content="${url.origin}/${randomPostfix}/image" />
<meta property="og:image:width" content="800" />
<meta property="og:image:height" content="800" />
<meta name="twitter:card" content="summary_large_image" />
${frameHtml(homeFrame, baseUrl)}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/p5.min.js" integrity="sha512-lvddmeF7aHRJwdbJeYThWd5kWSjTrXBzCRF/jYROiHzmhMJ1dEXfGH5Q7ft0yhizXTopAETG03s5ajTflauijA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/addons/p5.sound.min.js" integrity="sha512-WzkwpdWEMAY/W8WvP9KS2/VI6zkgejR4/KTxTl4qHx0utqeyVE0JY+S1DlMuxDChC7x0oXtk/ESji6a0lP/Tdg==" crossorigin="anonymous" referrerpolicy="no-referr
<link rel="shortcut icon" href="https://p5js.org/assets/img/favicon.ico" />
<link rel="icon" href="https://p5js.org/assets/img/favicon.ico" />
<style>
* {
padding: 0px;
margin: 0px;
}
main {
width: 100%;
height: 100%;
display: flex;
}
canvas {
margin: auto;
}
</style>
<script type="module">
import * as sketch from "${module}";
for (let f of Object.getOwnPropertyNames(sketch)) {
if (f !== "default") {
window[f] = sketch[f];
}
}
if (sketch.title) document.title = sketch.title;
</script>
</head>
<body>
</body>
</html>`
return new Response(html, { headers: { "Content-Type": "text/html" } })
}
}
function r() {
return Math.floor(Math.random() * 100)
}