Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing props to sketch function #21

Closed
F-Moody opened this issue Mar 25, 2018 · 1 comment
Closed

Passing props to sketch function #21

F-Moody opened this issue Mar 25, 2018 · 1 comment

Comments

@F-Moody
Copy link

F-Moody commented Mar 25, 2018

Hello there,
i have a sketch function ( same as the one on the first example) and i want to pass some props to this function and i cant understand how.
So i have the index.js file which is something like this

`import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import P5Wrapper from 'react-p5-wrapper';
import sketch from './demo';

class App extends Component {
state = {
x: undefined,
y: undefined
}

onMove(e){
    this.setState({x:e.screenX, y: e.screenY})
}

 
render() {
return (
  <div className="App" onMouseMove={(e) => this.onMove(e)}>
    <header className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h1 className="App-title">Welcome to React</h1>
    </header>
    <p className="App-intro">
        <P5Wrapper sketch={sketch} />
    </p>
  </div>
);

}

}

export default App;`

and my sketch function is this

`
export default function sketch (p) {
let rotation = 0;

p.setup = function () {
    p.createCanvas(800, 800, p.WEBGL);
    p.angleMode('DEGREES');
};

p.myCustomRedrawAccordingToNewPropsHandler = function (props) {
    if (props.rotation){
        rotation = props.rotation * Math.PI / 180;
    }
};

p.draw = function () {
    p.background(100);
    p.stroke(255);
    p.push();
    p.rotateX(rotation);
   // p.box(100);
    p.fill(255);
   // p.rotate(10);
    p.translate(props.x,props.y);
    p.rect(0,0,100,50);
    p.pop();
};

};`

now as you can see i want to pass down x and y to my sketch, but i can't figure out how

@and-who
Copy link
Contributor

and-who commented May 29, 2019

props is an Argument of the myCustomRedrawAccordingToNewPropsHandler Function.
So props is not in the Scope of the draw Function

@and-who and-who closed this as completed May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants