Skip to content

Commit 5d6b347

Browse files
authored
Merge branch 'main' into p5.loadingAnimation.js
2 parents a66ba7e + 7333293 commit 5d6b347

24 files changed

+107
-59
lines changed

src/api/OpenProcessing.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export type OpenProcessingCurationResponse = Array<{
2323
title: string;
2424
/** Description of sketch */
2525
description: string;
26+
instructions: string;
27+
mode: string;
28+
createdOn: string;
2629
userID: string;
2730
submittedOn: string;
2831
/** Author's name */
@@ -36,16 +39,19 @@ export type OpenProcessingCurationResponse = Array<{
3639
* @param limit max number of sketches to return
3740
* @returns sketches
3841
*/
39-
export const getCurationSketches = async (
42+
export const getCurationSketches = memoize(async (
4043
limit?: number,
4144
): Promise<OpenProcessingCurationResponse> => {
4245
const limitParam = limit ? `limit=${limit}` : "";
4346
const response = await fetch(
4447
`${openProcessingEndpoint}curation/${curationId}/sketches?${limitParam}`,
4548
);
49+
if(!response.ok){ //log error instead of throwing error to not cache result in memoize
50+
console.error('getCurationSketches', response.status, response.statusText)
51+
}
4652
const payload = await response.json();
4753
return payload as OpenProcessingCurationResponse;
48-
};
54+
});
4955

5056
/**
5157
* API Response from a call to the Sketch endpoint
@@ -69,26 +75,50 @@ export type OpenProcessingSketchResponse = {
6975

7076
/**
7177
* Get info about a specific sketch from the OpenProcessing API
78+
* First checks if the sketch is in the memoized curated sketches and returns the data if so,
79+
* Otherwise calls OpenProcessing API for this specific sketch
7280
*
7381
* https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#7cd344f6-6e87-426a-969b-2b4a79701dd1
7482
* @param id
7583
* @returns
7684
*/
77-
export const getSketch = memoize(async (
78-
id: string,
79-
): Promise<OpenProcessingSketchResponse> => {
85+
export const getSketch = memoize(
86+
async (id: string): Promise<OpenProcessingSketchResponse> => {
87+
// check for memoized sketch in curation sketches
88+
const curationSketches = await getCurationSketches();
89+
const memoizedSketch = curationSketches.find((el) => el.visualID === id);
90+
if (memoizedSketch) {
91+
return {
92+
...memoizedSketch,
93+
license: "",
94+
} as OpenProcessingSketchResponse;
95+
}
96+
97+
// check for sketch data in Open Processing API
8098
const response = await fetch(`${openProcessingEndpoint}sketch/${id}`);
99+
if (!response.ok) {
100+
//log error instead of throwing error to not cache result in memoize
101+
console.error("getSketch", id, response.status, response.statusText);
102+
}
81103
const payload = await response.json();
82104
return payload as OpenProcessingSketchResponse;
83105
});
84106

107+
/**
108+
* Note: this currently calls `/api/sketch/:id/code`
109+
* But only uses the width and height properties from this call
110+
* Width and height should instead be added to properties for `/api/sketch/:id` or `api/curation/:curationId/sketches` instead
111+
*/
85112
export const getSketchSize = memoize(async (id: string) => {
86113
const sketch = await getSketch(id)
87114
if (sketch.mode !== 'p5js') {
88115
return { width: undefined, height: undefined };
89116
}
90117

91118
const response = await fetch(`${openProcessingEndpoint}sketch/${id}/code`);
119+
if(!response.ok){ //log error instead of throwing error to not cache result in memoize
120+
console.error('getSketchSize', id, response.status, response.statusText)
121+
}
92122
const payload = await response.json();
93123

94124
for (const tab of payload) {

src/content/tutorials/en/get-started.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ Using the [p5.js Web Editor](https://editor.p5js.org/):
5353

5454
- Log in to the [p5.js Web Editor](https://editor.p5js.org/).
5555
- Name your project “Interactive Landscape” by clicking the *pencil icon* and typing “Interactive Landscape” into the text box that appears.
56+
57+
![A user on the p5.js Web Editor names a new project “Interactive Landscape”.](../images/introduction/p5_editor_interactive_landscape_1.png)
58+
5659
- Click on *File* and select *Save*.
60+
61+
![A user on the p5.js Web Editor saves their new project "Interactive Landscape".](../images/introduction/p5_editor_interactive_landscape_2.png)
62+
5763
- Confirm your project is saved by navigating to your gallery of saved sketches:
5864
- Click on *File* and select *Open*.
5965
- Your most recent sketches will appear at the top of the list of projects saved on your account.
6066

61-
![A user on the p5.js Web Editor names a new project Interactive Landscape”, saves it, finds it in their gallery of saved sketches, and clicks the project's name to open it.](../images/introduction/rename-sketch.gif)
67+
![A user on the p5.js Web Editor finds their project "Interactive Landscape" in their gallery of saved sketches.](../images/introduction/p5_editor_interactive_landscape_3.png)
6268

6369
#### Default Code
6470

src/content/tutorials/en/setting-up-your-environment.mdx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ If you are new to p5.js, start with the [p5.js Web Editor](https://editor.p5js.o
6161
- Open a desktop browser on your computer and go to [https://editor.p5js.org/](https://editor.p5js.org/).
6262
- Click the [“Sign up”](https://editor.p5js.org/signup) link on the top-right of the web page.
6363

64-
![An arrow pointing to the “Sign Up” link in the top right corner of the p5.js Web Editor.](../images/introduction/web-editor-signup.png)
64+
![An arrow pointing to the “Sign Up” link in the top right corner of the p5.js Editor.](../images/introduction/p5_editor_sign_up.png)
6565

6666

6767
### Step 2: Create an account with [p5.js Web Editor](https://editor.p5js.org/)
@@ -73,7 +73,7 @@ If you are new to p5.js, start with the [p5.js Web Editor](https://editor.p5js.o
7373
- Create and confirm a password. 
7474
- Click the “Sign Up” button.
7575

76-
![A user clicks the “Sign Up” button in the p5.js Web Editor, enters a new username, email address, and password.](../images/introduction/editor-signup.gif)
76+
![An arrow pointing to the highlighted User Name, Emaill, Password, and Confirm Password fields on the Sign Up page.](../images/introduction/p5_editor_sign_up_page.png)
7777

7878
- Using a Google account
7979
- Click on the button on the bottom of the page that says “Login with Google.” 
@@ -98,21 +98,29 @@ The [p5.js Web Editor](https://editor.p5js.org/) is an online environment where
9898

9999
The following diagram labels and describes each element of the editor’s interface:
100100

101-
![A labeled diagram of the p5.js Web Editor’s user interface.](../images/introduction/web-editor-diagram.png)
101+
![A labeled diagram of the p5.js Web Editor’s user interface.](../images/introduction/p5_editor_interface_breakdown.png)
102+
103+
{/* ![Play and Stop buttons.](../images/introduction/p5_editor_play_start.png)
104+
![Settings button with a gear icon in the center.](../images/introduction/p5_editor_settings.png) */}
102105

103106

104107
### Step 4: Name, save, and run your first sketch
105108

106109
- Name your projects by clicking on the pencil icon above the text editor and typing in a name for your project.
110+
111+
![An arrow points to a pencil icon to rename a sketch.](../images/introduction/p5_editor_naming_a_sketch.png)
112+
107113
- Save projects by clicking on *File* in the top toolbar, and selecting *Save*
108114
- Make sure you are logged in to your account or you will not be able to save the sketch.
109115
- Saving projects frequently helps to ensure that code isn’t lost if something happens to your computer, browser, or internet connection while you are coding.
110116

111-
![A user on the p5.js Web Editor names a new project “My First Sketch” and saves it. A notification box then appears for a moment with the text “Sketch saved.”](../images/introduction/renameSave.gif)
117+
![A user on the p5.js Web Editor names a new project and saves it.”](../images/introduction/p5_editor_saving_a_sketch.png)
118+
119+
![An arrow pointing to a notification box with the text “Sketch saved.”](../images/introduction/p5_editor_saved_sketch_notification.png)
112120

113121
To view the output of your code, click the *Play* button in the top left corner: 
114122

115-
![A blank canvas appears in the preview window of the p5.js Web Editor when a user clicks on the “Play” button in the top right corner.](../images/introduction/play.gif)
123+
![A blank canvas appears in the preview window of the p5.js Web Editor when a user clicks on the “Play” button in the top right corner.](../images/introduction/p5_editor_running_a_sketch.png)
116124

117125
A *p5.js sketch* is a text file with code written in the *JavaScript* programming language. *JavaScript* is a programming language used to make web pages interactive. p5.js is a library written in *JavaScript* – which is why it ends in “*.js*” for *JavaScript*. With p5.js, you can create programs that are colorful and animated, with features that users can interact with! To learn more about some of the things you can do with p5.js, watch the [p5.js Welcome Video!](https://hello.p5js.org/) To learn more about JavaScript, you can visit [this resource.](https://developer.mozilla.org/en-US/docs/Web/JavaScript)
118126

Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/layouts/SketchLayout.astro

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,76 +21,83 @@ interface Props {
2121
}
2222
2323
const { sketchId, authorName } = Astro.props;
24-
const sketchInfo = await getSketch(sketchId);
24+
const { title, createdOn, instructions } = await getSketch(sketchId);
2525
2626
const currentLocale = getCurrentLocale(Astro.url.pathname);
2727
const t = await getUiTranslator(currentLocale);
28-
const dateString = new Date(sketchInfo.createdOn).toLocaleDateString(
29-
currentLocale,
30-
{
31-
year: "numeric",
32-
month: "long",
33-
day: "numeric",
34-
}
35-
);
28+
const dateString = new Date(createdOn).toLocaleDateString(currentLocale, {
29+
year: "numeric",
30+
month: "long",
31+
day: "numeric",
32+
});
3633
3734
setJumpToState(null);
3835
const moreSketches = await getRandomCurationSketches(4);
39-
const featuredImageURL = makeThumbnailUrl(sketchInfo.visualID);
36+
const featuredImageURL = makeThumbnailUrl(sketchId);
4037
41-
let { width, height } = await getSketchSize(sketchInfo.visualID);
38+
let { width, height } = await getSketchSize(sketchId);
4239
let heightOverWidth = 1 / 1.5;
4340
if (width && height) {
4441
// Account for OP header bar
4542
height += 50;
4643
heightOverWidth = height / width;
4744
}
4845
49-
const iframeTitle = `OpenProcessing Sketch: ${sketchInfo.title} by ${authorName}`;
46+
const iframeTitle = `OpenProcessing Sketch: ${title} by ${authorName}`;
5047
---
5148

5249
<Head
53-
title={sketchInfo.title}
50+
title={title}
5451
locale={currentLocale}
5552
featuredImageSrc={featuredImageURL}
56-
description={sketchInfo.instructions}
53+
description={instructions}
5754
/>
5855

5956
<BaseLayout
60-
title={sketchInfo.title}
57+
title={title}
6158
titleAuthor={authorName}
6259
subtitle={dateString}
6360
variant="item"
6461
topic={"community"}
6562
>
6663
<div class="max-w-[770px]">
67-
<div style={{
68-
position: 'relative',
69-
width: '100%',
70-
paddingBottom: `${(heightOverWidth * 100).toFixed(4)}%`,
71-
}}>
72-
{width ? (
73-
<ScalingIframe
74-
client:load
75-
src={makeSketchEmbedUrl(sketchInfo.visualID)}
76-
width={width}
77-
height={height}
78-
title={iframeTitle}
79-
/>
80-
) : (
81-
<iframe
82-
src={makeSketchEmbedUrl(sketchInfo.visualID)}
83-
width="100%"
84-
height="100%"
85-
style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}
86-
title={iframeTitle}
87-
/>
88-
)}
64+
<div
65+
style={{
66+
position: "relative",
67+
width: "100%",
68+
paddingBottom: `${(heightOverWidth * 100).toFixed(4)}%`,
69+
}}
70+
>
71+
{
72+
width ? (
73+
<ScalingIframe
74+
client:load
75+
src={makeSketchEmbedUrl(sketchId)}
76+
width={width}
77+
height={height}
78+
title={iframeTitle}
79+
/>
80+
) : (
81+
<iframe
82+
src={makeSketchEmbedUrl(sketchId)}
83+
width="100%"
84+
height="100%"
85+
style={{
86+
position: "absolute",
87+
top: 0,
88+
left: 0,
89+
right: 0,
90+
bottom: 0,
91+
}}
92+
title={iframeTitle}
93+
/>
94+
)
95+
}
8996
</div>
9097
<div class="py-md grid gap-y-sm md:gap-y-md">
9198
<LinkButton
9299
variant="code"
93-
url={`${makeSketchLinkUrl(sketchInfo.visualID)}/#code`}
100+
url={`${makeSketchLinkUrl(sketchId)}/#code`}
94101
class="min-w-[184px] lg:min-w-[220px]">{t("Show Code")}</LinkButton
95102
>
96103
<LinkButton
@@ -100,16 +107,11 @@ const iframeTitle = `OpenProcessing Sketch: ${sketchInfo.title} by ${authorName}
100107
>
101108
</div>
102109

103-
{
104-
sketchInfo.instructions && (
105-
<p class="text-md my-sm md:my-lg">{sketchInfo.instructions}</p>
106-
)
107-
}
110+
{instructions && <p class="text-md my-sm md:my-lg">{instructions}</p>}
108111

109112
<p class="text-xs md:text-base mb-3xl">
110-
This <a
111-
class="text-type-magenta"
112-
href={makeSketchLinkUrl(sketchInfo.visualID)}>sketch</a
113+
This <a class="text-type-magenta" href={makeSketchLinkUrl(sketchId)}
114+
>sketch</a
113115
> is ported from the <a
114116
class="text-type-magenta"
115117
href="https://openprocessing.org">OpenProcessing</a

src/pages/[locale]/community.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import CommunityLayout from "@layouts/CommunityLayout.astro";
55
import { getCollectionInLocaleWithFallbacks } from "@pages/_utils";
66
77
export const getStaticPaths = async () => {
8-
const sketches = await getCurationSketches(10);
8+
const allSketches = await getCurationSketches();
9+
const sketches = allSketches.slice(0, 10);
910
return await Promise.all(
1011
nonDefaultSupportedLocales.map(async (locale) => {
1112
const libraries = await getCollectionInLocaleWithFallbacks(

src/pages/community.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { getCurationSketches } from "../api/OpenProcessing";
33
import CommunityLayout from "../layouts/CommunityLayout.astro";
44
import { getCollectionInDefaultLocale } from "./_utils";
55
6-
const sketches = await getCurationSketches(10);
6+
const allSketches = await getCurationSketches(10);
7+
const sketches = allSketches.slice(0, 10);
78
const libraries = await getCollectionInDefaultLocale("libraries");
89
const pastEvents = await getCollectionInDefaultLocale("events");
910
---

0 commit comments

Comments
 (0)