canvas實現的跟隨滑鼠的彩色絲帶效果
分享一段程式碼例項,它實現了跟隨滑鼠的彩色絲帶效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> * { margin: 0; padding: 0; } html, body { height: 100%; } body { background: #131313; } canvas { position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); border: 10px solid #333; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas, context; var WIDTH = Math.min(800, document.body.offsetWidth); var HEIGHT = Math.min(600, document.body.offsetHeight); var NUM = 25; var lines = []; var RADIUS = 110; var RADIUS_SCALE = 1; var RADIUS_SCALE_MIN = 1; var RADIUS_SCALE_MAX = 1.5; var MouseX = window.innerWidth - WIDTH; var MouseY = window.innerHeight - HEIGHT; var mouseIsDown = false; window.onload = function() { canvas = document.getElementById('canvas'); canvas.width = WIDTH; canvas.height = HEIGHT; context = canvas.getContext("2d"); document.addEventListener('mousemove', documentMousemove, false); document.addEventListener('mousedown', documentMousedown, false); document.addEventListener('mouseup', documentMouseup, false); canvas.addEventListener('touchstar', canvasTouchStar, false); canvas.addEventListener('touchmove', canvasTouchMove, false); window.addEventListener('resize', resize, false); addLines(); setInterval(function() { render() }, 16) } function render() { if (mouseIsDown) { RADIUS_SCALE += (RADIUS_SCALE_MAX - RADIUS_SCALE) * 0.02; } else { RADIUS_SCALE -= (RADIUS_SCALE - RADIUS_SCALE_MIN) * 0.02; } RADIUS_SCALE = Math.min(RADIUS_SCALE, RADIUS_SCALE_MAX); context.fillStyle = 'rgba(0,0,0,0.05)'; context.fillRect(0, 0, WIDTH, HEIGHT); for (var i = 0; i < NUM; i++) { var line = lines[i]; var lp = { x: line.position.x, y: line.position.y }; line.angle += line.speed; line.shift.x += (MouseX - line.shift.x) * line.speed; line.shift.y += (MouseY - line.shift.y) * line.speed; line.position.x = line.shift.x + Math.cos(i + line.angle) * (line.orbit * RADIUS_SCALE); line.position.y = line.shift.y + Math.sin(i + line.angle) * (line.orbit * RADIUS_SCALE); line.position.x = Math.max(Math.min(line.position.x, WIDTH), 0); line.position.y = Math.max(Math.min(line.position.y, HEIGHT), 0); line.size += (line.targetSize - line.size) * 0.05; if (Math.round(line.size) == Math.round(line.targetSize)) { line.targetSize = 1 + Math.random() * 7; } context.beginPath(); context.fillStyle = line.fillColor; context.strokeStyle = line.fillColor; context.lineWidth = line.size; context.moveTo(lp.x, lp.y); context.lineTo(line.position.x, line.position.y); context.stroke(); context.arc(line.position.x, line.position.y, line.size / 2, 0, Math.PI * 2, true); context.fill(); } } function addLines() { for (var i = 0; i < NUM; i++) { var aLine = { position: { x: MouseX, y: MouseY }, shift: { x: MouseX, y: MouseY }, size: 1, angle: 0, speed: 0.01 + Math.random() * 0.04, targetSize: 1, fillColor: '#' + (Math.random() * 0x404040 + 0xaaaaaa | 0).toString(16), orbit: RADIUS * 0.5 + Math.random() * 0.5 * RADIUS } lines.push(aLine); } } function documentMousemove(event) { MouseX = event.clientX - (window.innerWidth - WIDTH) / 2; MouseY = event.clientY - (window.innerHeight - HEIGHT) / 2; } function documentMousedown() { mouseIsDown = true; } function documentMouseup() { mouseIsDown = false; } function canvasTouchStar(event) { if (event.touches.length == 1) { event.preventDefault(); MouseX = event.touches[0].pageX - (window.innerWidth - WIDTH) * .5; MouseY = event.touches[0].pageY - (window.innerHeight - HEIGHT) * .5; } } function canvasTouchMove(event) { if (event.touches.length == 1) { event.preventDefault(); MouseX = event.touches[0].pageX - (window.innerWidth - WIDTH) * .5; MouseY = event.touches[0].pageY - (window.innerHeight - HEIGHT) * .5; } } function resize() { WIDTH = Math.min(800, document.body.offsetWidth); HEIGHT = Math.min(600, document.body.offsetHeight); canvas.width = WIDTH; canvas.height = HEIGHT; } </script> </body> </html>
相關文章
- JavaScript、CSS實現滑鼠跟隨繁花效果JavaScriptCSS
- 不可思議的純 CSS 實現滑鼠跟隨效果CSS
- CSS mask 實現滑鼠跟隨鏤空效果CSS
- js實現彈出層滑鼠跟隨效果JS
- 【canvas】箭頭跟隨滑鼠移動的動畫原理Canvas動畫
- JavaScript跟隨滑鼠移動的圖片效果JavaScript
- JavaScript 跟隨滑鼠指標的粒子效果JavaScript指標
- jQuery跟隨滑鼠移動的彈出層效果jQuery
- 一側具有滑鼠跟隨效果的垂直導航選單
- jquery css3跟隨滑鼠晃動的圖片效果jQueryCSSS3
- 滑鼠指標一長串元素跟隨效果指標
- 滑鼠懸浮具有背景動畫跟隨效果的導航選單動畫
- canvas實現的按住滑鼠拖動能夠繪製文字效果Canvas
- canvas實現 漂亮的下雨效果Canvas
- js跟隨滑鼠移動的元素JS
- javascript元素滑鼠跟隨效果程式碼例項詳解JavaScript
- JavaScript跟隨滑鼠指標的粒子效果程式碼例項JavaScript指標
- 基於canvas繪製的一個跟隨滑鼠變幻的動態背景線條Canvas
- canvas實現的雪花飄落效果Canvas
- canvas實現的可愛小貓效果Canvas
- canvas實現波浪效果Canvas
- canvas實現的圓形時鐘效果Canvas
- canvas實現的圖片放大鏡效果Canvas
- jQuery隨滑鼠旋轉的圖形效果jQuery
- [unity3d]如何實現遊戲物件跟隨滑鼠方向移動Unity3D遊戲物件
- 四角游標跟隨滑鼠, 滑鼠落在哪個圖片上, 游標就跟隨到哪張圖片 (一種實現思路)
- JavaScript 元素跟隨滑鼠運動JavaScript
- 用Canvas畫一隻會跟著滑鼠走的小狗Canvas
- canvas實現的逐步成長的大樹效果Canvas
- css實現的滑鼠滑過星星高亮效果CSS
- canvas實現的旋轉太極圖效果Canvas
- canvas實現的絢麗的電子時鐘效果Canvas
- canvas圓形隨機漂浮效果Canvas隨機
- 拖動滾動條實現div跟隨效果程式碼例項
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- canvas實現的圓形走動鐘錶效果Canvas
- JS特效--跟隨滑鼠移動的日曆時鐘JS特效