Skip to content
Open
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a5f85a7
Fix TypeScript typing for filterColor shader hook
Kathrina-dev Mar 15, 2026
ea3b6b4
Removed formatted code
Kathrina-dev Mar 16, 2026
9de7f94
fix: typescript generation script for typedef constants
Kathrina-dev Mar 16, 2026
fdf4b55
added condition for typedefs in p5Constants
Kathrina-dev Mar 16, 2026
ce9ef51
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Mar 16, 2026
36f70e2
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Mar 17, 2026
c42ef81
fixed typescript JSDoc generator to handle unions
Kathrina-dev Mar 17, 2026
c809894
removed auto-formatting
Kathrina-dev Mar 17, 2026
51a03f0
added useful comments
Kathrina-dev Mar 17, 2026
ca4c1a4
added useful comments
Kathrina-dev Mar 17, 2026
94ae3d4
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Mar 20, 2026
27b0d88
revert back to original structure
Kathrina-dev Mar 20, 2026
f29c5b8
added new function to handle typedef properties
Kathrina-dev Mar 20, 2026
825d0cb
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Mar 23, 2026
bc62e8c
added condition to allow filterColorHook to pass through p5Constants
Kathrina-dev Mar 23, 2026
6ce04fc
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Mar 26, 2026
4e64619
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Apr 3, 2026
17281cf
Merge branch 'dev-2.0' into fix-filterColor-object-type
Kathrina-dev Apr 7, 2026
1219e71
added new typing for objects in p5.strands.js
Kathrina-dev Apr 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions utils/patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import fs from 'fs';
export function applyPatches() {
const cache = {};
const patched = {};

const replace = (path, src, dest) => {
if (Array.isArray(path)) {
path.forEach(path => replace(path, src, dest));
return;
}
try {
if (!path.startsWith("types/"))
path = "types/" + path;
if (!path.startsWith('types/'))
path = 'types/' + path;

const before = patched[path] ??
(cache[path] ??= fs.readFileSync("./" + path, { encoding: 'utf-8' }));
(cache[path] ??= fs.readFileSync('./' + path, { encoding: 'utf-8' }));
const after = before.replaceAll(src, dest);

if (after !== before)
Expand All @@ -28,8 +28,8 @@ export function applyPatches() {

// TODO: Handle this better in the docs instead of patching
replace(
"p5.d.ts",
"constructor(detailX?: number, detailY?: number, callback?: Function);",
'p5.d.ts',
'constructor(detailX?: number, detailY?: number, callback?: Function);',
`constructor(
detailX?: number,
detailY?: number,
Expand All @@ -39,9 +39,9 @@ export function applyPatches() {
// https://github.com/p5-types/p5.ts/issues/31
// #todo: add readonly to appropriate array params, either here or in doc comments
replace(
["p5.d.ts", "global.d.ts"],
"random(choices: any[]): any;",
"random<T>(choices: readonly T[]): T;"
['p5.d.ts', 'global.d.ts'],
'random(choices: any[]): any;',
'random<T>(choices: readonly T[]): T;'
);

replace(
Expand All @@ -53,7 +53,7 @@ export function applyPatches() {
replace(
'p5.d.ts',
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): object[][];',
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): { x: number; y: number; alpha: number }[][];',
'textToContours(str: string, x: number, y: number, options?: { sampleFactor?: number; simplifyThreshold?: number }): { x: number; y: number; alpha: number }[][];'
);

replace(
Expand All @@ -77,7 +77,7 @@ export function applyPatches() {
'class __Graphics extends p5.Element {',
`class __Graphics extends p5.Element {
elt: HTMLCanvasElement;
`,
`
);

// Type .elt more specifically for audio and video elements
Expand All @@ -86,24 +86,24 @@ export function applyPatches() {
`class MediaElement extends Element {
elt: HTMLAudioElement | HTMLVideoElement;`,
`class MediaElement<T extends HTMLElement = HTMLAudioElement | HTMLVideoElement> extends Element {
elt: T;`,
elt: T;`
);
replace(
['p5.d.ts', 'global.d.ts'],
/createAudio\(src\?: string \| string\[\], callback\?: Function\): ([pP]5)\.MediaElement;/g,
'createAudio(src?: string | string[], callback?: (video: $1.MediaElement<HTMLAudioElement>) => any): $1.MediaElement<HTMLAudioElement>;',
'createAudio(src?: string | string[], callback?: (video: $1.MediaElement<HTMLAudioElement>) => any): $1.MediaElement<HTMLAudioElement>;'
);
replace(
['p5.d.ts', 'global.d.ts'],
/createVideo\(src\?: string \| string\[\], callback\?: Function\): ([pP]5)\.MediaElement;/g,
'createVideo(src?: string | string[], callback?: (video: $1.MediaElement<HTMLVideoElement>) => any): $1.MediaElement<HTMLVideoElement>;',
'createVideo(src?: string | string[], callback?: (video: $1.MediaElement<HTMLVideoElement>) => any): $1.MediaElement<HTMLVideoElement>;'
);

// More callback types
replace(
['p5.d.ts', 'global.d.ts'],
/createFileInput\(callback: Function, multiple\?: boolean\): ([pP]5)\.Element;/g,
'createFileInput(callback: (input: $1.File) => any, multiple?: boolean): $1.Element;',
'createFileInput(callback: (input: $1.File) => any, multiple?: boolean): $1.Element;'
);
replace(
['p5.d.ts', 'global.d.ts'],
Expand All @@ -120,7 +120,7 @@ export function applyPatches() {
replace(
'p5.d.ts',
'fontBounds(str: string, x: number, y: number, width?: number, height?: number): object;',
'fontBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };',
'fontBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };'
);
replace(
'p5.d.ts',
Expand All @@ -130,7 +130,7 @@ export function applyPatches() {
replace(
'p5.d.ts',
'textBounds(str: string, x: number, y: number, width?: number, height?: number): object;',
'textBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };',
'textBounds(str: string, x: number, y: number, width?: number, height?: number): { x: number; y: number; w: number; h: number };'
);

// Document Typr
Expand Down Expand Up @@ -166,10 +166,25 @@ export function applyPatches() {
`
);

// Fix filterColor hook typing
replace(
['p5.d.ts'],
'declare const filterColor: object;',
`declare const filterColor: {
texCoord: any;
canvasSize: any;
texelSize: any;
canvasContent: any;
begin(): void;
end(): void;
set(color: any): void;
};`
);

for (const [path, data] of Object.entries(patched)) {
try {
console.log(`Patched ${path}`);
fs.writeFileSync("./" + path, data);
fs.writeFileSync('./' + path, data);
} catch (err) {
console.error(err);
}
Expand Down