DIV CSS 佈局教程網

Canvas 打造的滑鼠軌跡特效:一段 window.onload 裡的數學
編輯:JQuery特效代碼     

這個特效會在游標移動後拖出一道由無數細線纏繞而成的彩色尾跡,顏色隨位置在色相環上遊走。它沒有使用任何 jQuery,純粹靠 Canvas 2D 與三角函數運算。

程式碼

<body>
<div id="text"></div>
<canvas id="keleyi_com"></canvas>

<script type="text/javascript">
window.onload = function () {
  C = Math.cos; S = Math.sin; U = 0;
  w = window; j = document;
  d = j.getElementById("kele" + "yi_com");
  c = d.getContext("2d");
  W = d.width = w.innerWidth;
  H = d.height = w.innerHeight;
  c.fillRect(0, 0, W, H);
  c.globalCompositeOperation = "lighter";
  c.lineWidth = 0.2;
  c.lineCap = "round";

  var bool = 0, t = 0;
  d.onmousemove = function (e) {
    if (window.T) {
      if (D == 9) { D = Math.random() * 15; f(1); }
      clearTimeout(T);
    }
    X = e.pageX; Y = e.pageY;
    A = X; B = Y;
    R = (e.pageX / W * 999 >> 0) / 999;
    r = (e.pageY / H * 999 >> 0) / 999;
    U = e.pageX / H * 360 >> 0;
    D = 9; g = 360 * Math.PI / 180;

    T = setInterval(f = function (e) {
      c.save();
      c.globalCompositeOperation = "source-over";
      if (e != 1) {
        c.fillStyle = "rgba(0,0,0,0.02)";
        c.fillRect(0, 0, W, H);       // 半透明黑覆蓋,製造殘影
      }
      c.restore();

      i = 25;
      while (i--) {
        c.beginPath();
        if (D > 450 || bool) {
          if (!bool) bool = 1;
          if (D < 0.1) bool = 0;
          t -= g; D -= 0.1;
        }
        if (!bool) { t += g; D += 0.1; }

        q = (R / r - 1) * t;
        x = (R - r) * C(t) + D * C(q) + (A + (X - A) * (i / 25)) + (r - R);
        y = (R - r) * S(t) - D * S(q) + (B + (Y - B) * (i / 25));
        if (a) { c.moveTo(a, b); c.lineTo(x, y); }
        c.strokeStyle = "hsla(" + (U % 360) + ",100%,50%,0.75)";
        c.stroke();
        a = x; b = y;
      }
      U -= 0.5; A = X; B = Y;
    }, 16);
  };
  j.onkeydown = function (e) { a = b = 0; R += 0.05 }
  d.onmousemove({ pageX: 300, pageY: 290 })
}
</script>

<style>
body { margin:0; overflow:hidden }
div { padding:1px 5px; background:#333; position:absolute; z-index:2;
      color:#555; font-family:arial }
</style>

幾個關鍵技巧

globalCompositeOperation = "lighter" 是顏色會發光的秘密。它讓每次描邊採用「加色混合」,重疊處會越疊越亮,於是密集的線條自然呈現霓虹質感。

每幀鋪一層 rgba(0,0,0,0.02) 而非清空畫布。這是全篇最漂亮的一手。用 2% 不透明度的黑色反覆覆蓋,舊軌跡不會瞬間消失,而是緩慢淡出,拖尾就這樣出現了。若改成 clearRect,效果會完全消失。

用 25 段線條模擬曲線。內層 while (i--) 每次畫 25 段,i 從大到小遞減,形成由舊到新的插值序列。由於相鄰點以 moveTo/lineTo 串接,视觉上就是一條連續的平滑曲線。

D 控制環的大小並在 0 與 450 之間反覆呼吸。當 D 超過 450 或降到 0.1 時,bool 旗標翻轉,t 的累加方向隨之反轉,軌跡就會週期性地放大再收縮。

U 讓顏色隨水平位置變化。以 hsla 表示色相,滑鼠越往右色相偏移越大,並隨幀微幅遞減,因此尾跡的顏色是漸層而非單色。

setInterval 間隔 16 毫秒,約等於 60fps,與螢幕刷新率對齊。

值得注意的細節

程式中大量使用 C、S、W、H、A、B 這類單字母變數,這是為了壓縮體積;日後維護時還原成 cos、sin、canvasWidth 會好讀太多。另外 pageX / W * 999 >> 0 這串是「先歸一化到 0 到 999,再用位元位移取整數」的寫法,效果等同 Math.floor 但更短。

實務上要留意兩點:一是 canvas 沒有設定 CSS 尺寸時,width/height 屬性會等於實際像素,在高 DPI 螢幕上可能略顯模糊,正式環境建議再乘上 devicePixelRatio 並用 CSS 縮回。二是這種全螢幕高頻繪製對低階裝置負擔不小,記得搭配 prefers-reduced-motion 提供關閉選項。

快過年了,拿來妝點個人首頁倒是很合適。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved