Mouse Clock 1.3.0: glow and font size options; editor fields and neon preset

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tangsongdayoandClaude Opus 5.5 committed 2026-11-20 17:47:00 +08:00
1 parent 3241c06114
commit 32aaf8a191
3 files changed
+72 -20

No files matched your search

+22 -11
View File
@@ -1,5 +1,5 @@
/*!
* 滑鼠立體時鐘 Mouse Clock 1.2.0
* 滑鼠立體時鐘 Mouse Clock 1.3.0
* https://code.tsdylab.com/tangsongdayo/mouse-clock
*
* 原作:Silly Clock 3 by Kurt Grigg
@@ -25,6 +25,10 @@
dateSpeed: 0.015, // 日期環旋轉速度(原版 0.06)
lang: 'zh-TW', // 日期語言:'zh-TW' 或 'en'
zIndex: 9999,
fontSize: 10, // 1.3.0:鐘面數字與日期的字大小(px)
handSize: 16, // 1.3.0:指針(點)的字大小(px)
glowColor: '', // 1.3.0:字的外光暈顏色,空白=不加
glowSize: 0, // 1.3.0:外光暈範圍(px),0=不加
reducedMotion: 'hide', // 使用者系統設定「減少動態」時:'hide' 不顯示、'show' 照常顯示
container: null // 1.2.0:只在這個元素(或 CSS 選擇器)裡顯示,時鐘不會跑出它的範圍
};
@@ -49,12 +53,13 @@
var o = {}, k;
for (k in DEFAULTS) o[k] = DEFAULTS[k];
for (k in (options || {})) if (options[k] !== undefined && options[k] !== '') o[k] = options[k];
['size', 'offsetX', 'offsetY', 'touchOffsetX', 'touchOffsetY', 'speed', 'dateSpeed', 'zIndex'].forEach(function (key) { o[key] = Number(o[key]); });
['size', 'offsetX', 'offsetY', 'touchOffsetX', 'touchOffsetY', 'speed', 'dateSpeed', 'zIndex', 'fontSize', 'handSize', 'glowSize'].forEach(function (key) { o[key] = Number(o[key]); });
if (o.reducedMotion === 'hide' && window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
return null;
}
if (!/^[#a-zA-Z0-9(),.%\s]*$/.test(String(o.glowColor || ''))) o.glowColor = ''; // 只接受顏色寫法
var box = o.container;
if (typeof box === 'string') box = document.querySelector(box);
if (box && box.nodeType !== 1) box = null;
@@ -65,10 +70,15 @@
var H = '...'.split(''), M = '....'.split(''), S = '.....'.split('');
var Face = '1 2 3 4 5 6 7 8 9 10 11 12'.split(' ');
var n = Face.length, Split = 360 / n, Dsplit = 360 / D.length;
var R = o.size, HandLen = R / 4.5, HandY = -7, HandX = -2.5;
var R = o.size, HandLen = R / 4.5;
var F = o.fontSize > 0 ? o.fontSize : 10, HS = o.handSize > 0 ? o.handSize : 16;
// 字放大時維持各字的中心位置(預設 10/16px 時與原版相同:FO=0、HandX=-2.5、HandY=-7)
var FO = (10 - F) / 2, HandX = 5 - HS * 0.46875, HandY = 5 - HS * 0.75;
var xmouse = window.innerWidth / 2, ymouse = window.innerHeight / 2, currStep = 0;
// 容器模式:時鐘所有字(含日期環、字寬)與容器邊緣的距離
var padL = Math.max(R * 1.5, R + 7) + 4, padR = Math.max(R * 1.5 + 10, R + 16) + 4;
var reach = 4 * HandLen; // 最長的秒針(5 個點)離中心的距離
var padL = Math.max(R * 1.5 - FO, reach - Math.min(HandX, HandY)) + 4;
var padR = Math.max(R * 1.5 + FO + F, reach + Math.max(HandX, HandY) + HS) + 4;
var oldPosition = null;
if (box) {
if (getComputedStyle(box).position === 'static') { oldPosition = box.style.position; box.style.position = 'relative'; }
@@ -93,8 +103,9 @@
css.textContent =
'.mouse-clock>div{position:' + (box ? 'absolute' : 'fixed') + ';top:0;left:0;pointer-events:none;text-align:center;line-height:1;' +
'will-change:transform;z-index:' + o.zIndex + ';margin:0;padding:0;}' +
'.mouse-clock .mc-face,.mouse-clock .mc-date{width:10px;height:10px;font:10px Arial,sans-serif;}' +
'.mouse-clock .mc-hand{width:16px;height:16px;font:bold 16px Arial,sans-serif;}';
'.mouse-clock .mc-face,.mouse-clock .mc-date{width:' + F + 'px;height:' + F + 'px;font:' + F + 'px Arial,sans-serif;}' +
'.mouse-clock .mc-hand{width:' + HS + 'px;height:' + HS + 'px;font:bold ' + HS + 'px Arial,sans-serif;}' +
(o.glowColor && o.glowSize > 0 ? '.mouse-clock>div{text-shadow:0 0 ' + o.glowSize + 'px ' + o.glowColor + ',0 0 ' + (o.glowSize * 2) + 'px ' + o.glowColor + ';}' : '');
root.appendChild(css);
if (box) root.style.cssText = 'position:absolute;inset:0;overflow:hidden;pointer-events:none;';
@@ -149,8 +160,8 @@
var min = -1.57 + Math.PI * t.getMinutes() / 30;
var hrs = -1.575 + Math.PI * t.getHours() / 6 + Math.PI * t.getMinutes() / 360;
for (i = 0; i < n; i++)
place(elFace[i], y[i] + R * Math.sin(-1.0471 + i * Split * Math.PI / 180),
x[i] + R * Math.cos(-1.0471 + i * Split * Math.PI / 180));
place(elFace[i], y[i] + FO + R * Math.sin(-1.0471 + i * Split * Math.PI / 180),
x[i] + FO + R * Math.cos(-1.0471 + i * Split * Math.PI / 180));
for (i = 0; i < H.length; i++)
place(elH[i], y[i] + HandY + (i * HandLen) * Math.sin(hrs), x[i] + HandX + (i * HandLen) * Math.cos(hrs));
for (i = 0; i < M.length; i++)
@@ -158,8 +169,8 @@
for (i = 0; i < S.length; i++)
place(elS[i], y[i] + HandY + (i * HandLen) * Math.sin(sec), x[i] + HandX + (i * HandLen) * Math.cos(sec));
for (i = 0; i < D.length; i++)
place(elDate[i], Dy[i] + R * 1.5 * Math.sin(currStep + i * Dsplit * Math.PI / 180),
Dx[i] + R * 1.5 * Math.cos(currStep + i * Dsplit * Math.PI / 180));
place(elDate[i], Dy[i] + FO + R * 1.5 * Math.sin(currStep + i * Dsplit * Math.PI / 180),
Dx[i] + FO + R * 1.5 * Math.cos(currStep + i * Dsplit * Math.PI / 180));
currStep -= o.dateSpeed;
}
@@ -208,7 +219,7 @@
var current = document.currentScript;
// 1.1.0:在載入前設定 window.MouseClockOptions = {...} 也可以調整(data-* 屬性優先)
var preset = window.MouseClockOptions;
window.MouseClock = { version: '1.2.0', start: start, stop: function () { if (running) running.stop(); }, defaults: DEFAULTS };
window.MouseClock = { version: '1.3.0', start: start, stop: function () { if (running) running.stop(); }, defaults: DEFAULTS };
// 加上 data-manual 就不自動啟動,改由 MouseClock.start({...}) 啟動
if (!(current && current.dataset && current.dataset.manual !== undefined)) {