File tree 4 files changed +47
-3
lines changed
4 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -2,25 +2,32 @@ import React from'react';
2
2
import ReactDOM from 'react-dom' ;
3
3
import P5Wrapper from 'react-p5-wrapper' ;
4
4
import sketch from './sketches/sketch' ;
5
+ import sketch2 from './sketches/sketch2' ;
5
6
6
7
class App extends React . Component {
7
8
8
9
constructor ( props ) {
9
10
super ( props ) ;
10
11
this . state = {
11
12
rotation : 150 ,
13
+ stateSketch : sketch ,
12
14
} ;
13
15
}
14
16
15
17
rotationChange ( e ) {
16
18
this . setState ( { rotation :e . target . value } ) ;
17
19
}
18
20
21
+ pressEvent ( ) {
22
+ this . state . stateSketch === sketch ? this . setState ( { stateSketch :sketch2 } ) : this . setState ( { stateSketch :sketch } ) ;
23
+ }
24
+
19
25
render ( ) {
20
26
return (
21
27
< div >
22
- < P5Wrapper sketch = { sketch } rotation = { this . state . rotation } />
28
+ < P5Wrapper sketch = { this . state . stateSketch } rotation = { this . state . rotation } />
23
29
< input type = "range" value = { this . state . rotation } min = "0" max = "360" step = "1" onInput = { this . rotationChange . bind ( this ) } />
30
+ < button onClick = { this . pressEvent . bind ( this ) } > Change Sketch</ button >
24
31
</ div >
25
32
) ;
26
33
}
Original file line number Diff line number Diff line change
1
+ export default function sketch ( p ) {
2
+ let rotation = 0 ;
3
+
4
+ p . setup = function ( ) {
5
+ p . createCanvas ( 600 , 400 , p . WEBGL ) ;
6
+ } ;
7
+
8
+ p . myCustomRedrawAccordingToNewPropsHandler = function ( props ) {
9
+ if ( props . rotation ) {
10
+ rotation = ( props . rotation + 90 ) * Math . PI / 180 ;
11
+ }
12
+ } ;
13
+ p . draw = function ( ) {
14
+ p . background ( 100 ) ;
15
+ p . noStroke ( ) ;
16
+
17
+ p . push ( ) ;
18
+ p . translate ( - 150 , 100 ) ;
19
+ p . rotateY ( rotation ) ;
20
+ p . rotateX ( - 0.9 ) ;
21
+ p . box ( 100 ) ;
22
+ p . pop ( ) ;
23
+
24
+ p . noFill ( ) ;
25
+ p . stroke ( 255 ) ;
26
+ p . push ( ) ;
27
+ p . translate ( 500 , p . height * 0.35 , - 200 ) ;
28
+ p . sphere ( 100 ) ;
29
+ p . pop ( ) ;
30
+ } ;
31
+ } ;
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " react-p5-wrapper" ,
3
- "version" : " 0.0.3 " ,
3
+ "version" : " 0.0.4 " ,
4
4
"description" : " p5-wrapper" ,
5
5
"main" : " lib/P5Wrapper.js" ,
6
6
"author" : {
Original file line number Diff line number Diff line change @@ -5,10 +5,16 @@ export default class P5Wrapper extends React.Component {
5
5
6
6
componentDidMount ( ) {
7
7
this . canvas = new p5 ( this . props . sketch , this . wrapper ) ;
8
- this . canvas . myCustomRedrawAccordingToNewPropsHandler ( this . props ) ;
8
+ if ( this . canvas . myCustomRedrawAccordingToNewPropsHandler ) {
9
+ this . canvas . myCustomRedrawAccordingToNewPropsHandler ( this . props ) ;
10
+ }
9
11
}
10
12
11
13
componentWillReceiveProps ( newprops ) {
14
+ if ( this . props . sketch !== newprops . sketch ) {
15
+ this . wrapper . removeChild ( this . wrapper . childNodes [ 0 ] ) ;
16
+ this . canvas = new p5 ( newprops . sketch , this . wrapper ) ;
17
+ }
12
18
if ( this . canvas . myCustomRedrawAccordingToNewPropsHandler ) {
13
19
this . canvas . myCustomRedrawAccordingToNewPropsHandler ( newprops ) ;
14
20
}
You can’t perform that action at this time.
0 commit comments