Comments (16)
@Hahlh It seems that this is an error from the p5
library not from react-p5
because in this wrapper I did not attach events to the window
or document
.......all events are handled by p5
(
I'll try today to debug it and find a way how to fix it maybe is a problem with the used p5
version.
About the list of events .....yes this list is not full, unfortunately.
But if someone wants to find and add there the remaining events, it will be nice and I will immediately merge and publish a new version.
from react-p5.
@Hahlh yes everything should be compatible because they didn't introduce breaking changes......
But I'm investigating this behaviour now, maybe I will found the problem.
from react-p5.
Regarding the attached events not firing:
I resolved this by setting the z-index of the canvas element to 50 and position to relative (to make z-index work) and manually attaching an event listener to the canvas:
const setup = (p5: p5Types, canvasParentRef: Element) => {
canvasParent = p5.createCanvas(width, height).parent(canvasParentRef)
canvasParent.elt.addEventListener("mousedown", (event) => {
console.log("EVENT FIRED----------------------------:", event)
})
}
This works now beautifully to also not trigger click events, if there are other DOM elements rendered over the canvas.
from react-p5.
@Hahlh yes, will be nice if you can find the remaining events and add them to the list in a PR.
About your workaround with mouseclick...I would propose another more let's say React related.
<div onClick={this.onCamvasClick}>
<Scketch setup={this.setup} draw={this.draw} />
</div>
from react-p5.
Also in the latest version, I've added the possibility to access the event...and I've documented it.
mouseClicked(_p5, event) {
console.log(event)
// event.preventDefault()
// event.stopPropagation()
// event.target
}
it can be useful.....maybe the problem is that p5
doesnt call stopPropagation or something else.....but need to test if that's the problem
from react-p5.
@Hahlh the benefit with props is that these listeners are directly handled by p5
and you have access to the p5
in these methods ....outside of these prop-methods you only have access to the window.p5
which is not the same reference as the p5
in your sketch methods....... I'm using new p5(p => {})
to create a sketch....and only using prop-methods you have access to this p
But I don't know at the moment why it listens to the window but not to canvas.
from react-p5.
@Hahlh hmm thanks for this investigation.... so thats actually a p5 issue but not react-p5 ...
I think I will document that because everyoane is confused.
from react-p5.
You are welcome, @Gherciu!
For the moment I think, that attaching events in the setup function and using state for the boolean values like isMousePressed
should cover most use cases.
If there are any use cases, that can't be dealt with with the current setup, I am sure we can find ways to solve them 👍
from react-p5.
@IcyTv did you used parent
to render the canvas or not??
from react-p5.
maybe is a issue related to wrong usage
from react-p5.
Good day @Gherciu !
I hope you are doing great.
I face a similar issue and have declared the canvas exactly as stated in the docs:
canvas = p5.createCanvas(width, height).parent(canvasParentRef)
return (
<Sketch
className="-z-10"
mouseClicked={drawSticker}
windowResized={windowResized}
setup={setup}
draw={draw}
preload={preload}
/>
)
What can I do to debug this issue?
I tried so far to find the specific event in both the Chrome and Firefox devtools, but unfortunately without success.
Which is akward, since the event triggers everywhere on the page and that should mean, that it is attached to html, body or some wrapper element?
How can I identify the specific event in the devtools?
I also tested setting a smaller canvas size to double check that the issue isn't related to canvas size.
Looking at the p5 object, the main canvas doesn't have anything set onclick
.
If you would like to and if it is helpful, I can provide the complete p5 object in a pastebin.
Please also tell me, if you would like me to open a separate issue for this.
from react-p5.
Is it correct, that events always get attached to the global p5 object?
See:
Lines 37 to 43 in 82a5086
According to this comment by Lauren Lee McCarthy it would be necessary to attach the event to the canvas directly instead of the p5 object.
So far I have had no luck with attaching an event listener to the canvas and the react-p5
wrapper. Those events get added successfully, but simply don't fire.
On a side note:
There is also just a limited set of events being passed through as props:
Lines 16 to 36 in 82a5086
It seems that all events that p5 allows could be used, if
- the whole list of available events is used or
- the props name is passed through and simply assigned to p5 making it the users responsibility to work with the correct prop names. Would that require an additional error check?
from react-p5.
@Gherciu Thank you for your incredibly fast reply!
P5js 1.3.0 got released 14 days ago. Do you think everything should be compatible?
from react-p5.
The next question would be, how to best allow users to decide whether they would like to attach an event to the canvas element or to the global p5 object.
Maybe the easiest enhancement for the moment would be, to add the remaining p5 events to the event list (they should remain fairly static over time) and to add to the documentation, that events that get added via the props will be attached to the global p5 object and that users should use the manual method like in the example above, if they would like events to be scoped for the canvas or other elements.
If you would like to, I will later create a PR for the docs.
For the remaining events, I will check how best to include them as some of the functionality like isMousePressed
is triggered through booleans and a function if they are true?
from react-p5.
Also in the latest version, I've added the possibility to access the event...and I've documented it.
mouseClicked(_p5, event) { console.log(event) // event.preventDefault() // event.stopPropagation() // event.target }it can be useful.....maybe the problem is that
p5
doesnt call stopPropagation or something else.....but need to test if that's the problem
Awesome, thank you, Gherciu!
I will check, whether I can make the same behavior work directly through manipulating the event object.
@Hahlh yes, will be nice if you can find the remaining events and add them to the list in a PR.
About your workaround with mouseclick...I would propose another more let's say React related.
<div onClick={this.onCamvasClick}> <Scketch setup={this.setup} draw={this.draw} /> </div>
I could imagine the props becoming quite complicated, if the ability to select the target element is added, so I find this approach reasonable 👍 .
Are there any direct benefits to using the props instead of adding the listeners within the component?
How would it be possible to restrict things like isMousePressed
to a specific element like the canvas?
Using state and the mousePressed
, mouseReleased
events?
from react-p5.
@Hahlh the benefit with props is that these listeners are directly handled by
p5
and you have access to thep5
in these methods ....outside of these prop-methods you only have access to thewindow.p5
which is not the same reference as thep5
in your sketch methods....... I'm usingnew p5(p => {})
to create a sketch....and only using prop-methods you have access to thisp
But I don't know at the moment why it listens to the window but not to canvas.
Here it listens to the window and therefore also events on the canvas work. Because it listens to the whole window the events fire across the whole page, but drawing happens only on the canvas.
Normally the event listener would have to be attached to the canvas itself in order to prevent the events triggering on clicks outside of the canvas, if I am understanding it right:
Therefore, if one doesn't use props, but attaches the events for example in the setup function using the p5 instance, event triggers are successfully limited to the canvas.
from react-p5.
Related Issues (20)
- Is it possible to change the image displayed with React image upload? HOT 2
- Problem with texture img when changing variable on click.
- How can I access to p5.Vector class? HOT 3
- Uncaught TypeError: particles[i].follow is not a function HOT 1
- Typing not available for the p5 declared in the window. HOT 1
- fft.waveform() doesn't return correct array
- issues with p5 sound library import HOT 3
- Cannot read properties of undefined (reading 'stringToGlyphs')
- Type errors appearing when implementing sketch in TS HOT 1
- Multiple instances of react-p5 don't seem to work HOT 2
- `noCanvas()` doesn't seem to work HOT 2
- windowWidth not defined HOT 2
- elements render twice requiring strict mode to be removed HOT 2
- stop sound if user navigates to another tab. HOT 1
- `window is not defined` even with the dynamic import on Next.js HOT 1
- Failed to load image in preload function HOT 1
- Can we update package.json to have the latest and greatest p5.js version? HOT 1
- Renderer for canvas? HOT 1
- Passing additional arguments to functions draw() and setup() HOT 1
- :boom: BUG: deconstruct not works ! HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-p5.