DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5特效代碼
1KB JavaScript 就能跑:canvas 畫出栩栩如生的星系
編輯:HTML5特效代碼     

跨年前後想來點炫的?這段不到 1KB 的 JavaScript,用 canvas 畫出一個會隨滑鼠游標流動、變色、扭曲的星系。程式碼被壓縮到極致,是很好的「用最少字元做最多事」的教材。

效果網址:https:///keleyi/phtml/html5/5.htm

完整程式碼

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>栩栩如生的星系(1020 bytes)</title><base target="_blank" />
<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 = 0; b = 0; 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}a{color:#ccf}a:hover{color:red}</style>

</head>
<body>
<div><a href="https:///">首頁</a> <a href="https:///a/bjad/8e9jd0u5.htm">原文</a> <a href="https:///keleyi/phtml/">特效庫</a> 1kb 的 Javascript 能幹什麼?</div>
<div id="text"></div>
<canvas id="keleyi_com"></canvas>
</body>
</html>

程式碼怎麼運作

  • 把常用函式縮成單字母:C = Math.cos; S = Math.sin; 後面全部用 C()、S() 呼叫。這是全篇最有效率的縮短技巧。
  • globalCompositeOperation = "lighter":把繪製模式設為「加色混合」,多條光線交會處會自然變亮,這是星系發光感的來源。
  • 半透明黑色當拖尾:每幀先用 rgba(0,0,0,0.02) 蓋一層極淡的黑,而不是 fillRect 全清。上一幀的內容因此不會完全消失,形成長長的光軌殘影。
  • 游標位置決定形狀:R = e.pageX / W、r = e.pageY / H 把游標座標正規化到 0~1,再參與 q = (R / r - 1) * t 的計算,游標移動時星系的旋臂張角就隨之扭曲。
  • 色相隨水平位置改變:U = e.pageX / H * 360,再用 hsla(U % 360, 100%, 50%, 0.75) 當 strokeStyle,畫面顏色便隨游標水平移動而流轉。
  • while (i--) 畫 25 條線:每一幀畫 25 條路徑,每條路徑的中間點都插入游標位置,讓線條被「拉」向游標,做出引力扭曲的錯覺。
  • bool 控制呼吸節奏:當 D 超過 450 或降到 0.1 時反轉方向,讓星系週期性地收縮與膨脹。
  • setInterval 的 16ms:約等於 60fps,是動畫的標準間隔。

最後那行 d.onmousemove({ pageX: 300, pageY: 290 }) 是個小技巧:直接偽造一個事件物件呼叫 handler,讓畫面在載入時不需要使用者移動滑鼠就能先跑起來。

如果要用在正式專案,建議把版號式的全域變數改寫成 const/let 並包進 IIFE 或模組作用域,同時把 setInterval 換成 requestAnimationFrame,並在頁面隱藏時暫停繪製以節省電力。

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