Here is some JS code extracted from a much bigger project that I worked on.