1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Horloge fluide</title>
<meta name="Author" content="NoSmoking">
<meta name="DVP-discussion" content="d2090203">
<meta name="description" content="Affichage d'une horloge avec déplacement des aiguilles fluide.">
<style>
html, body {
margin: 0;
padding: 0;
font: 1em/1.5 Verdana,sans-serif;
}
h1, h2, h3 {
margin: .25em 0;
color: #069;
}
time {
float: right;
margin: .5em;
font-size: 0.9em;
color: #888;
}
main {
display: block;
margin: auto;
max-width: 60em;
}
#clock {
position: relative;
width: 60vh;
height: 60vh;
margin: auto;
background-image: url(_fond-clock.png);
background-position: center;
background-repeat: no-repeat;
background-size: 100% 100%;
}
#clock div {
box-shadow: 0 0 .5em transparent; /* anti crénelage ! */
will-change: transform;
}
#heures {
position: absolute;
top: 25%;
left: calc( 50% - 1.1vh);
width: 2.2vh;
height: 50%;
border-radius: 1.1vh;
background: linear-gradient(rgba(100,150,200,.7) 0%, rgba(50,100,150,.7) 50%, transparent 50%);
}
#minutes {
position: absolute;
top: 10%;
left: calc( 50% - .8vh);
width: 1.6vh;
height: 80%;
border-radius: .8vh;
background: linear-gradient(rgba(150,200,250,.8) 0%, rgba(50,100,150,.8) 50%, transparent 50%);
}
#secondes {
position: absolute;
top: 0%;
left: calc( 50% - .4vh);
width: .8vh;
height: 100%;
border-radius: .4vh;
background: linear-gradient(rgba(100,150,200,.9) 0%, rgba(0,50,150,.9) 60%, transparent 60%);
}
#center {
position: absolute;
top: 50%;
left: 50%;
width: 1.6vh;
height: 1.6vh;
border: .5vh solid #069;
border-radius: 50%;
background-color: #CCC;
transform: translate(-50%, -50%);
}
#info {
position: absolute;
bottom: 30%;
width: 100%;
text-align: center;
font-size: 5vh;
color: #069;
}
</style>
</head>
<body>
<main>
<header>
<time datetime="2020-09-09">Sept. 2020</time>
<h1>Horloge avec déplacement fluide</h1>
</header>
<div id="clock">
<div id="heures"></div>
<div id="minutes"></div>
<div id="secondes"></div>
<div id="center"></div>
<div id="info"></div>
</div>
<p><b>Nota</b> : La fluidité sur le déplacement des aiguilles des heures et des minutes n'est pas forcément perceptible.</p>
<footer>
<p>Voir la <a href="https://www.developpez.net/forums/showthread.php?t=2090203">discussion sur Developpez.com</a>
</footer>
</main>
<script>
"use strict";
const HEURE_ALPHA_MS = 360 / 43200000; // pas de 0.000008333333333333334
const MINUTE_ALPHA_MS = 360 / 3600000; // pas de 0.0001
const SECONDE_ALPHA_MS = 360 / 60000; // pas de 0.006
const elHeures = document.getElementById("heures");
const elMinutes = document.getElementById("minutes");
const elSecondes = document.getElementById("secondes");
const elInfo = document.getElementById("info");
function digitaleClock(date) {
const sDate = date.toLocaleString().split(" ").pop();
elInfo.textContent = sDate;
}
function analogiqueClock(date) {
const h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const ms = date.getMilliseconds();
let angle;
// convert en millisecondes
const sToMs = (s * 1000) + ms;
const mToMs = ((m * 60) * 1000) + sToMs;
const hToMs = ((h * 3600) * 1000) + mToMs;
// draw Secondes
angle = (sToMs * SECONDE_ALPHA_MS) % 360;
elSecondes.style.transform = "rotate(" + angle + "deg)";
// draw Minutes
angle = (mToMs * MINUTE_ALPHA_MS) % 360;
elMinutes.style.transform = "rotate(" + angle + "deg)";
// draw Heures
angle = (hToMs * HEURE_ALPHA_MS) % 360;
elHeures.style.transform = "rotate(" + angle + "deg)";
}
function updateClock() {
const d = new Date();
analogiqueClock(d);
digitaleClock(d);
requestAnimationFrame(updateClock);
}
updateClock();
</script>
</body>
</html> |