File tree Expand file tree Collapse file tree 4 files changed +47
-3
lines changed Expand file tree Collapse file tree 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';
22import ReactDOM from 'react-dom' ;
33import P5Wrapper from 'react-p5-wrapper' ;
44import sketch from './sketches/sketch' ;
5+ import sketch2 from './sketches/sketch2' ;
56
67class App extends React . Component {
78
89 constructor ( props ) {
910 super ( props ) ;
1011 this . state = {
1112 rotation : 150 ,
13+ stateSketch : sketch ,
1214 } ;
1315 }
1416
1517 rotationChange ( e ) {
1618 this . setState ( { rotation :e . target . value } ) ;
1719 }
1820
21+ pressEvent ( ) {
22+ this . state . stateSketch === sketch ? this . setState ( { stateSketch :sketch2 } ) : this . setState ( { stateSketch :sketch } ) ;
23+ }
24+
1925 render ( ) {
2026 return (
2127 < div >
22- < P5Wrapper sketch = { sketch } rotation = { this . state . rotation } />
28+ < P5Wrapper sketch = { this . state . stateSketch } rotation = { this . state . rotation } />
2329 < 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 >
2431 </ div >
2532 ) ;
2633 }
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 11{
22 "name" : " react-p5-wrapper" ,
3- "version" : " 0.0.3 " ,
3+ "version" : " 0.0.4 " ,
44 "description" : " p5-wrapper" ,
55 "main" : " lib/P5Wrapper.js" ,
66 "author" : {
Original file line number Diff line number Diff line change @@ -5,10 +5,16 @@ export default class P5Wrapper extends React.Component {
55
66 componentDidMount ( ) {
77 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+ }
911 }
1012
1113 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+ }
1218 if ( this . canvas . myCustomRedrawAccordingToNewPropsHandler ) {
1319 this . canvas . myCustomRedrawAccordingToNewPropsHandler ( newprops ) ;
1420 }
You can’t perform that action at this time.
0 commit comments