Skip to content

Commit 8ebcefe

Browse files
authored
Merge pull request #81 from cmpadden/feat/remove-p5js-dependency
Remove dependency on p5.js (moved to cmpadden/p5)
2 parents 8f8b3a6 + 2aaa6e8 commit 8ebcefe

File tree

16 files changed

+212
-948
lines changed

16 files changed

+212
-948
lines changed

components/ModelPreview.global.vue

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<template>
2-
<div ref="container" class="h-64" />
2+
<div class="relative h-64 w-full overflow-hidden border border-white/40">
3+
<iframe
4+
:src="iframeSrc"
5+
class="h-full w-full"
6+
loading="lazy"
7+
referrerpolicy="no-referrer"
8+
title="Embedded p5 model preview"
9+
/>
10+
</div>
311
</template>
412

513
<script setup lang="ts">
6-
import { onBeforeUnmount, onMounted, ref } from "vue";
14+
import { computed } from "vue";
15+
const PLAYGROUND_ORIGIN = "https://cmpadden.github.io/p5";
716
817
const props = withDefaults(
918
defineProps<{
@@ -15,75 +24,29 @@ const props = withDefaults(
1524
},
1625
);
1726
18-
const container = ref<HTMLElement | null>(null);
19-
let p5Instance: any = null;
20-
21-
onMounted(async () => {
22-
if (!container.value) {
23-
return;
24-
}
25-
26-
const { $p5 } = useNuxtApp();
27-
const p5 = await $p5();
28-
const host = container.value;
29-
30-
const sketch = (s: any) => {
31-
let model;
32-
let angle = 0;
33-
let rotate = true;
34-
35-
const getSize = () => {
36-
return {
37-
width: host.clientWidth || 0,
38-
height: host.clientHeight || 0,
39-
};
40-
};
41-
42-
s.preload = () => {
43-
model = s.loadModel(props.path, true);
44-
};
45-
46-
s.setup = () => {
47-
const { width, height } = getSize();
48-
s.createCanvas(width, height, s.WEBGL);
49-
};
50-
51-
s.windowResized = () => {
52-
const { width, height } = getSize();
53-
s.resizeCanvas(width, height);
54-
};
55-
56-
s.mouseClicked = () => {
57-
rotate = false; // disable object rotation on click
58-
};
59-
60-
s.draw = () => {
61-
const bg = Number(props.backgroundColor ?? 100);
62-
63-
s.background(bg);
64-
s.lights();
65-
66-
if (rotate) {
67-
angle = s.frameCount * 0.005;
68-
}
69-
70-
s.rotateX(angle);
71-
s.rotateY(angle);
72-
s.orbitControl();
73-
74-
if (model) {
75-
s.model(model);
76-
}
77-
};
27+
const sanitizePath = (path: string) => {
28+
const ensureAbsolute = (p: string) => {
29+
const base = PLAYGROUND_ORIGIN.replace(/\/$/, "");
30+
const suffix = p.startsWith("/") ? p : `/${p}`;
31+
return `${base}${suffix}`;
7832
};
7933
80-
p5Instance = new p5(sketch, host);
81-
});
82-
83-
onBeforeUnmount(() => {
84-
if (p5Instance) {
85-
p5Instance.remove?.();
86-
p5Instance = null;
34+
if (!path) {
35+
return ensureAbsolute("/models/mac_mini_macbook_stand.stl");
36+
}
37+
if (/^https?:/i.test(path)) {
38+
return path;
8739
}
40+
return ensureAbsolute(path);
41+
};
42+
43+
const iframeSrc = computed(() => {
44+
const modelPath = sanitizePath(props.path);
45+
const search = new URLSearchParams({
46+
path: modelPath,
47+
bg: String(props.backgroundColor ?? 100),
48+
});
49+
const viewerBase = `${PLAYGROUND_ORIGIN.replace(/\/$/, "")}/experiments/model-viewer.html`;
50+
return `${viewerBase}?${search.toString()}`;
8851
});
8952
</script>

components/playground/Wave.vue

Lines changed: 0 additions & 104 deletions
This file was deleted.

content/blog/nuxt-p5js-3d-models.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ categories: ["examples"]
77

88
Three-dimensional models, whether they be of the format `.obj` or `.stl`, can be easily rendered in your Nuxt project by using the [p5js.org](https://p5js.org/) library.
99

10+
> Update 2025: the interactive demos on this site now load the standalone [p5 playground](https://cmpadden.github.io/p5/) within an iframe. The component API described below still works when embedding the GitHub Pages build of that project.
11+
1012
<!--more-->
1113

1214
I've been using p5 for a while now to tinker and explore generative art, some of which can be found in the [/playground](/playground) section of this website. To use it with Nuxt, a [client-side plugin](https://github.com/cmpadden/cmpadden.github.io/blob/70ba674acdbe80c4a41b510a9b54edcddb19f489/plugins/p5.client.js) was created like so:

layouts/light.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<main>
33
<Header />
44
<div class="container mx-auto h-screen bg-background font-display">
5-
<div class="flex flex-col border-2 bg-white">
5+
<div class="flex flex-col border-2">
66
<slot />
77
</div>
88
</div>

nuxt.config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,21 @@ export default defineNuxtConfig({
5858
hooks: {
5959
// Generate HTML redirect files for `/articles/**` to `/blog/**`
6060
async 'close'() {
61-
const { readdirSync, statSync } = await import('fs')
61+
const { readdirSync, statSync, existsSync } = await import('fs')
6262
const distBlogPath = join(process.cwd(), 'dist', 'blog')
6363
const distArticlesPath = join(process.cwd(), 'dist', 'articles')
6464

6565
try {
66-
if (!statSync(distBlogPath).isDirectory()) {
66+
if (!existsSync(distBlogPath)) {
6767
console.log('Blog directory not found, skipping redirect generation')
6868
return
6969
}
7070

71+
if (!statSync(distBlogPath).isDirectory()) {
72+
console.log('Blog directory path is not a directory, skipping redirect generation')
73+
return
74+
}
75+
7176
const blogDirs = readdirSync(distBlogPath, { withFileTypes: true })
7277
.filter(dirent => dirent.isDirectory())
7378
.map(dirent => dirent.name)

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
},
2525
"dependencies": {
2626
"chords.ts": "^0.1.0",
27-
"p5": "^1.11.7",
2827
"tailwindcss": "^3.4.17"
2928
},
3029
"packageManager": "pnpm@10.8.0+sha512.0e82714d1b5b43c74610193cb20734897c1d00de89d0e18420aebc5977fa13d780a9cb05734624e81ebd81cc876cd464794850641c48b9544326b5622ca29971"

pages/playground/conway.vue

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ definePageMeta({ layout: "light" });
33
</script>
44

55
<template>
6-
<main class="flex h-[1024px] justify-center border-2 border-gray-500">
7-
<iframe
8-
src="https://cmpadden.github.io/conway/"
9-
width="100%"
10-
height="100%"
11-
></iframe>
12-
</main>
6+
<main class="bg-white flex h-[1024px] justify-center border-2 border-gray-500">
7+
<iframe src="https://cmpadden.github.io/conway/" width="100%" height="100%"></iframe>
8+
</main>
139
</template>

0 commit comments

Comments
 (0)