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

首頁 > 編程 > JavaScript > 正文

JavaScript+html5 canvas制作的百花齊放效果完整實例

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

本文實例講述了JavaScript+html5 canvas制作的百花齊放效果。分享給大家供大家參考,具體如下:

運行效果截圖如下:

具體代碼如下:

<!DOCTYPE html><html> <head>  <title>demo</title>  <style type="text/css">   body {    margin:0; padding:0;    }   #canvas {    border:5px solid gray; box-shadow:0 0 15px 15px #494949 inset;    margin-top:50px; margin-left:200px;   }  </style> </head> <body >  <canvas id="canvas" width="1000px" height="500px"></canvas>  <script type="text/javascript">   var dyl = {};   dyl.canvas = document.getElementById("canvas");   dyl.ctx = dyl.canvas.getContext("2d");   dyl.runTime = 0;   dyl.colorList = "01234567890ABCDEFabcdef".split("");   dyl.colorListLength = dyl.colorList.length;   dyl.arcList = null;   /**   * 得到16進制隨機顏色值   */   dyl.getColor = function() {    var color = "#";    for(var i=0; i<6; i++) {     color += dyl.colorList[Math.floor(Math.random()*dyl.colorListLength)];    }    return color;   };   /**   * 一個隨機角度,隨機初始速度的斜拋對象   */   var Arc = function(i) {    // 設置自有屬性    this.v = Math.round(Math.random()*100)+50;    this.angle = Math.round(Math.random()*145) + 45;    this.startTime = +new Date();    this._angle = this.angle/180*Math.PI;    this.v_x = this.v*Math.cos(this._angle);    this.v_y_start = this.v*Math.sin(this._angle);    this.color = dyl.getColor();    this.x = 500;    this.g = 250;    this.y = 490;    this.index = i;    var _self = this;    this.run = function() {     var endTime = +new Date();     var timeSpan = (endTime - _self.startTime)/1000;     var v_y_now = _self.v_y_start - 1/2*_self.g*Math.pow(timeSpan, 2);     _self.x = _self.x +_self.v_x * timeSpan;     _self.y = _self.y - (_self.v_y_start * timeSpan - 1/2*_self.g*Math.pow(timeSpan, 2));     return this;    };    return this;   };   /**   * 全局繪制圖像   */   dyl.draw = function() {    var arcList = dyl.arcList;    var ctx = dyl.ctx;    dyl.runTime++;    for(var i=0, length=arcList.length; i<length; i++) {     var arc = arcList[i];     if(!arc) {      continue;     }     arc.run();     ctx.save();     ctx.beginPath();     ctx.fillStyle = arc.color;     ctx.arc(arc.x, arc.y, 2, 0, Math.PI*2);     ctx.fill();     ctx.closePath();     ctx.restore();    }    console.log(dyl.runTime);    if(dyl.runTime >= 25) {     setTimeout(dyl.init, 1050);    } else {     setTimeout(dyl.draw, 20);    }   };   /**   * 初始化整個事件   */   dyl.init = function() {    dyl.ctx.clearRect(0, 0, 1000, 500);    dyl.arcList = [];    dyl.runTime = 0;    for(var i=0; i<100; i++) {     dyl.arcList.push(new Arc(i));    }    dyl.draw();   };   dyl.init();  </script> </body></html>

更多關于js特效相關內容感興趣的讀者可查看本站專題:《jQuery動畫與特效用法總結》及《jQuery常見經典特效匯總

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 南城县| 慈利县| 马鞍山市| 榆中县| 新竹市| 乌兰浩特市| 沧源| 吴川市| 彰武县| 西乌珠穆沁旗| 大关县| 长丰县| 辉县市| 洞口县| 宁国市| 聂荣县| 措勤县| 泰来县| 容城县| 连山| 西峡县| 交口县| 嘉禾县| 桦川县| 南丰县| 河西区| 梁山县| 临江市| 杭州市| 陆川县| 聂荣县| 高清| 青田县| 盘锦市| 金昌市| 南宫市| 青河县| 延津县| 五原县| 临武县|