I’m afraid we can’t see your private (draft) notebook with your trials in it … if you want anyone to take a look, you’ll have to publish it first.
But in any case, from your description, I think it’s pretty clear what you’re after.
Here’s a notebook that combines a simple canvas with a mousemove listener to update a mutable mousePos value:
Note that the canvas cell depends on mousePos, and uses the value of this (the previous value of the canvas), to avoid creating a new canvas every time the mouse position changes. In this way, the canvas is only created once, and the event listener is only set up once.
Thanks a lot @mbostock and @jashkenas
I got the mousePos working based on your tips:
I’m a beginner at Generators, and am trying to wrap my head around the what happens in the block that executes yield canvas;…
I see how the conditional statement prevents creating a new canvas every frame, but in my code I am still instantiating let vehicle1 = new Vehicle(50, 50); every time the mouse changes. How could I restructure that such that I create vehicle1 once and then update its state after that?
Sorry for shifting this thread away from the original mousePos question