SVG实现时钟效果

<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> <style> * { margin: 0; } </style> </head> <body> <svg> <title>SVG Analog Clock</title> <circle cx="125" cy="125" r="100"></circle> <g transform="translate(125, 125)"> <path d="M95 0 L 100 -5 L 100 5 Z" transform="rotate(360)"></path> </g> <g> <path d="M 125 125 V 75" transform="rotate(0)"></path> <path d="M 125 125 V 50" transform="rotate(0)"></path> <path d="M 125 125 Q 100 100 125 80 T 125 40 V 30" transform="rotate(0)"></path> </g> <g transform="translate(125, 125)"> <circle cx="0" cy="0" r="6"></circle> </g> </svg> <script> var svgns = "http://www.w3.org/2000/svg"; var face = document.getElementById("face"), ticks = document.getElementById("ticks"), triangle = document.getElementById("triangle"), txt = document.getElementById("txt"); for (var i = 0; i < 11; i++) { var temp_triangle = triangle.cloneNode(true); var angle = i * 30 + 30; temp_triangle.setAttribute("transform", "rotate(" + angle + ")"); ticks.appendChild(temp_triangle); ticks.setAttribute("transform", "translate(125, 125), rotate(-90)"); } var hourHand = document.getElementById("hour"), minuteHand = document.getElementById("minute"), secondHand = document.getElementById("second"); var hourTransform, minuteTransform, secondTransform; var secPer12Hours = 60 * 60 * 12, secPerHour = 60 * 60, secPerMinute = 60; (function init() { hourTransform = hourHand.transform.baseVal.getItem(0); minuteTransform = minuteHand.transform.baseVal.getItem(0); secondTransform = secondHand.transform.baseVal.getItem(0); updateClock(); })() function updateClock() { var date = new Date(); var sec = date.getMilliseconds() / 1000 + date.getSeconds() + date.getMinutes() * 60 + date.getHours() * 60 * 60; var hourAngle = (sec % secPer12Hours) * 360 / secPer12Hours, minuteAngle = (sec % secPerHour) * 360 / secPerHour, secondAngle = (sec % secPerMinute) * 360 / secPerMinute; hourTransform.setRotate(hourAngle, 125, 125); minuteTransform.setRotate(minuteAngle, 125, 125); secondTransform.setRotate(secondAngle, 125, 125); window.requestAnimationFrame(updateClock); } </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9dfd975ebcbf3b6a0946189a06ac4ec0.html