canvas繪製小球漸隱漸現
分享一段程式碼例項,它實現了利用canvas繪製漸隱漸現小球的功能。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> *{ margin: 0; padding: 0; } html,body{ height:100%; width:100%; } #cvs{ position:absolute; top:0; display:block; background-color:#000; } </style> <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script> </head> <body> <canvas id = "cvs" width="1700" height="800"></canvas> <script type="text/javascript"> var canvas = $("#cvs"); var ctx = canvas.get(0).getContext("2d"); canvas.attr("width", $(window).get(0).innerWidth); canvas.attr("height", $(window).get(0).innerHeight); $(window).resize(resizeCanvas); function resizeCanvas() { canvas.attr("width", $(window).get(0).innerWidth); canvas.attr("height", $(window).get(0).innerHeight); }; resizeCanvas(); var canvasWidth = canvas.width(); var canvasHeight = canvas.height(); var Shape = function(p, x, y, radius) { this.p = p; this.x = x; this.y = y; this.radius = radius; }; var shapes = new Array(); for (var i = 0; i < 20; i++) { var p = 0.7; var x = Math.random() * canvasWidth; var y = Math.random() * canvasHeight / 3 + canvasHeight / 3; var radius = Math.random() * 10; shapes.push(new Shape(p, x, y, radius)); }; var playAnimation = true; function animate() { for (i = 0; i < 20; i++) { var a = shapes[i]; a.p -= 0.005; a.radius += .5; ctx.beginPath(); ctx.fillStyle = 'rgba(0, 0, 0,0.1)'; ctx.shadowBlur = 50; ctx.shadowColor = 'rgba(178, 0, 255, ' + a.p + ')'; ctx.arc(a.x, a.y, a.radius, 0, Math.PI * 2, false); ctx.fill(); if (a.p < 0) { ctx.clearRect(0, 0, canvasWidth, canvasHeight); a.x = Math.random() * canvasWidth; a.y = Math.random() * canvasHeight / 3 + canvasHeight / 3; a.radius = Math.random() * 10; a.p = 0.7; }; } if (playAnimation) { setTimeout(animate, 33); }; }; animate(); </script> </body> </html>
相關文章
- canvas繪製帶有漸變效果的直線Canvas
- Dreamweaver網頁元素怎麼製作漸隱漸現效果教程網頁
- Qt實現控制元件的漸隱漸現動效QT控制元件
- CSS3圓形漸隱漸現迴圈出現CSSS3
- iOS 繪製漸變·基礎篇iOS
- 純CSS漸變繪製 Chrome 圖示CSSChrome
- css radial-gradient繪製漸變背景CSS
- canvas createRadialGradient() 徑向漸變Canvas
- canvas具有漸變效果的矩形Canvas
- echarts 繪製圓形進度條帶漸變色Echarts
- 漸變色進度條的兩種繪製方案
- 使用canvas來完成線性漸變和徑向漸變的功能Canvas
- 短視訊直播原始碼,實現頂部導航欄背景圖片漸隱漸現效果原始碼
- canvas錐形漸變進度條Canvas
- 她漸行漸遠
- canvas實現手動繪製矩形Canvas
- Dreamweaver製作滑鼠經過圖片漸漸變暗效果教程
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- canvas 繪製矩形Canvas
- canvas小球亂撞Canvas
- canvas小球碰壁效果Canvas
- canvas 繪製線條Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas繪製直線Canvas
- canvas lineWidth 繪製原理Canvas
- canvas lineWidth繪製原理Canvas
- 圖形處理:給 Canvas 文字填充線性漸變Canvas
- canvas小球擺動效果Canvas
- Canvas 繪製雷達圖Canvas
- canvas 繪製圓角矩形Canvas
- canvas 繪製雙線技巧Canvas
- canvas繪製矩形框Canvas
- canvas繪製風車效果Canvas
- canvas 繪製矩形缺角Canvas
- canvas 繪製文字詳解Canvas
- canvas繪製笑臉表情Canvas