Your code was invaluable I’m very curious how you arrived at it, though; the reference docs do list all the functions you’re using, but how you managed to get it all working, in the right order, etc… [mind blown]
The only reason I rewrote it was because I couldn’t quite grok why you were setting up multiple persistent Z.every event handlers; it looked like they’d still be triggered, swapping the targets, even after a target had been found. In my case I’m only looking for two targets - the front and back of a postcard - just so I can spot if a user is pointing their phone at the wrong side and tell them to flip it over. As soon as I’ve found a target I don’t want any extraneous Z.every handlers stealing cycles.
So rather than setting up Z.every “loops”, I’m using a Z.after “timeout” that swaps the targets if nothing’s been recognised. Once a target’s acquired I no longer need to think about targets and swapping stuff.
I like to push things until they break so I know what the limits are, and I found that even at 100ms (!) per target my two test phones were able to acquire a target, which made it feel very snappy. Trouble is that if they weren’t pointing at a target, after a couple of minutes it’d crash the process. Extending the timeout to 500ms per target also extended the time before crashing, and for 2 targets, it’s still fast enough.
Faster swapping leading to faster crashes… hence my thought that there’s a memory leak or something similar going on.
The thing I can’t get my tiny head round is quite what happens to existing targetInstances when you swap the targetFinder. If you’ve found a target, a targetInstance is born, and you can tell one of your nodes to be positioned relative to it. But if the target is then lost, and you swap targets and then find another one, what happens to the previous instance? Does it just stop existing? And the nodes that were using the previous instance as a relativeTo parent - what becomes of them? What’s their “parent” at that point?
These are probably questions I need to explore rather than asking to be tutored on JS here (Still trying to get my head round that weird (function()={…})() code pattern too - never seen that before. Doesn’t seem to stop the memory leaks though)
Thanks for the info re using Windows / Chrome for debugging - shame the Mac doesn’t quite work on this front, but I can dig out a windows machine to play with.
Thanks again, man, you’re blazing a trail for us