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網頁元素怎麼製作漸隱漸現效果教程網頁
- jquery漸隱漸現程式碼例項jQuery
- iOS 繪製漸變·基礎篇iOS
- Qt實現控制元件的漸隱漸現動效QT控制元件
- CSS3圓形漸隱漸現迴圈出現CSSS3
- 用VC 實現圖象漸顯和漸隱 (轉)
- 純CSS漸變繪製 Chrome 圖示CSSChrome
- 點選實現元素的漸隱或者漸現程式碼例項
- css3迴圈漸隱漸現效果程式碼例項CSSS3
- css3網頁底部漸隱漸現的箭頭效果CSSS3網頁
- css3實現的文字顏色漸變和漸隱效果CSSS3
- 用Visual C++實現圖象漸顯和漸隱 (轉)C++
- canvas繪製滑鼠懸浮可以變大的立體小球Canvas
- css radial-gradient繪製漸變背景CSS
- 外掛製作--------過NP技術漸漸
- canvas具有漸變效果的矩形Canvas
- 使用canvas來完成線性漸變和徑向漸變的功能Canvas
- js實現的加分後出現漸隱提示效果JS
- 短視訊直播原始碼,實現頂部導航欄背景圖片漸隱漸現效果原始碼
- canvas錐形漸變進度條Canvas
- canvas createRadialGradient() 徑向漸變Canvas
- canvas實現文字線性漸變效果程式碼例項Canvas
- canvas實現手動繪製矩形Canvas
- canvas徑向漸變程式碼例項Canvas
- canvas線性漸變程式碼例項Canvas
- canvas 繪製矩形Canvas
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- 她漸行漸遠
- Dreamweaver製作滑鼠經過圖片漸漸變暗效果教程
- html5中的canvas線性漸變HTMLCanvas
- canvas 繪製線條Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas繪製直線Canvas
- canvas小球碰壁效果Canvas
- canvas小球亂撞Canvas