@@ -44,7 +44,7 @@ Then just open `http://localhost:3001` in a browser.
4444
4545### Javascript
4646
47- ``` js
47+ ``` javascript
4848import React from " react" ;
4949import { ReactP5Wrapper } from " react-p5-wrapper" ;
5050
@@ -68,9 +68,9 @@ export function App() {
6868}
6969```
7070
71- ### Typescript
71+ ### TypeScript
7272
73- Typescript sketches can be declared in two different ways, below you will find
73+ TypeScript sketches can be declared in two different ways, below you will find
7474two ways to declare a sketch, both examples do the exact same thing.
7575
7676In short though, the ` ReactP5Wrapper ` component requires you to pass a ` sketch `
@@ -80,7 +80,7 @@ of type `P5Instance`, you are good to go!
8080
8181#### Option 1: Declaring a sketch using the ` P5Instance ` type
8282
83- ``` ts
83+ ``` typescript
8484import React from " react" ;
8585import { ReactP5Wrapper , P5Instance } from " react-p5-wrapper" ;
8686
@@ -116,7 +116,7 @@ that the `p5` argument passed to the sketch function is auto-typed as a
116116> sketches and there is nothing wrong with using the ` P5Instance ` manually in a
117117> regular ` function ` declaration.
118118
119- ``` ts
119+ ``` typescript
120120import React from " react" ;
121121import { ReactP5Wrapper , Sketch } from " react-p5-wrapper" ;
122122
@@ -140,9 +140,135 @@ export function App() {
140140}
141141```
142142
143+ #### TypeScript Generics
144+
145+ We also support the use of Generics to add type definitions for your props. If
146+ used, the props will be properly typed when the props are passed to the
147+ ` updateWithProps ` method.
148+
149+ To utilise generics you can use one of two methods. In both of the exampled
150+ below, we create a custom internal type called ` MySketchProps ` which is a union
151+ type of ` SketchProps ` and a custom type which has a ` rotation ` key applied to
152+ it.
153+
154+ > Sidenote:
155+ >
156+ > We could also write the ` MySketchProps ` type as an interface to do exactly the
157+ > same thing if that is to your personal preference:
158+ >
159+ > ``` typescript
160+ > interface MySketchProps extends SketchProps {
161+ > rotation: number ;
162+ > }
163+ > ` ` `
164+
165+ This means, in these examples, that when the ` rotation ` prop that is provided as
166+ part of the ` props ` passed to the ` updateWithProps ` function, it will be
167+ correctly typed as a ` number ` .
168+
169+ ##### Usage with the ` P5Instance ` type
170+
171+ ` ` ` typescript
172+ import React , { useState , useEffect } from " react" ;
173+ import { ReactP5Wrapper , P5Instance , SketchProps } from " react-p5-wrapper" ;
174+
175+ type MySketchProps = SketchProps & {
176+ rotation: number ;
177+ };
178+
179+ function sketch<MySketchProps >(p5 : P5Instance <MySketchProps >) {
180+ let rotation = 0 ;
181+
182+ p5 .setup = () => p5 .createCanvas (600 , 400 , p5 .WEBGL );
183+
184+ p5 .updateWithProps = props => {
185+ if (props .rotation ) {
186+ rotation = (props .rotation * Math .PI ) / 180 ;
187+ }
188+ };
189+
190+ p5 .draw = () => {
191+ p5 .background (100 );
192+ p5 .normalMaterial ();
193+ p5 .noStroke ();
194+ p5 .push ();
195+ p5 .rotateY (rotation );
196+ p5 .box (100 );
197+ p5 .pop ();
198+ };
199+ }
200+
201+ export function App() {
202+ const [rotation, setRotation] = useState (0 );
203+
204+ useEffect (() => {
205+ const interval = setInterval (
206+ () => setRotation (rotation => rotation + 100 ),
207+ 100
208+ );
209+
210+ return () => {
211+ clearInterval (interval );
212+ };
213+ }, []);
214+
215+ return <ReactP5Wrapper sketch ={sketch} rotation ={rotation} />;
216+ }
217+ ```
218+
219+ ##### Usage with the ` Sketch ` type
220+
221+ ``` typescript
222+ import React , { useState , useEffect } from " react" ;
223+ import { ReactP5Wrapper , Sketch , SketchProps } from " react-p5-wrapper" ;
224+
225+ type MySketchProps = SketchProps & {
226+ rotation: number ;
227+ };
228+
229+ const sketch: Sketch <MySketchProps > = p5 => {
230+ let rotation = 0 ;
231+
232+ p5 .setup = () => p5 .createCanvas (600 , 400 , p5 .WEBGL );
233+
234+ p5 .updateWithProps = props => {
235+ if (props .rotation ) {
236+ rotation = (props .rotation * Math .PI ) / 180 ;
237+ }
238+ };
239+
240+ p5 .draw = () => {
241+ p5 .background (100 );
242+ p5 .normalMaterial ();
243+ p5 .noStroke ();
244+ p5 .push ();
245+ p5 .rotateY (rotation );
246+ p5 .box (100 );
247+ p5 .pop ();
248+ };
249+ };
250+
251+ export function App() {
252+ const [rotation, setRotation] = useState (0 );
253+
254+ useEffect (() => {
255+ const interval = setInterval (
256+ () => setRotation (rotation => rotation + 100 ),
257+ 100
258+ );
259+
260+ return () => {
261+ clearInterval (interval );
262+ };
263+ }, []);
264+
265+ return <ReactP5Wrapper sketch ={sketch} rotation ={rotation} />;
266+ }
267+ ```
268+
143269### Using abstracted setup and draw functions
144270
145- ``` js
271+ ``` javascript
146272import React from " react" ;
147273import { ReactP5Wrapper } from " react-p5-wrapper" ;
148274
@@ -193,7 +319,7 @@ wrapper are changed, if it is set within your sketch. This way we can render our
193319` ReactP5Wrapper ` component and react to component prop changes directly within
194320our sketches!
195321
196- ``` js
322+ ``` javascript
197323import React , { useState , useEffect } from " react" ;
198324import { ReactP5Wrapper } from " react-p5-wrapper" ;
199325
0 commit comments