跳到內容

每條游來游去的小魚,都是正在瀏覽這一頁的訪客,有一條是你自己 ( ´ ▽ ` )ノ 覺得干擾閱讀的話,可以把小魚關掉。

游標影子

cursor

游標終於有影子了,只是這影子好像很有想法。(´・ω・`)

名稱描述
Canvas Shader使用 GLSL 開發,直接在 GPU 上執行,比 Canvas 2D API 更快,但也更難
SDF (Signed Distance Field)有號距離場,用數學函式描述形狀輪廓,常用於 Shader 中繪製平滑圖形
Document elementFromPoint()取得指定座標位置最上層的元素
Window getComputedStyle()取得元素 CSS 屬性的解析值(resolved values)

影子依游標下的元素自動換造型,遠近、大小、模糊程度則由光源與高度決定。

還會有各種演出效果,來發掘看看吧 ◝( •ω• )◟

今天要做的事

?
  • ⠿幫游標裝影子
  • ⠿訓練影子別亂跑
  • 同步中⋯
也試試雙擊連點同一顆鈕複製、剪下、貼上移出視窗放著別動
<template>
<div class="w-full flex flex-col gap-6 p-4">
<div class="stage flex flex-col items-center gap-10 rounded-xl px-8 py-16 md:px-12 md:py-20">
<!-- 待辦清單:data-cursor-shadow-elevation 讓卡片比背景高,影子落在上面更近、更清楚 -->
<article
class="card max-w-sm w-full flex flex-col gap-5 rounded-2xl p-6"
data-cursor-shadow-elevation="16"
>
<header class="flex items-center gap-2">
<h3 class="flex-1 font-bold">
{{ t('title') }}
</h3>
<span
class="icon-btn cursor-help"
:title="t('helpTitle')"
>?</span>
<!-- 指定造型:愛心鈕讓影子怦然心動 -->
<button
class="icon-btn"
:class="{ 'icon-btn--liked': isLiked }"
data-cursor-shadow="heart"
:aria-label="t('like')"
@click="isLiked = !isLiked"
>
♥
</button>
</header>
<ul class="flex flex-col gap-2">
<li
v-for="todo in todoList"
:key="todo.id"
class="row flex items-center gap-3 rounded-xl px-3 py-2"
>
<!-- 拖曳把手:grab 變貓掌,按住變握拳 -->
<span class="handle cursor-grab select-none active:cursor-grabbing">⠿</span>
<button
class="check"
:class="{ 'check--done': todo.isDone }"
role="checkbox"
:aria-checked="todo.isDone"
@click="todo.isDone = !todo.isDone"
/>
<span
class="flex-1 text-sm"
:class="{ 'line-through opacity-50': todo.isDone }"
>{{ todo.text }}</span>
<!-- 指定造型:刪除鈕讓影子嚇到靈魂出竅 -->
<button
class="remove"
data-cursor-shadow="ghost"
:aria-label="t('remove')"
@click="removeTodo(todo.id)"
>
✕
</button>
</li>
<!-- 等待中:progress 變蝸牛 -->
<li class="row row--syncing flex cursor-progress items-center gap-3 rounded-xl px-3 py-2 text-sm">
<span class="spinner" />
{{ t('syncing') }}
</li>
</ul>
<!-- 輸入框變鉛筆;沒填字時「新增」停用,影子舉手投降 -->
<form
class="flex gap-2"
@submit.prevent="addTodo"
>
<input
v-model="draft"
class="field min-w-0 flex-1"
:placeholder="t('placeholder')"
>
<button
class="btn btn--primary"
:disabled="!draft.trim()"
>
{{ t('add') }}
</button>
</form>
<!-- 密碼欄:影子摀住眼睛;嚇人的按鈕:影子躲到後面偷看 -->
<div class="flex gap-2">
<input
class="field min-w-0 flex-1"
type="password"
autocomplete="new-password"
:placeholder="t('password')"
>
<button
class="btn btn--danger"
type="button"
data-cursor-shadow="scary"
@click="todoList = []"
>
{{ t('clear') }}
</button>
</div>
</article>
<!-- 其他演出的觸發方式 -->
<div class="flex flex-wrap items-center justify-center gap-2 text-xs">
<span class="opacity-50">{{ t('tryAlso') }}</span>
<span
v-for="item in tipList"
:key="item"
class="tip rounded-full px-3 py-1"
>
{{ t(item) }}
</span>
</div>
</div>
<cursor-shadow class="z-[9999]" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import CursorShadow from '../cursor-shadow.vue'
interface Todo {
id: number;
text: string;
isDone: boolean;
}
const { t } = useI18n()
const isLiked = ref(false)
let nextId = 3
const todoList = ref<Todo[]>([
{ id: 1, text: t('todoFirst'), isDone: true },
{ id: 2, text: t('todoSecond'), isDone: false },
])
const draft = ref('')
function addTodo() {
const text = draft.value.trim()
if (!text) {
return
}
// 最多留 4 項,避免範例越長越高
todoList.value = [...todoList.value, { id: nextId++, text, isDone: false }].slice(-4)
draft.value = ''
}
function removeTodo(id: number) {
todoList.value = todoList.value.filter((todo) => todo.id !== id)
}
const tipList = [
'tipDoubleClick',
'tipFatigue',
'tipCopy',
'tipLeave',
'tipIdle',
]
</script>
<style scoped lang="sass">
// 固定淺色舞台,深色模式下影子才看得清楚
.stage
background: #f3eee4
color: #3b352d
// 卡片不加陰影,只用細框,畫面上只有游標的影子,才不會互相干擾
.card
border: 1px solid rgba(59, 53, 45, 0.1)
background: #fffdf8
.icon-btn
display: flex
width: 2rem
height: 2rem
align-items: center
justify-content: center
border-radius: 999px
background: rgba(59, 53, 45, 0.08)
font-weight: bold
&--liked
color: #e2587a
.row
background: rgba(59, 53, 45, 0.05)
&--syncing
color: rgba(59, 53, 45, 0.6)
.handle
color: rgba(59, 53, 45, 0.35)
.check
width: 1.1rem
height: 1.1rem
flex-shrink: 0
border: 1.5px solid rgba(59, 53, 45, 0.4)
border-radius: 0.35rem
&--done
border-color: #3b352d
background: #3b352d
.remove
color: rgba(59, 53, 45, 0.4)
font-size: 0.75rem
.spinner
width: 0.9rem
height: 0.9rem
border: 2px solid rgba(59, 53, 45, 0.2)
border-top-color: #6aa9d8
border-radius: 999px
animation: spin 1s linear infinite
@keyframes spin
to
transform: rotate(360deg)
.btn
flex-shrink: 0
padding: 0.35rem 0.9rem
border: 1px solid rgba(59, 53, 45, 0.3)
border-radius: 999px
font-size: 0.875rem
cursor: pointer
&:disabled
cursor: not-allowed
opacity: 0.45
&--primary
border-color: #3b352d
background: #3b352d
color: #fffdf8
&--danger
border-color: #c94f4f
color: #c94f4f
.field
padding: 0.35rem 0.9rem
border: 1px solid rgba(59, 53, 45, 0.3)
border-radius: 999px
background: transparent
font-size: 0.875rem
.tip
background: rgba(59, 53, 45, 0.07)
</style>

影子全在 WebGL 的片段著色器裡逐像素計算。

  • 造型:依游標下元素的 cursor 樣式與 data-cursor-shadow 屬性決定。每個造型是一個 SDF(有號距離場)函式,換造型時把新舊距離場內插,像黏土一樣揉過去。
  • 投影:把游標當成懸在頁面上、尖端朝下的傾斜薄片,從每個像素朝光源看,查視線有沒有被造型擋住。影子因此自然會位移、拉長,游標離得越遠越大。
  • 半影:光源是一個圓盤,從每個像素朝圓盤上多個點取樣,被擋住的比例就是濃淡。遮擋點越貼近表面越銳利,所以尖端清楚、尾端模糊。
  • 墊高的表面:帶 data-cursor-shadow-elevation 的元素比頁面更靠近游標,落在上面的影子更銳利、更深,跨過邊緣時會錯開一點。
  • 顏色:以 mix-blend-mode: multiply 疊在頁面上,並在線性色彩空間計算,保留底色的色相,不是蓋一層黑紗。
interface Props {
/** 是否顯示影子,切換時淡入淡出。@default true */
visible?: boolean;
/** 影子最深處的顏色,可用任何 CSS 顏色。
* 影子以相乘疊在頁面上,會保留底色的色相;帶點彩度的深色比純黑自然。@default '#1c1830'
*/
color?: string;
/** 影子最深處的濃度,0 ~ 1。@default 0.75 */
opacity?: number;
/** 造型大小(px),約為造型的邊長。@default 36 */
size?: number;
/** 游標尖端的懸浮高度(px)。越高影子越遠、越大、越模糊。@default 40 */
height?: number;
/** 碰到可互動元素(造型不是箭頭)時游標會湊近,與表面的距離縮為此比例。
* 按住時再湊近一半,但仍保留一段距離,只有箭頭會真正貼上。@default 0.7
*/
touchRatio?: number;
/** 懸浮時影子與游標熱點的最小間距(px),水平或垂直至少有一軸錯開這段距離,
* 影子才不會被游標圖示擋住。按下貼上表面時不受此限。@default 32
*/
hoverGap?: number;
/** 光源位置,以視窗寬高比例表示,(0, 0) 為左上角、(1, 1) 為右下角,可超出範圍。
* 影子落在游標背離光源的那一側。@default { x: 0, y: 0 }
*/
lightPosition?: LightPosition;
/** 光源高度(px),須大於 height。越低影子拉得越遠。@default 800 */
lightHeight?: number;
/** 光源直徑(px)。越大半影越寬,影子越模糊。@default 100 */
lightSize?: number;
/** 光源跟著本機時鐘走:早晚太陽低、影子長,中午短;入夜換成檯燈,偶爾閃一下。
* 開啟後 lightPosition 與 lightHeight 改由時間決定。@default false
*/
clockLight?: boolean;
/** 光源輕微呼吸,像燭光,只讓半影些微變化。@default false */
flicker?: boolean;
/** 深色模式。相乘在深色背景上看不見,改用一般混合,影子外圍再加一圈極淡的亮光。
* 未指定時跟隨文件站的深淺色。
*/
dark?: boolean;
/** 滑鼠靜止多久後影子開始打盹(ms),睡著前會先打個呵欠,偶爾還會自己玩手影。
* 0 表示不打盹。@default 4000
*/
idleDelay?: number;
}