Skip to content

Commit

Permalink
Convert the first WebGL tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Mar 2, 2024
1 parent 2fc0abc commit d9426cb
Show file tree
Hide file tree
Showing 8 changed files with 799 additions and 7 deletions.
Binary file added public/images/webgl/2d3d_coordinates.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/components/Callout/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* A component that points out something to the reader, such as a call to
* try out an idea oneself.
* props: { title?: string; children: any }
*/
export const Callout = (props) => (
<div className="callout">
<h5>{props.title || "Try this!"}</h5>
{props.children}
</div>
);
15 changes: 9 additions & 6 deletions src/components/CodeEmbed/frame.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const wrapJsInMarkup = (jsCode) => `<!DOCTYPE html>
const wrapJsInMarkup = (jsCode, options) => `<!DOCTYPE html>
<meta charset="utf8" />
<body></body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.8.0/p5.min.js"></script>
<script id="code" type="module">${jsCode}</script>
${options.cssCode ? (`<style type='text/css'>${options.cssCode}</style>`) : ""}
<body>${options.bodyCode || ""}</body>
<script id="code" type="text/javascript">${jsCode}</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.1/p5.min.js"></script>
`;

/*
* Component that uses an iframe to run p5 code.
* props: { code: string }
* props: { code: string; height?: number | string; width?: number | string }
*/
export const CodeFrame = (props) => (
<iframe
srcDoc={wrapJsInMarkup(props.code)}
srcDoc={wrapJsInMarkup(props.code, props)}
sandbox="allow-scripts allow-popups allow-modals allow-forms"
aria-label="Code Preview"
title="Code Preview"
height={props.height}
width={props.width}
/>
);
9 changes: 8 additions & 1 deletion src/components/CodeEmbed/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "preact/hooks";
import { useState, useEffect } from "preact/hooks";
import CodeMirror from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";

Expand All @@ -14,9 +14,16 @@ import { CodeFrame } from "./frame";
* }
*/
export const CodeEmbedCodeMirror = (props) => {
const [rendered, setRendered] = useState(false);
const [codeString, setCodeString] = useState(props.initialValue ?? "");
const [previewCodeString, setPreviewCodeString] = useState(codeString);

useEffect(() => {
setRendered(true)
}, []);

if (!rendered) return <div className="code-placeholder" />;

return (
<>
<CodeMirror
Expand Down
5 changes: 5 additions & 0 deletions src/components/EditableSketch/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CodeEmbedCodeMirror from "../CodeEmbed";

export const EditableSketch = (props) => (
<CodeEmbedCodeMirror initialValue={props.code.trim()} previewable editable />
);
21 changes: 21 additions & 0 deletions src/components/SketchEmbed/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CodeFrame } from "../CodeEmbed/frame";

/*
* A component that displays a full-width sketch without showing its code.
* props: {
* code: string;
* cssCode?: string;
* bodyCode?: string;
* width?: number | string;
* height?: number | string;
* }
*/
export const SketchEmbed = (props) => (
<CodeFrame
code={props.code}
cssCode={props.cssCode}
bodyCode={props.bodyCode}
height={props.height || 400}
width="100%"
/>
);

0 comments on commit d9426cb

Please sign in to comment.