炫酷的滑鼠特效JS 原生程式碼
阿新 • • 發佈:2018-12-29
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>star</title> <script type="text/javascript"> window.onload = function () { C = Math.cos; // 快取數學物件 S = Math.sin; U = 0; w = window; j = document; d = j.getElementById("c"); 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; // 上一個位置.x b=0; // 上一個位置.y A = X, // 原始位置.x B = Y; // 原始位置.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; // decrease theta D -= 0.1; // decrease size } if(!bool) { t += g; // increase theta D += 0.1; // increase size } q = (R / r - 1) * t; // (see: http://en.wikipedia.org/wiki/Hypotrochoid) 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) { // draw once two points are set c.moveTo(a, b); c.lineTo(x, y) } c.strokeStyle = "hsla(" + (U % 360) + ",100%,50%,0.75)"; // c.stroke(); a = x; // set previous coord.x b = y; // set previous coord.y } U -= 0.5; // 增加色彩 A = X; // set original coord.x B = Y; // set original coord.y }, 16); } j.onkeydown = function(e) { a=b=0; R += 0.05 } d.onmousemove({pageX:300, pageY:290}) } </script> </head> <body style="margin:0px;padding:0px;width:100%;height:100%;overflow:hidden;"> <canvas id="c"></canvas> </body> </html>