Skip to content

Every little fish swimming around is a visitor on this page right now, and one of them is you ( ´ ▽ ` )ノ If they get in the way of reading, feel free to turn them off.

Cursor Shadow

cursor

The cursor finally casts a shadow, though this one is a bit of a drama queen and acts out whatever it touches. (๑•̀ㅂ•́)و✧

NameDescription
Canvas ShaderWritten in GLSL and executed directly on the GPU, faster than the Canvas 2D API but harder to master
SDF (Signed Distance Field)Describes shape outlines with mathematical functions, commonly used in shaders to draw smooth graphics
Document elementFromPoint()Gets the topmost element at the specified coordinates
Window getComputedStyle()Gets the resolved values of CSS properties on an element

The shadow picks its figure from the element under the cursor, while its distance, size and blur come from the light source and the cursor height.

It also puts on all kinds of performances. Go find them ◝( •ω• )◟

Today's to-dos

?
  • ⠿Give the cursor a shadow
  • ⠿Teach the shadow to stay put
  • Syncing…
Also tryDouble-clickSpam one buttonCopy, cut, pasteLeave the windowStay still
<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>

The shadow is computed per pixel in a WebGL fragment shader.

  • Figures: chosen from the cursor style and the data-cursor-shadow attribute of the element under the cursor. Each figure is an SDF (signed distance field) function, and switching figures interpolates the old and new fields, kneading one into the other like clay.
  • Projection: the cursor is treated as a tilted sheet hovering over the page with its tip pointing down. From each pixel the shader looks toward the light and checks whether the figure blocks the line of sight, so the shadow naturally shifts, stretches, and grows as the cursor rises.
  • Penumbra: the light is a disk sampled at several points from each pixel; the blocked fraction is the darkness. The closer the blocker is to the surface, the sharper the edge, so the tip stays crisp while the tail blurs.
  • Raised surfaces: elements with data-cursor-shadow-elevation sit closer to the cursor than the page, so the shadow on them is sharper and darker, with a slight offset where it crosses an edge.
  • Color: the canvas multiplies onto the page with mix-blend-mode: multiply, computed in linear color space, so the page keeps its own hue instead of getting a black veil.
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;
}