We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to make an animation (image sequence) that is controlled by the position of the mouse (mouseX). My idea is to use the map() function to scale the mouse’s x-coordinate numbers between 0 and width to image numbers (image1, image2,...image360) so that the location of the mouse determines which image displays. The goal is to get the illusion of rotating an object by using an image sequence exported from an 3d animation. I just can't get my head around how to do this, any ideas? Thank you so much!
Answers
Hi
This is untested code. Here I just show the concept of what it would look like to select an image from an image array and display it on screen. The image is selected based on the position of the mouse along the width of the sketch. All the way to the left shows the first image, all the way to the right... it shows the last image. I hope this helps,
Kf
@Sofia_hki -- Interesting concept, and your idea of how to do it with map() is on exactly the right track. You should try it as @kfrajer says. Or, to be extremely concise (but slightly less readable), you can drop the index, count, and working image variables, and just use an image array -- something like this:
Note that if your images are large, you may not actually want to try to preload 360 images in memory simultaneously! However, loading purely on-demand may create real slowdown if you want smooth animation. There are a number of ways of tackling this to increase performance or save resources -- but they get increasingly complex, so you may want to start simple.
Keep in mind that if your only goal is to rotate an object, there are 3D object formats in Processing that are designed specifically for this and will perform much better than loading stacks of images.
Also keep in mind that there is a Processing video library -- I'm not sure how it performs on scrubbing the video frame read location (that may depend on the video file format), but controlling video play location with the mouse might be another way of manipulating an animated stack of images.
Thank you so much for your help @kfrajer and @jeremydouglass! I tried your examples but for some reason with both of them I got the error code "Type mismatch, "float" does not match with "int"", that seemed to be related to the map function. Do you happen to know what that means? My end goal is to rotate an object in the browser. I tried using Processing video library and controlling video with the mouse – it worked with Processing but my challenge is to get it work in Squarespace using Processing JS. I think I'd have the same problem with 3d objects.
https://forum.Processing.org/two/discussion/18929/how-to-export-a-processing-file-as-a-webpage-html-format-file
https://processing.org/reference/map_.html
@Sofia_hki The map function manages float values. To fix your type mismatch, do the following:
floar index = map((float)mouseX, 0.0, (float)width, 0.0, (float)(allFigures.length-1));
I am casting the integer values into float. I hope this helps. If you have any questions about p5.js, begin with GoToLoop's link and come back to the forum with any other question.
Kf
Thanks @kfrajer but for some reason I keep getting the same error message?
@Sofia_hki --
allFigures[]
wants an int (an array index).map()
returns a float.allFigures[map()]
gives an error -- it wants an int, but gets a float. So use(int)map()
orint(map())
to convert the the float value that map returns to an int, then use that as a the array index. Now everything works.length-1
. Just uselength
.Here is a small working example.
Thank you so much @jeremydouglass, now it works! Really appreciate your help!!