本篇文章給大家介紹一下使用canvas實現(xiàn)雪花隨機動態(tài)飄落效果的方法,文中示例代碼介紹的非常詳細。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),泗縣企業(yè)網(wǎng)站建設(shè),泗縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,泗縣網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,泗縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

用canvas實現(xiàn)雪花隨機動態(tài)飄落,代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body{
margin: 0;
padding: 0;
}
canvas{
background: #000;
}
</style>
<body>
<canvas id = "snow">
</canvas>
<script>
var canvas = document.getElementById('snow');
var context = canvas.getContext('2d');
// 獲得可視區(qū)的寬高
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function snow(){
context.save();
// 開啟路徑
context.beginPath();
// 移動畫布,確定雪花終點為中心點
context.translate(100,100);
//起點
context.moveTo(-20,0);
// 終點
context.lineTo(20,0);
// 線變成白色
context.strokeStyle = '#fff';
// 線變粗
context.lineWidth = 10;
// 線變圓頭
context.lineCap = 'round';
context.stroke();
// 角度轉(zhuǎn)弧度
var disX = Math.sin(30*Math.PI/180)*20;
var disY = Math.sin(60*Math.PI/180)*20;
// 畫第二條線,左下到右上的線
context.moveTo(-disX,disY);
context.lineTo(disX,-disY);
// 畫第三條線
context.moveTo(-disX,-disY);
context.lineTo(disX,disY);
context.stroke();
context.restore();
}
// snow();
//生成雪花的構(gòu)造函數(shù)
function Snow(x,y,scale,rotate,speedX,speedY,speedR){
this.x = x;
this.y = y;
this.scale = scale;
this.rotate = rotate;
this.speedX = speedX;
this.speedY = speedY;
this.speedR = speedR;
// 渲染雪花
this.render = function(){
context.save();
context.beginPath();
context.translate(this.x,this.y);
context.scale(this.scale,this.scale);
context.rotate(this.rotate*Math.PI/180);
context.moveTo(-20,0);
context.lineTo(20,0);
context.strokeStyle = '#fff';
context.lineWidth = 10;
context.lineCap = 'round';
context.stroke();
var disX = Math.sin(30*Math.PI/180)*20;
var disY = Math.sin(60*Math.PI/180)*20;
context.moveTo(-disX,disY);
context.lineTo(disX,-disY);
context.moveTo(-disX,-disY);
context.lineTo(disX,disY);
context.stroke();
context.restore();
}
}
// var snow = new Snow(50,50,1,10,10,10,10);
// snow.render();
// 存儲所有生成的雪花
var snowArray = [];
// 生成雪花
function init(){
var len = 100;
for(var i = 0;i<len;i++){
var x = Math.random()*canvas.width;
var scale = Math.random()+0.5;
var rotate = Math.random()*60;
var speedX = Math.random()+1
var speedY = Math.random()+5;
var speedR = Math.random()*4+2;
// var snow = new Snow(x,0,scale,rotate,speedX,speedY,speedR);
// snow.render();
(function(x,y,scale,rotate,speedX,speedY,speedR){
setTimeout(function(){
var snow = new Snow(x,0,scale,rotate,speedX,speedY,speedR);
snow.render();
snowArray.push(snow);
},Math.random()*8000);
})(x,0,scale,rotate,speedX,speedY,speedR);
}snowing();
}init();
// 動起來
function snowing(){
setInterval(function(){
//先清除
context.clearRect(0,0,canvas.width,canvas.height);
for(var i = 0;i < snowArray.length;i++){
snowArray[i].x = (snowArray[i].x+snowArray[i].speedX)%canvas.width;
snowArray[i].y = (snowArray[i].y+snowArray[i].speedY)%canvas.height;
snowArray[i].rotate = (snowArray[i].rotate+snowArray[i].speedR)%60;
snowArray[i].render();
}
},30);
}
/**
* sin60 = 對邊/斜邊 => 對邊 = sin60*斜邊 => y=sin60*半徑(r);
*/
</script>
</body>
</html>動態(tài)效果圖:

以上就是canvas實現(xiàn)雪花隨機動態(tài)飄落效果(代碼示例)的詳細內(nèi)容,更多請關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!
標(biāo)題名稱:如何用canvas實現(xiàn)動態(tài)雪花飄落特效
標(biāo)題URL:http://www.chinadenli.net/article12/iggigc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈、手機網(wǎng)站建設(shè)、、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)