CSS如何實現波浪效果

CSS如何實現波浪效果

程序員們都知道使用純 CSS 實現波浪效果是十分困難的。

CSS 實現波浪效果

實現波浪的曲線需要藉助貝塞爾曲線。而使用純 CSS 的方式實現貝塞爾曲線暫時是沒有很好的方法。
當然,藉助其他力量(SVG、CANVAS),是可以很輕鬆的完成所謂的波浪效果的。

我們先來看看非 CSS 方式實現的波浪效果

SVG 實現波浪效果

藉助 SVG ,是很容易畫出三次貝塞爾曲線的。

SVG 實現波浪效果

<svg width="200px" height="200px" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <text class="liquidFillGaugeText" text-anchor="middle" font-size="42px" transform="translate(100,120)" style="fill: #000">50.0%</text>
    <!-- Wave -->
    <g id="wave">
    <path id="wave-2" fill="rgba(154, 205, 50, .8)" d="M 0 100 C 133.633 85.12 51.54 116.327 200 100 A 95 95 0 0 1 0 100 Z">
    <animate dur="5s" repeatCount="indefinite" attributeName="d" attributeType="XML" values="M0 100 C90 28, 92 179, 200 100 A95 95 0 0 1 0 100 Z;
    M0 100 C145 100, 41 100, 200 100 A95 95 0 0 1 0 100 Z;
    M0 100 C90 28, 92 179, 200 100 A95 95 0 0 1 0 100 Z"></animate>
    </path>
    </g>
    <circle cx="100" cy="100" r="80" stroke-width="10" stroke="white" fill="transparent"></circle>
    <circle cx="100" cy="100" r="90" stroke-width="20" stroke="yellowgreen" fill="none" class="percentage-pie-svg"></circle></svg>

SVG

Copy

可以複製上方代碼至在線coding, 查看效果。

畫出三次貝塞爾曲線的核心在於 這一段。感興趣的可以自行去研究研究。

canvas 實現波浪效果

使用 canvas 實現波浪效果的原理與 SVG 一樣,都是利用路徑繪製出三次貝塞爾曲線並賦予動畫效果。

canvas 實現波浪效果

$(function() {
    let canvas = $("canvas");
    let ctx = canvas[0].getContext('2d');
    let radians = (Math.PI / 180) * 180;
    let startTime = Date.now();
    let time = 2000;
    let clockwise = 1;
    let cp1x, cp1y, cp2x, cp2y;

    // 初始狀態
    // ctx.bezierCurveTo(90, 28, 92, 179, 200, 100);
    // 末尾狀態
    // ctx.bezierCurveTo(145, 100, 41, 100, 200, 100);

    requestAnimationFrame(function waveDraw() { 
        let t = Math.min(1.0, (Date.now() - startTime) / time);

        if(clockwise) {
            cp1x = 90 + (55 * t);
            cp1y = 28 + (72 * t);
            cp2x = 92 - (51 * t);
            cp2y = 179 - (79 * t);
        } else {
            cp1x = 145 - (55 * t);
            cp1y = 100 - (72 * t);
            cp2x = 41 + (51 * t);
            cp2y = 100 + (79 * t);
        }

        ctx.clearRect(0, 0, 200, 200); 
        ctx.beginPath();
        ctx.moveTo(0, 100);
        // 繪製三次貝塞爾曲線
        ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, 200, 100);
        // 繪製圓弧
        ctx.arc(100, 100, 100, 0, radians, 0);
        ctx.fillStyle = "rgba(154, 205, 50, .8)";
        ctx.fill();
        ctx.save(); 

        if( t == 1 ) {
            startTime = Date.now();
            clockwise = !clockwise;
        }

        requestAnimationFrame(waveDraw);
    });})

JavaScript

Copy

可以複製上方代碼至在線coding, 查看效果。

主要是利用了動態繪製 ctx.bezierCurveTo() 三次貝塞爾曲線實現波浪的運動效果,感興趣的可以自行研究。

CSS實現波浪效果

最開始不是說css不能實現嗎?是,我們沒有辦法直接繪製出三次貝塞爾曲線,但是我們可以利用一些討巧的方法,模擬達到波浪運動時的效果,下面來看看這種方法。

原理

原理十分簡單,我們都知道,一個正方形,給它添加 border-radius: 50%,將會得到一個圓形。

CSS實現波浪效果

width: 240px;height: 240px;background: #f13f84;border-radius: 50%;

CSS

Copy

好的,如果 border-radius 沒到 50%,但是接近 50% ,我們會得到一個這樣的圖形(注意邊角,整個圖形給人的感覺是有點圓,卻不是很圓。)

CSS實現波浪效果

width: 240px;height: 240px;background: #f13f84;border-radius: 40%;

CSS

Copy

好的,那整這麼個圖形又有什麼用?還能變出波浪來不成?

我們讓上面這個圖形滾動起來(rotate) ,看看效果:

CSS實現波浪效果

@keyframes rotate{
    from{transform: rotate(0deg)}
    to{transform: rotate(359deg)}}.ripple{
    width: 240px;
    height: 240px;
    background: #f13f84;
    border-radius: 40%;
    animation: rotate 3s linear infinite;}

CSS

Copy

可能很多人看到這裏還沒懂旋轉起來的意圖,仔細盯着一邊看,是會有類似波浪的起伏效果的。

而我們的目的,就是要藉助這個動態變換的起伏動畫,模擬製造出類似波浪的效果。

實現

當然,這裏看到是全景實現圖,所以感覺並不明顯,OK,讓我們用一個個例子看看具體實現起來能達到什麼樣的效果。

我們利用上面原理可以做到的一種波浪運動背景效果圖:

CSS實現波浪效果

後面漂浮的波浪效果,其實就是利用了上面的 border-radius: 40% 的橢圓形,只是放大了很多倍,視野之外的圖形都 是隱藏的 ,只留下了一條邊的視野,並且增加了一些相應的transform 變換。

可能有部分同學,還存在疑問,OK,那我們把上面的效果隱藏部分顯示處理,將視野之外的動畫也補齊,那麼其實生成波浪的原理是這樣的:

CSS實現波浪效果

圖中的紅色框就是我們實際的視野範圍。

值得注意的是,要看到,這裏我們生成波浪,並不是利用旋轉的橢圓本身,而是利用它去切割背景,產生波浪的效果。

完整代碼如下:

<!DOCTYPE html><html>
  <head>
    <title>Web秀</title>
    <meta name="name" content="Javan" />
    <style>
    body,html{
        width: 100%;
        height: 100%;
    }
    @keyframes rotate{
        from{transform: rotate(0deg)}
        to{transform: rotate(359deg)}
    }
    .water{
        background: #86cbff;
        margin-top: 200px;
        position: relative;
        height: 350px;
        width: 300px;
        margin-left: 200px;
    }
    .ripple{
        position: absolute;
        width: 500px;
        height: 500px;
        background: #fff;
        border-radius: 40%;
        animation: rotate 5s linear infinite;
        margin-top: -200px;
        margin-left: -100px;
    }    </style>
  </head>
  <body>
    <div class="water">
        <div class="ripple"></div>
    </div>
  </body></html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章