Skip to content

Instantly share code, notes, and snippets.

@wodin
Last active February 18, 2020 16:49
Show Gist options
  • Save wodin/8ac8c4d035f41fa71b92a99a7e91f45a to your computer and use it in GitHub Desktop.
Save wodin/8ac8c4d035f41fa71b92a99a7e91f45a to your computer and use it in GitHub Desktop.
Traffic Light*
Traffic Light*
Working*
fail -> Error
Green*
tick -> Yellow
Yellow
tick -> Red
Red
tick -> Green
# This should probably use a CSS animation rather than a tick event
# https://stackoverflow.com/a/13955165/495319
Error
repair -> Working
Red On*
tick -> Red Off
Red Off
tick -> Red On
// A function that draws individual traffic lights
function light(color, is_illuminated) {
return $("div", {
style: {
backgroundColor: color,
width: "20px",
height: "20px",
borderRadius: "10px",
margin: "2px",
opacity: is_illuminated ? 1 : 0.2
}
});
}
// A function that draws all of our lights, based on the statechart
function render(model) {
var active_light = model.active_states[0].name;
return $(
"div",
{ style: { backgroundColor: "#444", width: "24px", padding: "1px" } },
light("#ff6060", active_light === "Red" || active_light === "Red On"),
light("orange", active_light === "Yellow"),
light("#00ff40", active_light === "Green")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment