Repository navigation
Tutorial: How to make clock icon show a specific time #404
mholt
started this conversation in
Show and tell
Replies: 1 comment
|
Awesome! ❤️ |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
How to make the clock icon show a specific time
This was a fun weekend project, so I wanted to share it:
First we start with our SVG code. This is based on the
clock-hour-12icon because I found the one arm going straight up easier to start with. I've added a second arm that is slightly shorter for the hour hand:We will use SVG transforms to rotate each arm into place, which is easier than using trigonometry to figure out the start and end points of each arm ourselves.
Here's a JavaScript function that computes the angles of the arms:
And finally, some JS code that calls the function and applies the transform:
The
12 12at the end of eachrotate()are there because SVG rotations are done based on the corner of the viewbox by default, not the start of a path. So we shift/translate the rotation point to12,12which is the center of the24x24viewbox. That half the viewbox is the same number of hours on the clock is coincidence.Anyway, I think this'll be a neat little easter egg for users of my app. Hope you enjoy. And thanks to @codecalm for the icons!
All reactions