国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript實現起伏的水波背景效果

2019-11-20 10:03:04
字體:
來源:轉載
供稿:網友

本文實例為大家分享了js水波背景效果的實現代碼,供大家參考,具體內容如下

效果圖:

實現代碼:

<!DOCTYPE html><html><head> <title>水波背景</title> <meta charset="gb2312" /> <style> html, body {width:100%; height:100%; padding:0; margin:0;} </style></head><body> <canvas id="canvas" style="position:absolute;top:0px;left:0px;z-index:1;"></canvas> <script type="text/javascript">  var canvas = document.getElementById('canvas');   var ctx = canvas.getContext('2d');   canvas.width = canvas.parentNode.offsetWidth;   canvas.height = canvas.parentNode.offsetHeight;  //如果瀏覽器支持requestAnimFrame則使用requestAnimFrame否則使用setTimeout   window.requestAnimFrame = (function(){   return window.requestAnimationFrame  ||     window.webkitRequestAnimationFrame ||     window.mozRequestAnimationFrame ||     function( callback ){      window.setTimeout(callback, 1000 / 60);     };   })();   // 波浪大小  var boHeight = canvas.height / 10;  var posHeight = canvas.height / 1.2;  //初始角度為0   var step = 0;   //定義三條不同波浪的顏色   var lines = ["rgba(0,222,255, 0.2)",       "rgba(157,192,249, 0.2)",       "rgba(0,168,255, 0.2)"];   function loop(){    ctx.clearRect(0,0,canvas.width,canvas.height);    step++;    //畫3個不同顏色的矩形    for(var j = lines.length - 1; j >= 0; j--) {     ctx.fillStyle = lines[j];     //每個矩形的角度都不同,每個之間相差45度     var angle = (step+j*50)*Math.PI/180;     var deltaHeight = Math.sin(angle) * boHeight;    var deltaHeightRight = Math.cos(angle) * boHeight;     ctx.beginPath();    ctx.moveTo(0, posHeight+deltaHeight);     ctx.bezierCurveTo(canvas.width/2, posHeight+deltaHeight-boHeight, canvas.width / 2, posHeight+deltaHeightRight-boHeight, canvas.width, posHeight+deltaHeightRight);     ctx.lineTo(canvas.width, canvas.height);     ctx.lineTo(0, canvas.height);     ctx.lineTo(0, posHeight+deltaHeight);     ctx.closePath();     ctx.fill();    }   requestAnimFrame(loop);  }   loop();  </script></body></html>

以上就是本文的全部內容,希望對大家學習javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永泰县| 咸阳市| 广西| 隆林| 富蕴县| 江华| 阿克苏市| 湄潭县| 彩票| 历史| 永川市| 黔江区| 大丰市| 城口县| 安宁市| 育儿| 铁力市| 义马市| 浙江省| 河西区| 亚东县| 衡水市| 舟山市| 刚察县| 大石桥市| 文安县| 萝北县| 诸城市| 怀宁县| 柯坪县| 隆化县| 高雄县| 常山县| 温宿县| 留坝县| 当涂县| 秦安县| 齐齐哈尔市| 岫岩| 克东县| 来凤县|