Skip to content
Advertisement

How to display text sequentially using P5.js deviceMoved() function?

I am currently trying to make a program where the text changes as the phone moves every couple value(s) using the P5.JS deviceMoved() function. (the gif below displays how i wanted the text to change eventually as the device moved)

enter image description here

As seen on the code below, I’ve put all the text in the array and I wanted to change the index to +1 each time say the move value ads 30 and repeat until all the text is gone.

JavaScript
JavaScript

I think my problem is within the onMove function, where I need to define the current value and what values could change the text, I’m fairly new at this so any insight/solution to do this would be highly appreciated 🙂

Thank you!

Advertisement

Answer

There are several issues related to the onMove function. First and foremost it is never called, and unlike deviceMoved it is not a special function that p5.js automatically invokes. Additional issues:

JavaScript

Here’s a fixed version:

JavaScript

In order to test this out on my mobile device (iOS 14) I had to add some code to request access to the DeviceMotionEvent, and host it in an environment using HTTPS and not embedding in an iframe. You can see my code on glitch and run it live here.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement