Last active
March 19, 2020 16:13
-
-
Save shritesh/7e7d31488c04ba118c3e15d5e65607d5 to your computer and use it in GitHub Desktop.
ReasonML p5.js Stopwatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type p5; | |
type sketch; | |
type button; | |
type state = | |
| Initializing | |
| NotStarted(button) | |
| Running(button, int) | |
| Stopped(button, int); | |
[@bs.module] [@bs.new] external createP5: (sketch => unit) => p5 = "p5"; | |
[@bs.send] external createCanvas: (sketch, int, int) => unit = "createCanvas"; | |
[@bs.send] external noCanvas: sketch => unit = "noCanvas"; | |
[@bs.set] external onSetup: (sketch, unit => unit) => unit = "setup"; | |
[@bs.set] external onDraw: (sketch, unit => unit) => unit = "draw"; | |
[@bs.send] external millis: sketch => int = "millis"; | |
[@bs.send] external createButton: (sketch, string) => button = "createButton"; | |
[@bs.send] external html: (button, string) => unit = "html"; | |
[@bs.send] | |
external mousePressed: (button, unit => unit) => unit = "mousePressed"; | |
[@bs.send] external nfc: (sketch, float, int) => string = "nfc"; | |
let makeSketch = s => { | |
let appState = ref(Initializing); | |
let btnPressed = () => | |
switch (appState^) { | |
| NotStarted(btn) => appState := Running(btn, millis(s)) | |
| Running(btn, time) => appState := Stopped(btn, time) | |
| Stopped(btn, _) => | |
appState := NotStarted(btn); | |
btn->html("Start"); | |
| _ => () | |
}; | |
s->onSetup(() => { | |
s->noCanvas; | |
let btn = s->createButton("Start"); | |
btn->mousePressed(btnPressed); | |
appState := NotStarted(btn); | |
}); | |
s->onDraw(() => | |
switch (appState^) { | |
| Running(btn, time) => | |
let seconds = float(millis(s) - time) /. 1000.0; | |
let str = s->nfc(seconds, 2); | |
btn->html(str ++ " Seconds"); | |
| _ => () | |
} | |
); | |
}; | |
createP5(makeSketch); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment