马黑PHP整站系统

svg时钟(石英钟)

位置: 首页 > 代码集锦[ 发布时间: 2024.10.10  作者: 马黑  阅读: 185 ]
<style>
	#papa { margin: 30px; text-align:center; }
	#hHand, #mHand, #sHand { animation: turning var(--dur) linear infinite; }
	#hHand { --begin: 0deg; --dur: 216000s; }
	#mHand { --begin: 0deg; --dur: 3600s; }
	#sHand { --begin: 0deg; --dur: 60s; }
	#kedu { font: normal 16px Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; text-anchor: middle; dominant-baseline: middle; fill: cyan; user-select: none; }
	@keyframes turning { from { transform: rotate(var(--begin)); } to { transform: rotate(calc(360deg + var(--begin))); } }
</style>

<div id="papa">
	<svg id="clock" width="300" height="300" viewBox="-100 -100 200 200">
		<defs>
			<linearGradient id="bg" x1="0" x2="1" y1="0" y2="1">
				<stop offset="0%" stop-color="red" />
				<stop offset="50%" stop-color="green" />
				<stop offset="100%" stop-color="navy" />
			</linearGradient>
		</defs>
		<circle cx="0" cy="0" r="95" fill="dimgray" stroke="url(#bg)" stroke-width="10" />
		<g id="kedu">
			<text font-size="14" fill="silver" text-anchor="middle">
				<tspan id="tdate" x="5" y="-35">日期</tspan>
				<tspan id="tday" x="0" y="-15">星期</tspan>
				<tspan x="0" y="40" fill="gray">HUACHAO</tspan>
			</text>
		</g>
		<line id="hHand" x1="0" y1="0" x2="0" y2="-65" stroke="whitesmoke" stroke-width="4" />
		<line id="mHand" x1="0" y1="0" x2="0" y2="-75" stroke="snow" stroke-width="3" />
		<line id="sHand" x1="0" y1="0" x2="0" y2="-85" stroke="white" stroke-width="2" />
		<circle cx="0" cy="0" r="6" fill="red" stroke="white" stroke-width="2" />
	</svg>
</div>

<script>
setAttr = (elm, objData) => {
	for(var key in objData) {
		elm.setAttribute(key, objData[key]);
	}
};

mkScale = (total=60) => {
	var deg = 360 / total;
	Array(total).fill('').forEach((l,k) => {
		var w = -6;
		if(k % 5 === 0) {
			var t = document.createElementNS('http://www.w3.org/2000/svg', 'text');
			setAttr(t, {transform: `rotate(${deg * k - 60} 0 0) translate(75) rotate(${-1 * (deg * k - 60)} 0 0)`});
			t.textContent = k / 5 + 1;
			kedu.appendChild(t);
			w = -4;
		}
		l = document.createElementNS('http://www.w3.org/2000/svg', 'line');
		setAttr(l, {transform: `rotate(${deg * k - 60} 0 0) translate(90)`, x1: 0, y1: 0, x2: w, y2: 0, stroke: 'cyan'});
		kedu.appendChild(l);
	});
};

setTime = () => {
	var now = new Date();
	var hr = now.getHours() > 12 ? now.getHours() - 12 : now.getHours(),
		min = now.getMinutes(),
		sec = now.getSeconds(),
		msec = now.getMilliseconds();
	var hDeg = hr * 30 + (min * 6 / 12),
		mDeg = min * 6 + (sec * 6 / 60),
		sDeg = sec * 6 + (msec * 0.36 / 1000);
	hHand.style.setProperty('--begin', hDeg + 'deg');
	mHand.style.setProperty('--begin', mDeg + 'deg');
	sHand.style.setProperty('--begin', sDeg + 'deg');
};

setDate = () => {
	var sDate = new Date();
	var sDateS = sDate.getSeconds() * 1000,
		sDateMs = sDate.getMilliseconds();
	tdate.textContent = `${sDate.getFullYear()}年${sDate.getMonth() + 1}月${sDate.getDate()}日`;
	tday.textContent = `星期${'日一二三四五六'.substr(sDate.getDay(),1)}`;
	setTimeout( () => {
		setDate();
	}, 60000 - sDateS - sDateMs);
};

mkScale();
setTime();
setDate();
</script>

前一篇: svg文本路径动画:舞动的文字
下一篇: 没有了

发表评论:

       

评论列表 [3条]

#3 | 知名不具 于 2024-10-17 09:29 发布: 这svg时钟设计精巧细致,当宝贝一样珍藏起来。

#2 | 飞飞 于 2024-10-10 19:44 发布: 这表盘颜色,指针颜色,沉稳大气,功能齐全。。颜值高级,又实用。。耐看有内涵型的。。

#1 | 飞飞 于 2024-10-10 19:43 发布: 设计很巧妙,也精准。。前面代码还好,能看懂个大概。。。到JS部分直接抓 瞎了。。。大概看得出一丢丢。。

Copyright © 2023 All Right Reserved 马黑PHP文章管理整站系统v1.8
联系我们: gxblk@163.com