Tutorial: How to make clock icon show a specific time #404
mholt
started this conversation in
Show and tell
Replies: 1 comment
-
Awesome! ❤️ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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-12
icon 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 12
at 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,12
which is the center of the24x24
viewbox. 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!
Beta Was this translation helpful? Give feedback.
All reactions