Skip to content

Commit 15fe476

Browse files
authored
Merge pull request #358 from processing/fix/sketches
Add fixes for sketch thumbnails and layouts
2 parents e5f5417 + 7fe1b48 commit 15fe476

26 files changed

Lines changed: 163 additions & 23 deletions

docs/community_sketches.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Community Sketches
2+
3+
Community sketches [from our curation](https://openprocessing.org/curation/87649) are fetched dynamically from OpenProcessing's API. The thumbnails generated from OpenProcessing are 400x400, but we display a few featured sketches at a larger size on the homepage and community page, so we also manually provide thumbnails at a larger resolution.
4+
5+
## Adding a new sketch
6+
7+
1. Add it to the curation on OpenProcessing
8+
2. Take a screenshot of the sketch at a resolution of 1500x1000 as a `.png`
9+
3. Add it to `src/api/images`, naming it after the ID of the sketch (the numeric part of the OpenProcessing URL.)
10+
11+
For example, the sketch https://openprocessing.org/sketch/2211029 would be saved as `2211029.png` in the folder.

src/api/OpenProcessing.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type OpenProcessingSketchResponse = {
6464
userID: string;
6565
submittedOn: string;
6666
createdOn: string;
67+
mode: string;
6768
};
6869

6970
/**
@@ -81,6 +82,33 @@ export const getSketch = memoize(async (
8182
return payload as OpenProcessingSketchResponse;
8283
});
8384

85+
export const getSketchSize = memoize(async (id: string) => {
86+
const sketch = await getSketch(id)
87+
if (sketch.mode !== 'p5js') {
88+
return { width: undefined, height: undefined };
89+
}
90+
91+
const response = await fetch(`${openProcessingEndpoint}sketch/${id}/code`);
92+
const payload = await response.json();
93+
94+
for (const tab of payload) {
95+
if (!tab.code) continue;
96+
const match = /createCanvas\(\s*(\w+),\s*(\w+)\s*(?:,\s*(?:P2D|WEBGL)\s*)?\)/m.exec(tab.code);
97+
if (match) {
98+
if (match[1] === 'windowWidth' && match[2] === 'windowHeight') {
99+
return { width: undefined, height: undefined };
100+
}
101+
102+
const width = parseFloat(match[1]);
103+
const height = parseFloat(match[2]);
104+
if (width && height) {
105+
return { width, height };
106+
}
107+
}
108+
}
109+
return { width: undefined, height: undefined };
110+
});
111+
84112
export const makeSketchLinkUrl = (id: string) =>
85113
`https://openprocessing.org/sketch/${id}`;
86114

@@ -90,6 +118,17 @@ export const makeSketchEmbedUrl = (id: string) =>
90118
export const makeThumbnailUrl = (id: string) =>
91119
`https://openprocessing-usercontent.s3.amazonaws.com/thumbnails/visualThumbnail${id}@2x.jpg`;
92120

121+
export const getSketchThumbnailSource = async (id: string) => {
122+
const manualThumbs = import.meta.glob<ImageMetadata>('./images/*', { import: 'default' })
123+
const key = `./images/${id}.png`;
124+
if (manualThumbs[key]) {
125+
const img = await manualThumbs[key]()
126+
return img;
127+
}
128+
129+
return makeThumbnailUrl(id)
130+
}
131+
93132
/**
94133
* The size of the thumbnails generated by OpenProcessing in px
95134
*/
@@ -98,7 +137,7 @@ export const thumbnailDimensions = 400;
98137
export function isCurationResponse<C extends keyof AnyEntryMap>(
99138
item: OpenProcessingCurationResponse[number] | CollectionEntry<C>,
100139
): item is OpenProcessingCurationResponse[number] {
101-
return "visualID" in item;
140+
return "visualID" in (item as any);
102141
}
103142

104143
export const getRandomCurationSketches = memoize(async (num = 4) => {

src/api/images/1957050.png

3.13 MB
Loading

src/api/images/2036000.png

947 KB
Loading

src/api/images/2111906.png

679 KB
Loading

src/api/images/2174234.png

2.15 MB
Loading

src/api/images/2194525.png

104 KB
Loading

src/api/images/2211029.png

433 KB
Loading

src/api/images/2211175.png

54.6 KB
Loading

src/api/images/2211298.png

880 KB
Loading

0 commit comments

Comments
 (0)