Skip to content

Commit e694425

Browse files
authored
Merge pull request #812 from processing/feat/perf
Fully unmount sketches that go out of frame
2 parents ef23528 + 540b8cb commit e694425

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/components/CodeEmbed/frame.tsx

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useLayoutEffect, useEffect } from "preact/hooks";
1+
import { useRef, useLayoutEffect, useEffect, useState } from "preact/hooks";
22
import { cdnLibraryUrl } from "@/src/globals/globals";
33

44
interface CodeBundle {
@@ -86,6 +86,7 @@ export const CodeFrame = (props: CodeFrameProps) => {
8686
const p5ScriptTag = document.getElementById(
8787
"p5ScriptTag",
8888
) as HTMLScriptElement;
89+
const [mounted, setMounted] = useState(false);
8990

9091
// For performance, set the iframe to display:none when
9192
// not visible on the page. This will stop the browser
@@ -101,11 +102,7 @@ export const CodeFrame = (props: CodeFrameProps) => {
101102
(entries) => {
102103
entries.forEach((entry) => {
103104
if (!iframeRef.current) return;
104-
if (entry.isIntersecting) {
105-
iframeRef.current.style.removeProperty("display");
106-
} else {
107-
iframeRef.current.style.display = "none";
108-
}
105+
setMounted(entry.isIntersecting);
109106
});
110107
},
111108
{ rootMargin: "20px" },
@@ -118,6 +115,7 @@ export const CodeFrame = (props: CodeFrameProps) => {
118115
useEffect(() => {
119116
(async () => {
120117
if (!p5ScriptTag || !iframeRef.current) return;
118+
if (!mounted) return;
121119

122120
/*
123121
* Uses postMessage to receive the text content of p5.min.js, to be included
@@ -148,7 +146,7 @@ export const CodeFrame = (props: CodeFrameProps) => {
148146
return;
149147
}
150148
})();
151-
}, [props.jsCode]);
149+
}, [props.jsCode, mounted]);
152150

153151
return (
154152
<div
@@ -157,13 +155,13 @@ export const CodeFrame = (props: CodeFrameProps) => {
157155
>
158156
<iframe
159157
ref={iframeRef}
160-
srcDoc={wrapInMarkup({
158+
srcDoc={mounted ? wrapInMarkup({
161159
js: props.jsCode,
162160
css: props.cssCode,
163161
htmlBody: props.htmlBodyCode,
164162
base: props.base,
165163
scripts: props.scripts,
166-
})}
164+
}) : undefined}
167165
sandbox="allow-scripts allow-popups allow-modals allow-forms allow-same-origin"
168166
aria-label="Code Preview"
169167
title="Code Preview"

src/content/tutorials/en/intro-to-p5-strands.mdx

+12-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function lightingSetup() {
5959
6060
function setup() {
6161
createCanvas(200, 200, WEBGL);
62+
pixelDensity(1);
6263
}
6364
6465
function draw() {
@@ -110,6 +111,7 @@ function firstShaderCallback() {
110111
111112
async function setup() {
112113
createCanvas(200, 200, WEBGL);
114+
pixelDensity(1);
113115
// p5.strands are made using the modify function on a shader
114116
firstShader = baseColorShader().modify(firstShaderCallback);
115117
}
@@ -173,6 +175,7 @@ function instancingCallback() {
173175
174176
async function setup() {
175177
createCanvas(300, 300, WEBGL);
178+
pixelDensity(1);
176179
particleModel = buildGeometry(() => sphere(10, 10, 2));
177180
instancingShader = baseColorShader().modify(instancingCallback);
178181
instancingStrokeShader = baseStrokeShader().modify(instancingCallback);
@@ -227,6 +230,7 @@ getWorldInputs((inputs) => {
227230
228231
async function setup() {
229232
createCanvas(400, 400, WEBGL);
233+
pixelDensity(1);
230234
particleModel = buildGeometry(() => sphere(10, 4, 2));
231235
instancingShader = baseColorShader().modify(instancingCallback);
232236
instancingStrokeShader = baseStrokeShader().modify(instancingCallback);
@@ -301,6 +305,7 @@ getWorldInputs((inputs) => {
301305
302306
async function setup() {
303307
createCanvas(400, 400, WEBGL);
308+
pixelDensity(1);
304309
particleModel = buildGeometry(() => sphere(10, 4, 2));
305310
instancingShader = baseColorShader().modify(instancingCallback);
306311
instancingStrokeShader = baseStrokeShader().modify(instancingCallback);
@@ -410,6 +415,7 @@ function fresnelCallback() {
410415
411416
async function setup() {
412417
createCanvas(400, 400, WEBGL);
418+
pixelDensity(1);
413419
fresnelShader = baseColorShader().modify(fresnelCallback);
414420
}
415421
@@ -456,6 +462,7 @@ function pixelateCallback() {
456462
457463
async function setup() {
458464
createCanvas(200, 200);
465+
pixelDensity(1);
459466
pixelateShader = baseFilterShader().modify(pixelateCallback)
460467
}
461468
@@ -489,6 +496,7 @@ function pixelateCallback() {
489496
490497
async function setup() {
491498
createCanvas(200, 200, WEBGL);
499+
pixelDensity(1);
492500
pixelateShader = baseFilterShader().modify(pixelateCallback)
493501
}
494502
@@ -526,7 +534,7 @@ function draw() {
526534
image(originalImage, 0, 0);
527535

528536
// changing this value affects the spread of the bloom
529-
filter(BLUR, 20);
537+
filter(BLUR, 15);
530538
}
531539
```
532540

@@ -570,6 +578,7 @@ function bloomCallback() {
570578
571579
async function setup() {
572580
createCanvas(200, 200, WEBGL);
581+
pixelDensity(1);
573582
originalImage = createFramebuffer();
574583
bloomShader = baseFilterShader().modify(bloomCallback);
575584
}
@@ -715,6 +724,7 @@ function bloomShaderCallback() {
715724
716725
async function setup(){
717726
createCanvas(800, 600, WEBGL);
727+
pixelDensity(1);
718728
stars = buildGeometry(() => sphere(8, 4, 2))
719729
originalImage = createFramebuffer();
720730
@@ -752,7 +762,7 @@ function draw(){
752762
imageMode(CENTER)
753763
image(originalImage, 0, 0)
754764
755-
filter(BLUR, 25)
765+
filter(BLUR, 15)
756766
filter(bloomShader);
757767
}
758768
`} />

0 commit comments

Comments
 (0)