Skip to content

Instantly share code, notes, and snippets.

@lassemt
Created August 22, 2019 18:19
Show Gist options
  • Save lassemt/8117fe45d431a432551deb12248f5cd9 to your computer and use it in GitHub Desktop.
Save lassemt/8117fe45d431a432551deb12248f5cd9 to your computer and use it in GitHub Desktop.
Spark AR – Counter based on time.
const D = require('Diagnostics');
const Scene = require('Scene');
const Time = require('Time');
const R = require('Reactive');
const FaceTracking = require('FaceTracking');
//
// Define constants
//
const OPENNESS_THRESHOLD = 0.5;
// How often it should count (miliseconds)
// This 👇 will increase the number every 100ms
const INTERVAL_TIME = 100;
// How much number should increase on each iteration
const INCREASE_VALUE = 1;
//
// Define varies
//
let counterInterval;
let currentCount = 0;
//
// Get objects
//
const objCounter = Scene.root.find('counter');
//
// Functions
//
// This function is called in intervals later on.
function increaseCount () {
// Increase the current count
currentCount += INCREASE_VALUE;
// Update the text with new count
objCounter.text = currentCount.toString();
}
//
// Facetracking shit
//
const trackedFace = FaceTracking.face(0);
const triggerMouthOpen = trackedFace.mouth.openness.gt(OPENNESS_THRESHOLD);
// Trigger mouth open
triggerMouthOpen.monitor().subscribe(e => {
if (e.newValue) {
// Mouth is open, start counter
counterInterval = Time.setInterval(increaseCount, INTERVAL_TIME);
} else {
// Mouth is closed, stop counting
Time.clearInterval(counterInterval);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment