Building a Field-Ready Leaf Angle Measurement App
Leaf angle, the inclination of a leaf from vertical or horizontal, affects light interception and canopy photosynthesis and is used in crop models. Measuring it by hand in the field is slow and error-prone.
I built a browser app that reads a phone’s IMU to measure leaf angle and export the data as CSV, and changed it based on feedback from people using it in the field.
The GEMINI Project needed to record leaf angles across many plots, with the crop name, plot number, and time for each measurement. Dedicated inclinometers are expensive, paper datasheets are slow to digitize, and generic sensor apps did not export structured CSV. I wanted a free tool that opens in a phone browser with nothing to install.
Initial Design Plans
Before writing code, I sketched the UI and data flow: a large Record button and a readout of the current angle.
Idea
Smartphones already contain 3-axis accelerometers and gyroscopes. Web browsers expose these through the DeviceOrientationEvent API, which gives real-time pitch (β), roll (γ), and yaw (α) angles. A simple calculation converts these into a leaf angle value with respect to vertical.
The idea is to hold the phone flat against a leaf and press Record.
- Works in any mobile browser (no app install)
- Customizable crop name and plot number
- Log timestamped measurements in a table
- Export data as a downloadable CSV file
- Usable one-handed in the field
First Draft
The first version was built as a vanilla JavaScript / HTML Progressive Web App (PWA). It subscribed to deviceorientation events and computed the leaf angle from the gravity vector components. A simple table below the sensor readout accumulated each recorded measurement.
The first draft worked like this:
- Register a
deviceorientationabsoluteevent listener - Compute the leaf angle:
leafAngle = Math.acos(Math.cos(beta * Math.PI/180) * Math.cos(gamma * Math.PI/180)) * (180/Math.PI) - Render live X (β), Y (γ), Z (α), and computed leaf angle values
- On Record, push a row
{time, crop, plot, count, x, y, z, leafAngle}to an in-memory array and render the table - On Download, serialize the array to CSV using FileSaver.js
The Start, Record, Undo, and Download buttons were at the top of the screen.
Design Suggestion from Ismael
I shared the first draft with the field team, and Ismael Mayanja, a plant scientist who uses the app in field trials, sent feedback on Slack (May 2023):
“Can we have the buttons (Start/Record/Undo) at the bottom of the screen instead of the top, it is easier to press them that way especially in the field.” “Can we also have both volume buttons to also record if I press them.” “Can we make both the crop ID and the plot number customizable so that a user is prompted with a keyboard.” “Digital clock showing at the top. A compass.”
He also sent a hand-drawn sketch of the layout he wanted:
A follow-up message listed three more issues:
- Pressing Download CSV wasn’t working.
- The latest recorded data should appear at the top of the table, not the bottom.
- Replace “Num. of data points” with “Count” so users can quickly see how many measurements they’ve taken.
Final Design
After Ismael’s feedback and more field testing, there have been two versions: the 2023 release (v1) and a 2026 UI refresh (v2).
Changes between versions:
| Feature | Before | After |
|---|---|---|
| Button placement | Top | Bottom (thumb-friendly) |
| Volume buttons as record | ✗ | ✓ (both volume keys) |
| Crop / Plot ID | Fixed dropdown | Free-text input (keyboard prompt) |
| Digital clock | ✗ | ✓ (top center) |
| Compass | ✗ | ✓ (top right, if API available) |
| CSV download | Broken | Fixed via FileSaver.js |
| Data order in table | Oldest first | Newest first |
| Data count label | “Num. of data points” | “Count” |
Tools and steps
| Tool | Purpose |
|---|---|
| HTML / CSS / JavaScript | Core app structure, styling, and logic |
| DeviceOrientationEvent API | Real-time IMU sensor data from the phone |
| DeviceMotionEvent API | Hardware volume button detection |
| Vue.js | Reactive UI framework for live data binding and component structure |
| @vueuse/core | Vue composables for device orientation (useDeviceOrientation) |
| FileSaver.js | Client-side CSV file download |
| GitHub Pages | Free static hosting for the PWA |
| Vite | Build toolchain for the Vue project |
The steps were:
- A vanilla JavaScript prototype to check that
deviceorientationabsolutegives usable angles on Android and iOS. - A rewrite in Vue.js to clean up the reactive state (sensor readings → computed leaf angle → table rows).
@vueuse/corefor the device orientation hooks:npm i @vueuse/core- A new layout with the controls in a fixed bottom bar, a clock at the top, and free-text Crop and Plot inputs.
- FileSaver.js to fix the CSV download:
npm i file-saver unshift()instead ofpush()so the latest reading appears first.- A Vite build deployed to GitHub Pages:
npm run build # deploy dist/ to the gh-pages branch
The app can be saved to a phone’s home screen, where it opens like a native app:
On iPhone: Share → Add to Home Screen
On Android: browser menu → Add to Home Screen
References
- MDN Web Docs: DeviceOrientationEvent
- MDN Web Docs: DeviceMotionEvent
- Vue.js: https://vuejs.org/
- VueUse: @vueuse/core (
useDeviceOrientationcomposable) - FileSaver.js: https://github.com/eligrey/FileSaver.js/
- Vite: https://vitejs.dev/
- GitHub Pages: https://pages.github.com/
- GEMINI Project at UC Davis: https://projectgemini.ucdavis.edu/