Forked from saolsen/p5
Public
Script
Readme

P5 sketch

Easily turn a p5.js sketch into a val. See https://www.val.town/v/saolsen/p5_sketch for an example.

Usage

  • Make a p5 sketch, you can import the p5 types to make it easier.
import type * as p5 from "npm:@types/p5";
  • Export any "global" p5 functions. These are functions like setup and draw that p5 will call.

  • Set the val type to http and default export the result of sketch, passing in import.meta.url.

A full example looks like this.

import type * as p5 from "npm:@types/p5"; export function setup() { createCanvas(400, 400); } export function draw() { if (mouseIsPressed) { fill(0); } else { fill(255); } ellipse(mouseX, mouseY, 80, 80); } import { sketch } from "https://esm.town/v/saolsen/p5"; export default sketch(import.meta.url);

How it works

The sketch function returns an http handler that sets up a basic page with p5.js added. It then imports your module from the browser and wires up all the exports so p5.js can see them. All the code in your val will run in the browser (except for the default sketch export) so you can't call any Deno functions, environment variables, or other server side apis.

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)
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 18, 2024