溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Canvas怎么繪制一個下雨動畫

發(fā)布時間:2021-04-09 16:05:08 來源:億速云 閱讀:202 作者:Leah 欄目:web開發(fā)

今天就跟大家聊聊有關(guān)使用Canvas怎么繪制一個下雨動畫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

使用方法如下:

function anim() {
    ctx.fillStyle = clearColor;
    ctx.fillRect(0,0,w,h);
    for(var i in drops){
        drops[i].draw();
    }
    requestAnimationFrame(anim);
}

一般情況下優(yōu)先使用requestAnimationFrame能保持動畫繪制的頻率和瀏覽器重繪的頻率一致。不幸的是requestAnimationFrame的兼容性還不是很好。IE9以下和addroid 4.3以下好像不支持這個屬性。不支持的瀏覽器要用setInterval或setTimeout做兼容。

雨滴下落效果

首先來講講雨滴下落的效果如何制作。雨滴其實是一個長方形,然后加殘影。殘影的繪制可以說是雨滴下落的關(guān)鍵。殘影是通過在前進(jìn)的方向每一幀都繪制一個半透明的背景和一個長方形,然后前面繪制的圖形疊加產(chǎn)生的效果。由于前進(jìn)方向的圖形最后繪制,所以顯得明亮,后面的圖形疊加的比較多,所以視覺上減弱。整體看起來后面的就像殘影。這里繪制具有透明度背景是關(guān)鍵,否則產(chǎn)生不了疊加效果。

那么來繪制個雨滴看看。首先準(zhǔn)備一個畫板:

html代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>霓虹雨</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <style type="text/css">
        .bg {
            background: #000;
            overflow: hidden;
        }
    </style>

</head>
<body class="bg">
<canvas id="canvas-club"></canvas>
<script type="text/javascript" src="raindrop.js"></script>
</body>
</html>

我在js文件里繪制動畫(raindrop.js),代碼如下:

var c = document.getElementById("canvas-club");
var ctx = c.getContext("2d");//獲取canvas上下文
var w = c.width = window.innerWidth;
var h = c.height = window.innerHeight;//設(shè)置canvas寬、高
var clearColor = 'rgba(0, 0, 0, .1)';//畫板背景,注意最后的透明度0.1 這是產(chǎn)生疊加效果的基礎(chǔ)

function random(min, max) {
    return Math.random() * (max - min) + min;
}

function RainDrop(){}
//雨滴對象 這是繪制雨滴動畫的關(guān)鍵
RainDrop.prototype = {
    init:function(){
        this.x =  random(0, w);//雨滴的位置x
        this.y = 0;//雨滴的位置y
        this.color = 'hsl(180, 100%, 50%)';//雨滴顏色 長方形的填充色
        this.vy = random(4, 5);//雨滴下落速度
        this.hit = random(h * .8, h * .9);//下落的最大值
        this.size = 2;//長方形寬度
    },
    draw:function(){
        if (this.y < this.hit) {
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y, this.size, this.size * 5);//繪制長方形,通過多次疊加長方形,形成雨滴下落效果
        }
        this.update();//更新位置
    },
    update:function(){
        if(this.y < this.hit){
            this.y += this.vy;//未達(dá)到底部,增加雨滴y坐標(biāo)
        }else{
            this.init();
        }
    }
};

function resize(){
    w = c.width = window.innerWidth;
    h = c.height = window.innerHeight;
}

//初始化一個雨滴
var r = new RainDrop();
r.init();

function anim() {
    ctx.fillStyle = clearColor;//每一幀都填充背景色
    ctx.fillRect(0,0,w,h);//填充背景色,注意不要用clearRect,否則會清空前面的雨滴,導(dǎo)致不能產(chǎn)生疊加的效果
    r.draw();//繪制雨滴
    requestAnimationFrame(anim);//控制動畫幀
}

window.addEventListener("resize", resize);
//啟動動畫
anim();

漣漪效果

接著來繪制漣漪效果。與繪制雨滴的方式類似,也是通過具有透明度的背景來疊加前面的圖像產(chǎn)生內(nèi)陰影的效果。

代碼如下(rippling.js):

var c = document.getElementById("canvas-club");
var ctx = c.getContext("2d");//獲取canvas上下文
var w = c.width = window.innerWidth;
var h = c.height = window.innerHeight;//設(shè)置canvas寬、高
var clearColor = 'rgba(0, 0, 0, .1)';//畫板背景,注意最后的透明度0.1 這是產(chǎn)生疊加效果的基礎(chǔ)

function random(min, max) {
    return Math.random() * (max - min) + min;
}

function Rippling(){}
//漣漪對象 這是漣漪動畫的主要部分
Rippling.prototype = {
    init:function(){
        this.x = random(0,w);//漣漪x坐標(biāo)
        this.y = random(h * .8, h * .9);//漣漪y坐標(biāo)
        this.w = 2;//橢圓形漣漪寬
        this.h = 1;//橢圓漣漪高
        this.vw = 3;//寬度增長速度
        this.vh = 1;//高度增長速度
        this.a = 1;//透明度
        this.va = .96;//漣漪消失的漸變速度
    },
    draw:function(){
        ctx.beginPath();
        ctx.moveTo(this.x, this.y - this.h / 2);
        //繪制右弧線
        ctx.bezierCurveTo(
            this.x + this.w / 2, this.y - this.h / 2,
            this.x + this.w / 2, this.y + this.h / 2,
            this.x, this.y + this.h / 2);
        //繪制左弧線
        ctx.bezierCurveTo(
            this.x - this.w / 2, this.y + this.h / 2,
            this.x - this.w / 2, this.y - this.h / 2,
            this.x, this.y - this.h / 2);
        
        ctx.strokeStyle = 'hsla(180, 100%, 50%, '+this.a+')';
        ctx.stroke();
        ctx.closePath();
        this.update();//更新坐標(biāo)
    },
    update:function(){
        if(this.a > .03){
            this.w += this.vw;//寬度增長
            this.h += this.vh;//高度增長
            if(this.w > 100){
                this.a *= this.va;//當(dāng)寬度超過100,漣漪逐漸變淡消失
                this.vw *= .98;//寬度增長變緩慢
                this.vh *= .98;//高度增長變緩慢
            }
        } else {
            this.init();
        }

    }
};

function resize(){
    w = c.width = window.innerWidth;
    h = c.height = window.innerHeight;
}

//初始化一個漣漪
var r = new Rippling();
r.init();

function anim() {
    ctx.fillStyle = clearColor;
    ctx.fillRect(0,0,w,h);
    r.draw();
    requestAnimationFrame(anim);
}

window.addEventListener("resize", resize);
//啟動動畫
anim();

看完上述內(nèi)容,你們對使用Canvas怎么繪制一個下雨動畫有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI