# 涂抹显影 Scratch Reveal > 上面一张图被鼠标擦掉,露出下面那张,擦过的不恢复,像刮刮乐。 分类:鼠标特效 · 角色:主角(占一整块、抢全部注意力,一页最多一个) [全部组件和流程](https://www.dachuiai.com/llms.txt) · [鼠标特效选型规则](https://www.dachuiai.com/guides/cursor.md) ## 给 AI:用户把这个组件发给了你 1. 先看用户的项目:框架、页面类型、现在的风格、手机还是电脑为主。 2. 对照下面的「适合 / 不适合」判断合不合适。不合适就直接说,并从[鼠标特效选型规则](https://www.dachuiai.com/guides/cursor.md)或[全部组件](https://www.dachuiai.com/llms.txt)里另挑一个。 3. 合适就按「装进项目」装好,示例里的文字和图片换成用户自己的。 4. 告诉用户用了什么、放在哪个文件、以后想改快慢、颜色、大小改哪个参数。 ## 什么时候用 - 适合:修复前后、改造前后、新旧对比,或者抽奖、彩蛋这种要人动手的地方。 - 不适合:要马上看到结果的信息;以手机访问为主的页面;两张图构图对不上。 - 适合的网站:活动页、官网、作品集、本地小店 - 放在:首屏、内容区 - 手机上:要改:手机上不盖上面那张,直接显示下面的内容;想在手机上刮,要另做手指版,并处理好和页面滚动的冲突。 - 性能代价:低:只在鼠标移动时画。 - 常搭配:[数字滚动 Count Up](https://www.dachuiai.com/c/count-up.md)、[滚动出现 Scroll Reveal](https://www.dachuiai.com/c/scroll-reveal.md) ## 装进项目 - 写法:React 18 以上 + TypeScript + Tailwind CSS v4,一个文件就能用,不引用别的组件。 - 依赖:只用 react,不用另外安装 - 文件:原样存成 `scratch-reveal.tsx`(比如 `src/components/scratch-reveal.tsx`)。能跑命令就下载[原文](https://www.dachuiai.com/code/scratch-reveal.tsx),比手抄准:`curl -fsSL -o src/components/scratch-reveal.tsx https://www.dachuiai.com/code/scratch-reveal.tsx`(Windows 写 `curl.exe`)。下载完看一眼第一行是 `import`,不是网页。 - 导出:`ScratchReveal` 是可以直接用的部分;最后的 `export default` 是演示示例,只作参考。 - 图片:示例用了本站的 [/art/bg-beach-waves-wide.jpg](https://www.dachuiai.com/art/bg-beach-waves-wide.jpg)。换成用户自己的图;只想先看效果,可以从本站同样的路径下载,放进项目的 public 目录。 - 注意:cover 要和 children 里的图同一构图;decorate 在盖好之后再往上画(颗粒、标签);区域大小变了会重新盖上。 - Next.js(有 app 文件夹):文件第一行加 `'use client';`。 - 用 JavaScript 不用 TypeScript:Vite 和 Next.js 都能直接用 .tsx 文件;也可以删掉类型标注,改名 .jsx。 - 没有 Tailwind:先装 Tailwind CSS v4(Vite 项目 `npm i tailwindcss @tailwindcss/vite`,vite 配置的 plugins 里加 `tailwindcss()`,入口引入的那个 CSS 文件(一般是 `src/index.css`)第一行写 `@import "tailwindcss";`),或者把类名改写成项目用的样式。 - 已经装了 Tailwind v3:别为了一个组件升级。类名大多通用,少数 v4 写法要换,比如焦点框 `outline-2` 在 v3 里要写成 `outline outline-2`。 - 不是 React(Vue、Svelte、小程序、纯 HTML):照着代码的做法改写,时长、曲线、参数保持一样。Wix、有赞这类建站平台装不进代码,告诉用户去平台自带的动效设置里找。 ## 参数 | 参数 | 默认值 | 作用 | | --- | --- | --- | | `cover` | | 上面那张图的地址 | | `children` | | 包在里面的内容 | | `filter` | `'none'` | 画上面那张时加的滤镜(和 CSS 的 filter 一样写),比如做旧成黑白 | | `decorate` | | 盖好之后再往上画点东西(颗粒、暗角、划痕、标签),参数是画布、宽、高 | | `brush` | `66` | 笔刷半径(px) | | `className` | `''` | 加在最外层的类名,用来调大小、位置、间距 | ## 你可以说 > 两张图叠放,上面一张画在 Canvas 上;鼠标划过的地方用柔边笔刷擦掉,露出下面那张,擦过不恢复。 ## 完整代码 ```tsx import {useEffect, useRef, type ReactNode} from 'react'; /* --------------------------------------------------------------- * ScratchReveal 涂抹显影 * 两张图叠放:上面一张画在 Canvas 上,鼠标划过的地方用柔边笔刷擦掉,露出下面那张;擦过的不会恢复。 * 只有移动的鼠标会擦,停着不动不擦;走得快也是连成一条,不会断成一个个点。 * 只在有鼠标的设备上盖上面那张;手机、平板上直接显示下面的内容。区域大小变了会重新盖上。 * cover:上面那张图的地址 * filter:画上面那张时加的滤镜(和 CSS 的 filter 一样写),比如做旧成黑白 * decorate:盖好之后再往上画点东西(颗粒、暗角、划痕、标签),参数是画布、宽、高 * brush:笔刷半径(px) * 下面那张写在 children 里,可以是图,也可以是任何内容。 * ------------------------------------------------------------- */ type Props = {cover: string; children: ReactNode; filter?: string; decorate?: (ctx: CanvasRenderingContext2D, w: number, h: number) => void; brush?: number; className?: string}; export function ScratchReveal({cover, children, filter = 'none', decorate, brush = 66, className = ''}: Props) { const root = useRef(null); const canvas = useRef(null); useEffect(() => { if (!matchMedia('(pointer: fine)').matches) return; const el = root.current!, cv = canvas.current!, ctx = cv.getContext('2d')!; const img = new Image(); let W = 0, H = 0, ready = false, last: {x: number; y: number} | null = null; const paint = () => { if (!img.naturalWidth) return; const d = Math.min(2, devicePixelRatio || 1); W = el.clientWidth; H = el.clientHeight; cv.width = Math.round(W * d); cv.height = Math.round(H * d); ctx.setTransform(d, 0, 0, d, 0, 0); // 和 object-fit: cover 一样铺满 const k = Math.max(W / img.naturalWidth, H / img.naturalHeight), w = img.naturalWidth * k, h = img.naturalHeight * k; ctx.filter = filter; ctx.drawImage(img, (W - w) / 2, (H - h) / 2, w, h); ctx.filter = 'none'; decorate?.(ctx, W, H); ctx.globalCompositeOperation = 'source-over'; ready = true; cv.style.opacity = '1'; }; // 柔边笔刷:中间擦干净,边上渐渐透 const stamp = (x: number, y: number) => { const g = ctx.createRadialGradient(x, y, 0, x, y, brush); g.addColorStop(0, 'rgba(0,0,0,1)'); g.addColorStop(0.45, 'rgba(0,0,0,.9)'); g.addColorStop(1, 'rgba(0,0,0,0)'); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(x, y, brush, 0, Math.PI * 2); ctx.fill(); }; const move = (e: PointerEvent) => { if (e.pointerType === 'touch' || !ready) return; const r = el.getBoundingClientRect(), x = e.clientX - r.left, y = e.clientY - r.top; if (last) { ctx.globalCompositeOperation = 'destination-out'; const n = Math.ceil(Math.hypot(x - last.x, y - last.y) / (brush / 8)); for (let i = 1; i <= n; i++) stamp(last.x + ((x - last.x) * i) / n, last.y + ((y - last.y) * i) / n); ctx.globalCompositeOperation = 'source-over'; } last = {x, y}; }; const leave = () => { last = null; }; let size = ''; const ro = new ResizeObserver(() => { const next = `${el.clientWidth}x${el.clientHeight}`; if (next !== size) { size = next; paint(); } }); img.onload = () => { ro.observe(el); }; img.src = cover; el.addEventListener('pointermove', move); el.addEventListener('pointerleave', leave); return () => { ro.disconnect(); img.onload = null; el.removeEventListener('pointermove', move); el.removeEventListener('pointerleave', leave); }; }, [cover, filter, decorate, brush]); return (
{children}
); } /* ---------------- 示例:修复一张旧照片 ---------------- */ const PHOTO = '/art/bg-beach-waves-wide.jpg'; const SANS = '-apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif'; // 做旧:暖灰的颗粒、四周压暗、几粒灰尘和划痕,左上角一个「旧」字标签 function age(ctx: CanvasRenderingContext2D, W: number, H: number) { let seed = 11; const rnd = () => { seed = (seed + 0x6d2b79f5) >>> 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; const grain = document.createElement('canvas'); grain.width = grain.height = 256; const g = grain.getContext('2d')!, px = g.createImageData(256, 256); for (let i = 0; i < px.data.length; i += 4) { const v = 90 + rnd() * 165; px.data[i] = px.data[i + 1] = px.data[i + 2] = v; px.data[i + 3] = 34; } g.putImageData(px, 0, 0); ctx.globalCompositeOperation = 'overlay'; ctx.fillStyle = ctx.createPattern(grain, 'repeat')!; ctx.fillRect(0, 0, W, H); ctx.globalCompositeOperation = 'multiply'; const v = ctx.createRadialGradient(W / 2, H / 2, Math.min(W, H) * 0.3, W / 2, H / 2, Math.hypot(W, H) * 0.62); v.addColorStop(0, 'rgba(255,250,240,1)'); v.addColorStop(1, 'rgba(96,84,70,1)'); ctx.fillStyle = v; ctx.fillRect(0, 0, W, H); ctx.globalCompositeOperation = 'source-over'; ctx.strokeStyle = 'rgba(255,252,244,.5)'; ctx.fillStyle = 'rgba(255,252,244,.55)'; for (let i = 0; i < 34; i++) { const x = rnd() * W, y = rnd() * H; if (rnd() < 0.6) { ctx.beginPath(); ctx.arc(x, y, 0.6 + rnd() * 1.4, 0, 7); ctx.fill(); continue; } ctx.lineWidth = 0.6 + rnd() * 0.6; ctx.beginPath(); ctx.moveTo(x, y); ctx.quadraticCurveTo(x + (rnd() - 0.5) * 30, y + rnd() * 40, x + (rnd() - 0.5) * 26, y + 30 + rnd() * 60); ctx.stroke(); } ctx.fillStyle = 'rgba(36,32,28,.82)'; ctx.beginPath(); ctx.roundRect(24, 22, 64, 36, 18); ctx.fill(); ctx.fillStyle = '#C9BFAE'; ctx.beginPath(); ctx.arc(42, 40, 3.5, 0, 7); ctx.fill(); ctx.fillStyle = '#F1EADC'; ctx.font = `700 16px ${SANS}`; ctx.textBaseline = 'middle'; ctx.fillText('旧', 52, 41); } export default function ScratchRevealDemo() { return ( 海边的沙滩和浪 新 ); } ```