commit 4ba201fa276fc1f37fa5c356df71c83c8eaa621b Author: tangsongdayo Date: Fri Nov 20 14:46:07 2026 +0800 Mouse Clock 1.0.0: Silly Clock 3 (Kurt Grigg) rewrite with touch support, settings and setup guide Co-Authored-By: Claude Opus 5.5 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3ac4b03 --- /dev/null +++ b/LICENSE @@ -0,0 +1,31 @@ +滑鼠立體時鐘 Mouse Clock + +Original script: "Silly Clock 3" by Kurt Grigg. +Traditional Chinese adaptation: 網頁建置百寶箱 JavaScript 收集區. +The original clock design and algorithm remain the work of Kurt Grigg. +Please keep the credit comment at the top of mouse-clock.js. + +The modifications in this repository (packaging, settings, touch support, +start/stop API, documentation) are released under the MIT License: + +MIT License + +Copyright (c) 2026 唐宋打油 (https://tsdylab.com/) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..37d60d6 --- /dev/null +++ b/README.md @@ -0,0 +1,139 @@ +# 滑鼠立體時鐘 Mouse Clock + +一個會跟著滑鼠跑的類比時鐘:鐘面數字、時針、分針、秒針一個字一個字拖著跑,外圈還有一圈旋轉的日期。這是 1990 年代末很多個人網頁都會放的經典效果,原作是 Kurt Grigg 的 **Silly Clock 3**,台灣的「網頁建置百寶箱」收錄過中文化版本。 + +這個版本由[唐宋打油](https://tsdylab.com/)改寫,用在〈[平淡文章的華麗產地:敲敲打打的工坊✨BBP 11](https://tsdylab.com/savepoint/tinkering-workshop/)〉。和原版相比: + +- 一個檔案、一行就能放進網頁,不用改任何程式。 +- 手機也能用:手指在畫面上滑動,時鐘會出現在手指上方(不會被手掌擋住)。 +- 顏色、大小、位置、速度、日期語言(中文/英文)都可以設定。 +- 分頁在背景時不計算;系統開啟「減少動態」的使用者預設不顯示。 +- 可以用程式開關(`MouseClock.start()`、`MouseClock.stop()`)。 + +## 檔案 + +| 檔案 | 用途 | +| --- | --- | +| `mouse-clock.js` | 時鐘本體,只需要這個檔案 | +| `index.html` | 範例頁,下載後用瀏覽器打開就能試 | + +--- + +## 放置教學 + +### 方法一:上傳檔案再引用(推薦) + +1. 下載 `mouse-clock.js`(在檔案頁按「原始檔」或「下載」)。 +2. 上傳到你的網站,例如放在網站根目錄的 `js/` 資料夾。 +3. 在網頁的 `` 前面加一行: + +```html + +``` + +完成。重新整理網頁,移動滑鼠就會看到時鐘。 + +> 請不要直接引用本站(code.tsdylab.com)的檔案網址當作 `src`:程式碼平台送出的檔案類型是純文字,瀏覽器會拒絕執行;而且這個網址之後可能變動。請下載後放在自己的網站。 + +### 方法二:直接貼進網頁(沒辦法上傳檔案時) + +打開 `mouse-clock.js`,全選複製,貼在網頁 `` 前面,並用 ` +``` + +### 各平台放法 + +**WordPress** +- 只想在某一篇文章出現:編輯文章 → 新增「自訂 HTML」區塊 → 用方法二貼上。 +- 想全站都有:用「WPCode」這類可以在頁尾插入程式碼的外掛,把方法一的 ` + ``` + +**Blogger** +版面配置 → 新增小工具 → 「HTML/JavaScript」→ 用方法二貼上 → 儲存。 + +**Hugo/Hexo/Jekyll 等靜態網站** +把 `mouse-clock.js` 放進靜態檔案資料夾(Hugo 是 `static/js/`、Hexo 是 `source/js/`、Jekyll 是 `assets/js/`),再在佈景主題的頁尾樣板加上方法一的那一行。 + +**不能用的地方** +Medium、方格子、Notion、Facebook 這類平台不允許文章放自己的 JavaScript,沒辦法使用。 + +--- + +## 設定 + +在 ` +``` + +| 屬性 | 預設 | 說明 | +| --- | --- | --- | +| `data-date-color` | `#000000` | 外圈日期的顏色 | +| `data-face-color` | `#0000ff` | 鐘面數字 1~12 的顏色 | +| `data-hour-color` | `#ff0000` | 時針顏色 | +| `data-minute-color` | `#0000ff` | 分針顏色 | +| `data-second-color` | `#00ff00` | 秒針顏色 | +| `data-size` | `40` | 鐘面半徑(px),日期環是它的 1.5 倍 | +| `data-offset-x` | `80` | 電腦:時鐘中心在游標右邊幾 px(負數=左邊) | +| `data-offset-y` | `80` | 電腦:時鐘中心在游標下面幾 px(負數=上面) | +| `data-touch-offset-x` | `0` | 手機:時鐘中心相對手指的水平位置 | +| `data-touch-offset-y` | `-110` | 手機:時鐘中心相對手指的垂直位置(預設在手指上方) | +| `data-speed` | `0.3` | 跟隨速度 0~1,越小拖得越長(原版是 0.6) | +| `data-date-speed` | `0.015` | 日期環旋轉速度(原版是 0.06) | +| `data-lang` | `zh-TW` | 日期語言:`zh-TW`(2026年 十一月 20日 星期五)或 `en`(FRIDAY 20 NOVEMBER 2026) | +| `data-z-index` | `9999` | 疊放順序;時鐘被網站的選單蓋住時調大 | +| `data-reduced-motion` | `hide` | 使用者在系統開啟「減少動態」時:`hide` 不顯示,`show` 照常顯示 | + +### 用程式控制 + +在 ` + + +``` + +`MouseClock.start(設定)` 的設定名稱和上表相同,只是改成駝峰寫法(`data-hour-color` → `hourColor`)。重複呼叫 `start()` 會先關掉舊的再開新的。 + +--- + +## 常見問題 + +**時鐘沒有出現?** +- 確認 `src` 的路徑對:直接在瀏覽器打開那個網址,應該要看到程式碼。 +- 手機要先用手指在畫面上點一下或滑動,時鐘才會跑到手指附近;一開始會在畫面正中央。 +- 系統有開「減少動態」的話預設不顯示,可以加 `data-reduced-motion="show"`。 + +**時鐘被網站的選單或標題列蓋住?** +加大 `data-z-index`,例如 `99999`。 + +**會影響網頁的點擊嗎?** +不會。時鐘的每個字都設定成滑鼠點不到(`pointer-events: none`),也不會被螢幕閱讀器念出來。 + +--- + +## 授權與致謝 + +- 原作:**Silly Clock 3**,Kurt Grigg。 +- 中文化:網頁建置百寶箱 JavaScript 收集區(已停站)。原始碼參考自[國立臺灣海洋大學教學頁](http://squall.cs.ntou.edu.tw/Dreamweaver5/2-09MouseClockJavaScript.htm)。 +- 改寫:唐宋打油([tsdylab.com](https://tsdylab.com/)),2026。 + +改寫的部分以 MIT 授權釋出,歡迎自由使用、修改、放在任何網站(包含商業網站)。原作的時鐘設計與演算法屬於 Kurt Grigg,請保留 `mouse-clock.js` 開頭的註解,讓原作者的名字繼續被看見。詳見 [LICENSE](LICENSE)。 diff --git a/index.html b/index.html new file mode 100644 index 0000000..b1b80bf --- /dev/null +++ b/index.html @@ -0,0 +1,26 @@ + + + + + +滑鼠立體時鐘 範例 + + + +

滑鼠立體時鐘

+

移動滑鼠(手機上用手指滑動),時鐘會跟著你。

+

+ + + + +

+

放到自己網頁的方法見 README.md。

+ + + + diff --git a/mouse-clock.js b/mouse-clock.js new file mode 100644 index 0000000..95a50e2 --- /dev/null +++ b/mouse-clock.js @@ -0,0 +1,188 @@ +/*! + * 滑鼠立體時鐘 Mouse Clock 1.0.0 + * https://code.tsdylab.com/tangsongdayo/mouse-clock + * + * 原作:Silly Clock 3 by Kurt Grigg + * 中文化:網頁建置百寶箱 JavaScript 收集區 + * 改寫:唐宋打油 https://tsdylab.com/(觸控支援、設定項、可停止) + * 授權說明見 LICENSE + */ +(function () { + 'use strict'; + + var DEFAULTS = { + dateColor: '#000000', // 日期環顏色 + faceColor: '#0000ff', // 鐘面數字顏色 + hourColor: '#ff0000', // 時針顏色 + minuteColor: '#0000ff', // 分針顏色 + secondColor: '#00ff00', // 秒針顏色 + size: 40, // 鐘面半徑(px) + offsetX: 80, // 時鐘中心在游標右邊多少 px(負值=左邊) + offsetY: 80, // 時鐘中心在游標下面多少 px(負值=上面) + touchOffsetX: 0, // 手機:時鐘中心相對手指的位置 + touchOffsetY: -110, // 預設在手指上方,不會被手掌擋住 + speed: 0.3, // 跟隨速度 0~1,越小拖得越長(原版 0.6) + dateSpeed: 0.015, // 日期環旋轉速度(原版 0.06) + lang: 'zh-TW', // 日期語言:'zh-TW' 或 'en' + zIndex: 9999, + reducedMotion: 'hide' // 使用者系統設定「減少動態」時:'hide' 不顯示、'show' 照常顯示 + }; + + var NAMES = { + 'zh-TW': { + days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'], + months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], + format: function (y, mo, d, wd) { return y + '年 ' + mo + ' ' + d + '日 ' + wd + ' '; } + }, + en: { + days: ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'], + months: ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'], + format: function (y, mo, d, wd) { return ' ' + wd + ' ' + d + ' ' + mo + ' ' + y + ' '; } + } + }; + + var running = null; + + function start(options) { + if (running) running.stop(); + 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]); }); + + if (o.reducedMotion === 'hide' && window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) { + return null; + } + + var names = NAMES[o.lang] || NAMES['zh-TW']; + var now = new Date(); + var D = names.format(now.getFullYear(), names.months[now.getMonth()], now.getDate(), names.days[now.getDay()]).split(''); + 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 xmouse = window.innerWidth / 2, ymouse = window.innerHeight / 2, currStep = 0; + var isTouch = !!(window.matchMedia && window.matchMedia('(hover: none)').matches); + + function zeros(len) { var a = []; for (var i = 0; i < len; i++) a.push(0); return a; } + var y = zeros(n), x = zeros(n), Y = zeros(n), X = zeros(n); + var Dy = zeros(D.length), Dx = zeros(D.length), DY = zeros(D.length), DX = zeros(D.length); + + var root = document.createElement('div'); + root.className = 'mouse-clock'; + root.setAttribute('aria-hidden', 'true'); + var css = document.createElement('style'); + css.textContent = + '.mouse-clock>div{position: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;}'; + root.appendChild(css); + + function make(chars, cls, color) { + return chars.map(function (c) { + var el = document.createElement('div'); + el.className = cls; + el.style.color = color; + el.textContent = c; + root.appendChild(el); + return el; + }); + } + var elDate = make(D, 'mc-date', o.dateColor); + var elFace = make(Face, 'mc-face', o.faceColor); + var elH = make(H, 'mc-hand', o.hourColor); + var elM = make(M, 'mc-hand', o.minuteColor); + var elS = make(S, 'mc-hand', o.secondColor); + document.body.appendChild(root); + + function place(el, top, left) { el.style.transform = 'translate(' + left + 'px,' + top + 'px)'; } + + // Pointer Events 分辨滑鼠和手指(手機點擊後補發的滑鼠事件不會干擾) + function onPointer(e) { + var touch = e.pointerType !== 'mouse'; + if (isTouch && !touch) return; + ymouse = e.clientY + (touch ? o.touchOffsetY : o.offsetY); + xmouse = e.clientX + (touch ? o.touchOffsetX : o.offsetX); + } + // 手指滑動捲頁時瀏覽器會中斷 pointermove,改由 touchmove 接手 + function onTouch(e) { + var t = e.touches[0]; + if (!t) return; + ymouse = t.clientY + o.touchOffsetY; + xmouse = t.clientX + o.touchOffsetX; + } + document.addEventListener('pointerdown', onPointer, { passive: true }); + document.addEventListener('pointermove', onPointer, { passive: true }); + document.addEventListener('touchmove', onTouch, { passive: true }); + + function draw() { + var t = new Date(), i; + var sec = -1.57 + Math.PI * t.getSeconds() / 30; + 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)); + 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++) + place(elM[i], y[i] + HandY + (i * HandLen) * Math.sin(min), x[i] + HandX + (i * HandLen) * Math.cos(min)); + 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)); + currStep -= o.dateSpeed; + } + + function tick() { + if (document.hidden) return; // 分頁在背景時不計算 + var i; + Dy[0] = Math.round(DY[0] += (ymouse - DY[0]) * o.speed); + Dx[0] = Math.round(DX[0] += (xmouse - DX[0]) * o.speed); + for (i = 1; i < D.length; i++) { + Dy[i] = Math.round(DY[i] += (Dy[i - 1] - DY[i]) * o.speed); + Dx[i] = Math.round(DX[i] += (Dx[i - 1] - DX[i]) * o.speed); + } + y[0] = Math.round(Y[0] += (ymouse - Y[0]) * o.speed); + x[0] = Math.round(X[0] += (xmouse - X[0]) * o.speed); + for (i = 1; i < n; i++) { + y[i] = Math.round(Y[i] += (y[i - 1] - Y[i]) * o.speed); + x[i] = Math.round(X[i] += (x[i - 1] - X[i]) * o.speed); + } + draw(); + } + var timer = setInterval(tick, 20); // 原版每 20ms 一格,保持相同節奏 + + running = { + stop: function () { + clearInterval(timer); + document.removeEventListener('pointerdown', onPointer); + document.removeEventListener('pointermove', onPointer); + document.removeEventListener('touchmove', onTouch); + if (root.parentNode) root.parentNode.removeChild(root); + running = null; + } + }; + return running; + } + + // 讀取