js+svg实现数字时钟显示时间效果代码
代码语言:html
所属分类:其他
代码描述:js+svg实现数字时钟显示时间效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body { background: #111; } #clock { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: calc(100vw - 4px); max-width: 300px; } </style> </head> <body> <div id="clock"></div> <script> // this function creates the SVG and appends it to the element you give to it function initClock(elt, color = '#0ff', backgroundColor = 'none') { let timeObj = { numberPathsD: { topM: "M 8 4 L 16 4 L 18 6 L 16 8 L 8 8 L 6 6 Z", midd: "M 8 18 L 16 18 L 18 20 L 16 22 L 8 22 L 6 20 Z", topL: "M 6 7 L 8 9 L 8 17 L 6 19 L 4 17 L 4 9 Z", topR: "M 18 7 L 20 9 L 20 17 L 18 19 L 16 17 L 16 9 Z", btmM: "M 8 32 L 16 32 L 18 34 L 16 36 L 8 36 L 6 34 Z", btmL: "M 6 21 L 8 23 L 8 31 L 6 33 L 4 31 L 4 23 Z", btmR: "M 18 21 L 20 23 L 20 31 L 18 33 L 16 31 L 16 23 Z" }, seconds: { topM: [1, 0, 1, 1, 0, 1, 1, 1, 1, 1], midd: [0, 0, 1, 1, 1, 1, 1, 0, 1, 1], topL: [1, 0, 0, 0, 1, 1, 1, 0, 1, 1], topR: [1, 1, 1, 1, 1, 0, 0, 1, 1, 1], btmM: [1, 0, 1, 1, 0, 1, 1, 0, 1, 1], btmL: [1, 0, 1, 0, 0, 0, 1, 0, 1, 0], btmR: [1, 1, 0, 1, 1, 1, 1, 1, 1, 1] }, tenSeconds: { topM: [1, 0, 1, 1, 0, 1], midd: [0, 0, 1, 1, 1, 1], topL: [1, 0, 0, 0, 1, 1], topR: [1, 1, 1, 1, 1, 0], btmM: [1, 0, 1, 1, 0, 1], btmL: [1, 0, 1, 0, 0, 0], btmR: [1, 1, 0, 1, 1, 1] }, minutes: { topM: [1, 0, 1, 1, 0, 1, 1, 1, 1, 1], midd: [0, 0, 1, 1, 1, 1, 1, 0, 1, 1], topL: [1, 0, 0, 0, 1, 1, 1, 0, 1, 1], topR: [1, 1, 1, 1, 1, 0, 0, 1, 1, 1], btmM: [1, 0, 1, 1, 0, 1, 1, 0, 1, 1], btmL: [1, 0, 1, 0, 0, 0, 1, 0, 1, 0], btmR: [1, 1, 0, 1, 1, 1, 1, 1, 1, 1] }, tenMinutes: { topM: [1, 0, 1, 1, 0, 1], midd: [0, 0, 1, 1, 1, 1], topL: [1, 0, 0, 0, 1, 1], topR: [1, 1, 1, 1, 1, 0], btmM: [1, 0, 1, 1, 0, 1], btmL: [1, 0, 1, 0, 0, 0], btmR: [1, 1, 0, 1, 1, 1] }, hours: { topM: [1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], midd: [1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0], topL: [0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0], topR: [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1], btmM: [1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0], btmL: [1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0], btmR: [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1] }, tenHours: { topM: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], midd: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], topL: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], topR: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], btmM: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], btmL: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], btmR: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] } }; // it's a piece of cake to make a pretty date const now = new Date(); // now.setHours(11); // now.setMinutes(59); // now.setSeconds(56); const hours = now.getHours(); const onesDigitHours = hours % 12; const minutes = now.getMinutes(); const onesDigitMinutes = minutes % 10; const seconds = now.getSeconds(); const onesDigit = now.getSeconds() % 10; const milliseconds = now.getMilliseconds(); let HH = hours.toString().padStart(2, '0'); let MM = minutes.toString().padStart(2, '0'); let SS = seconds.toString().padStart(2, '0'); let M0 = MM.charAt(0) * 1; let M1 = MM.charAt(1) * 1; let S0 = SS.charAt(0) * 1; let S1 = SS.charAt(1) * 1; // convert to number HH = (HH * 1); // starting time let HHMMSS = `${HH}:${MM}:${SS}`; // percent that the milliseconds are at let milliAnimStartPercent = (milliseconds / 1000) * 1; // shift an array to an index const shiftAtIndex = (arr, index) => { let rm = arr.splice(0, index); arr.push(...rm); return arr; } // rearrange indexes of digits based on what it currently is const adjustProps = (digit, numberIndex) => { for (let prop in timeObj[digit]) { timeObj[digit][prop] = shiftAtIndex(timeObj[digit][prop], numberIndex); } } // rearrange the animations adjustProps('seconds', S1); adjustProps('tenSeconds', S0); adjustProps('minutes', M1); adjustProps('tenMinutes', M0); adjustProps('hours', HH); adjustProps('tenHours', HH); // function that'll create a path with correctly ordered animations const populateAnimations = (array, pathD, waitTime, id, alrea.........完整代码请登录后点击上方下载按钮下载查看
网友评论0