aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/js-small-apps/clock
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-02-20 16:11:50 +0100
committerArmand Philippot <git@armandphilippot.com>2022-02-20 16:15:08 +0100
commit73a5c7fae9ffbe9ada721148c8c454a643aceebe (patch)
treec8fad013ed9b5dd589add87f8d45cf02bbfc6e91 /public/projects/js-small-apps/clock
parentb01239fbdcc5bbc5921f73ec0e8fee7bedd5c8e8 (diff)
chore!: restructure repo
I separated public files from the config/dev files. It improves repo readability. I also moved dotenv helper to public/inc directory and extract the Matomo tracker in the same directory.
Diffstat (limited to 'public/projects/js-small-apps/clock')
-rw-r--r--public/projects/js-small-apps/clock/README.md19
-rw-r--r--public/projects/js-small-apps/clock/app.js135
-rw-r--r--public/projects/js-small-apps/clock/index.html72
-rw-r--r--public/projects/js-small-apps/clock/style.css75
4 files changed, 301 insertions, 0 deletions
diff --git a/public/projects/js-small-apps/clock/README.md b/public/projects/js-small-apps/clock/README.md
new file mode 100644
index 0000000..7a89cc1
--- /dev/null
+++ b/public/projects/js-small-apps/clock/README.md
@@ -0,0 +1,19 @@
+# Clock
+
+What time is it?
+
+## Description
+
+You can see the current time in three formats:
+
+- analog clock
+- digital clock
+- text based
+
+## Preview
+
+You can see a live preview here: https://demo.armandphilippot.com/#clock
+
+## License
+
+This project is open-source and available under the [MIT License](../LICENSE).
diff --git a/public/projects/js-small-apps/clock/app.js b/public/projects/js-small-apps/clock/app.js
new file mode 100644
index 0000000..7ae2702
--- /dev/null
+++ b/public/projects/js-small-apps/clock/app.js
@@ -0,0 +1,135 @@
+function setDate(day, month, year) {
+ const div = document.getElementById("date");
+ div.textContent = `${day}/${month}/${year}`;
+}
+
+function get12Rotation(int) {
+ const hour = int > 12 ? int - 12 : int;
+ return (360 / 12) * hour;
+}
+
+function get60Rotation(int) {
+ return (360 / 60) * int;
+}
+
+function setSvgClockHours(hours) {
+ const clockHours = document.getElementById("svg-clock_hours");
+ clockHours.style.transform = `rotate(${get12Rotation(hours)}deg)`;
+ clockHours.style.transformOrigin = "center";
+}
+
+function setSvgClockMinutes(minutes) {
+ const clockMinutes = document.getElementById("svg-clock_minutes");
+ clockMinutes.style.transform = `rotate(${get60Rotation(minutes)}deg)`;
+ clockMinutes.style.transformOrigin = "center";
+}
+
+function setSvgClockSeconds(seconds) {
+ const clockSeconds = document.getElementById("svg-clock_seconds");
+ clockSeconds.style.transform = `rotate(${get60Rotation(seconds)}deg)`;
+ clockSeconds.style.transformOrigin = "center";
+}
+
+function setDigitalClockHours(hours) {
+ const clockHours = document.getElementById("digital-clock_hours");
+ clockHours.textContent = hours;
+}
+
+function setDigitalClockMinutes(minutes) {
+ const formatted = minutes < 10 ? `0` + minutes : minutes;
+ const clockMinutes = document.getElementById("digital-clock_minutes");
+ clockMinutes.textContent = formatted;
+}
+
+function getHoursToString(hours) {
+ const hoursToText = [
+ "noon",
+ "one",
+ "two",
+ "three",
+ "four",
+ "five",
+ "six",
+ "seven",
+ "eight",
+ "nine",
+ "ten",
+ "eleven",
+ "twelve",
+ ];
+
+ return hoursToText[hours % 12];
+}
+
+function getMinutesToString(minutes) {
+ const ones = [
+ "",
+ "one",
+ "two",
+ "three",
+ "four",
+ "five",
+ "six",
+ "seven",
+ "eight",
+ "nine",
+ ];
+ const teens = [
+ "ten",
+ "eleven",
+ "twelve",
+ "thirteen",
+ "fourteen",
+ "fifteen",
+ "sixteen",
+ "seventeen",
+ "eighteen",
+ "nineteen",
+ ];
+ const tens = ["", "", "twenty", "thirty", "forty", "fifty"];
+ const minutesToArray = minutes.toString().split("");
+ let text = "";
+
+ if (minutes < 10) {
+ text = ones[minutes];
+ } else if (minutes < 20) {
+ text = teens[minutesToArray[1]];
+ } else {
+ text = `${tens[minutesToArray[0]]} ${ones[minutesToArray[1]]}`;
+ }
+
+ return text;
+}
+
+function setTextClock(hours, minutes) {
+ const div = document.getElementById("text-clock");
+ const meridiem = hours < 12 ? "am" : "pm";
+ div.textContent = `It's ${getHoursToString(hours)} ${getMinutesToString(
+ minutes
+ )} ${meridiem}.`;
+}
+
+function updateAll() {
+ const now = new Date();
+ const [month, day, year] = [
+ now.getMonth() + 1,
+ now.getDate(),
+ now.getFullYear(),
+ ];
+ const [hours, minutes, seconds] = [
+ now.getHours(),
+ now.getMinutes(),
+ now.getSeconds(),
+ ];
+
+ setDate(day, month, year);
+ setSvgClockHours(hours);
+ setSvgClockMinutes(minutes);
+ setSvgClockSeconds(seconds);
+ setDigitalClockHours(hours);
+ setDigitalClockMinutes(minutes);
+ setTextClock(hours, minutes);
+}
+
+updateAll();
+setInterval(updateAll, 1000);
diff --git a/public/projects/js-small-apps/clock/index.html b/public/projects/js-small-apps/clock/index.html
new file mode 100644
index 0000000..9174170
--- /dev/null
+++ b/public/projects/js-small-apps/clock/index.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <title>What time is it?</title>
+ <link rel="stylesheet" href="style.css" />
+ </head>
+ <body>
+ <header class="header">
+ <h1>What time is it?</h1>
+ </header>
+ <main class="app">
+ <div id="date"></div>
+ <svg
+ id="svg-clock"
+ viewBox="0 0 135.46667 135.46667"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ id="path846"
+ fill="#ffffff"
+ stroke="#000000"
+ stroke-width="2.5"
+ d="M 134.20638,67.73333 A 66.473045,66.473045 0 0 1 67.73333,134.20638 66.473045,66.473045 0 0 1 1.2602844,67.73333 66.473045,66.473045 0 0 1 67.73333,1.2602844 66.473045,66.473045 0 0 1 134.20638,67.73333 Z"
+ />
+ <path
+ d="m 71.242395,67.733337 a 3.509058,3.509058 0 0 1 -3.509058,3.509058 3.509058,3.509058 0 0 1 -3.509058,-3.509058 3.509058,3.509058 0 0 1 3.509058,-3.509058 3.509058,3.509058 0 0 1 3.509058,3.509058 z"
+ />
+ <path
+ id="svg-clock_seconds"
+ d="M 67.265175,4.7740932 H 68.20149 V 68.316688 h -0.936315 z"
+ />
+ <path
+ id="svg-clock_minutes"
+ d="m 66.797363,16.601835 h 1.871941 V 68.31669 h -1.871941 z"
+ />
+ <path
+ id="svg-clock_hours"
+ d="m 65.857079,29.393064 h 3.752515 V 68.31669 h -3.752515 z"
+ />
+ <path
+ id="path1522-7"
+ d="m 67.733338,127.0842 -3.736107,8.23531 h 7.472208 z"
+ />
+ <path
+ id="path1522-7-3"
+ d="M 8.3966895,67.733338 0.16137954,63.997231 v 7.472208 z"
+ />
+ <path
+ id="path1522-7-3-5"
+ d="m 127.04505,67.733338 8.23531,-3.736107 v 7.472208 z"
+ />
+ <path
+ id="path1522-7-5"
+ d="m 67.73333,8.4010979 -3.7361,-8.23531003 h 7.47221 z"
+ />
+ </svg>
+ <div id="digital-clock">
+ <div id="digital-clock_hours" class="digital-clock__value">00</div>
+ <div class="digital-clock__sep">:</div>
+ <div id="digital-clock_minutes" class="digital-clock__value">00</div>
+ </div>
+ <div id="text-clock"></div>
+ </main>
+ <footer class="footer">
+ What time is it?. MIT 2021. Armand Philippot.
+ </footer>
+ <script src="app.js"></script>
+ </body>
+</html>
diff --git a/public/projects/js-small-apps/clock/style.css b/public/projects/js-small-apps/clock/style.css
new file mode 100644
index 0000000..f1327e1
--- /dev/null
+++ b/public/projects/js-small-apps/clock/style.css
@@ -0,0 +1,75 @@
+*,
+*::before,
+*::after {
+ box-sizing: border-box;
+ padding: 0;
+ margin: 0;
+}
+
+body {
+ display: flex;
+ flex-flow: column nowrap;
+ min-height: 100vh;
+ font-size: 16px;
+ font-size: 1rem;
+ font-family: Georgia, "Times New Roman", Times, serif;
+}
+
+.header,
+.app,
+.footer {
+ width: min(calc(100vw - 2rem), 800px);
+ margin: auto;
+}
+
+.header {
+ font-size: 2rem;
+ text-align: center;
+ margin-bottom: 2rem;
+ padding: 2rem 0;
+}
+
+.footer {
+ font-size: 0.9rem;
+ text-align: center;
+ margin-top: 3rem;
+ padding: 2rem 0;
+}
+
+.app {
+ flex: 1;
+ display: flex;
+ flex-flow: row wrap;
+ align-items: center;
+ justify-content: center;
+ gap: clamp(1rem, 5vw, 3rem);
+}
+
+#date {
+ flex: 0 0 100%;
+ font-size: 1.5rem;
+ text-align: center;
+ padding: 1rem 0;
+}
+
+#svg-clock {
+ width: 20rem;
+ height: 20rem;
+}
+
+#digital-clock {
+ display: flex;
+ flex-flow: row nowrap;
+ gap: 0.5rem;
+ width: max-content;
+ padding: 2rem;
+ border: 5px solid #000;
+ font-family: Verdana, Geneva, Tahoma, sans-serif;
+ font-size: 2rem;
+ font-weight: 600;
+}
+
+#text-clock {
+ flex: 0 0 100%;
+ text-align: center;
+}