成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

萬圣節(jié)動畫-canvas像素點

tainzhi / 857人閱讀

摘要:萬圣節(jié)到了,寫一個小例子了解一下畫圖方法,可以實現(xiàn)一些有趣的效果,動畫實現(xiàn)。移動路徑方法把路徑移動到畫布中指定點,不創(chuàng)建線條。實現(xiàn)初始畫布顯示文字萬圣節(jié)快樂閃電打雷反轉(zhuǎn)畫布重置畫布總結(jié)萬圣節(jié)快樂

萬圣節(jié)到了,寫一個小例子了解一下canvas畫圖方法,canvas可以實現(xiàn)一些有趣的效果,動畫實現(xiàn)。以一個簡單的頁面實現(xiàn)了解一下基礎(chǔ)的畫圖方法。
原文鏈接

canvas可以實現(xiàn)一些有趣的效果,動畫實現(xiàn)。以一個簡單的頁面實現(xiàn)了解一下基礎(chǔ)的畫圖方法。

效果

萬圣節(jié)快樂 ^_^

預(yù)備知識
let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");

## 開始路徑

context.beginPath();

beginPath()方法在畫布上開始一條繪制路徑,或重置當(dāng)前的路徑。

移動路徑
context.moveTo();

moveTo()方法把路徑移動到畫布中指定點,不創(chuàng)建線條。

添加線條
context.lineTo();

lineTo()方法添加一個新點,在畫布中創(chuàng)建該點到指定點的線條。

畫圖drawImage
context.drawImage(image,x,y);

drawImage()方法可以在畫布上繪制圖像、畫布或視頻,也可以繪制圖像的某些部分,增加/減少圖像的尺寸。

獲取像素數(shù)據(jù)
context.getImageData(x,y,width,height);

getImageData()方法可以獲取畫布imageData對象,該對象指定了矩形的像素數(shù)據(jù)。

在imageData對象中每個像素都存在rgba值,以數(shù)組形式存儲在data屬性中。

放回像素數(shù)據(jù)
context.putImageData(imageData,x,y);

putImageData()方法將獲取的圖像數(shù)據(jù)放回到畫布上。

實現(xiàn) html


## css

html,
body,
canvas {
    width: 100%;
    height: 100%;
    margin: 0;
}

.switch {
    position: absolute;
    top: 70%;
    right: 10%;
    width: 50px;
    height: 50px;
    border-radius: 50px;
    outline: none;
    cursor: pointer;
    animation: switch-animate alternate infinite ease 1s 0s;
}

@keyframes switch-animate {
    from {
        box-shadow: 0 0 30px #ece9c5;
    }

    to {
        box-shadow: 0 0 100px #eae5a7;
    }
}    

## js

(function () {

    class Halloween {
        constructor(id) {
            this.canvas = document.getElementById(id);
            this.ctx = this.canvas.getContext("2d");
            this.w = this.canvas.width;
            this.h = this.canvas.height;
            this.data = [];
        }

        //初始畫布
        initDraw(img) {
            this.w = this.canvas.width = img.width;
            this.h = this.canvas.height = img.height;
            this.ctx.drawImage(img, 0, 0);
            this.data = this.ctx.getImageData(0, 0, this.w, this.h);
        }

        //顯示文字
        drawText() {
            this.ctx.font = "60px Verdana";
            this.ctx.fillStyle = "#ffffff";
            this.ctx.fillText("萬圣節(jié)快樂", 350, 280);
        }

        //閃電
        lightning() {
            let ctx = this.ctx;
            ctx.strokeStyle = "#fff";
            ctx.lineWidth = 2;
            ctx.beginPath();
            ctx.moveTo(800, 10);
            ctx.lineTo(600, 100);
            ctx.lineTo(500, 200);
            ctx.stroke();
            ctx.beginPath();
            ctx.moveTo(600, 100);
            ctx.lineTo(650, 170);
            ctx.stroke()
        }

        //打雷
        thunder() {
            let timer = Math.floor(800 * Math.random());
            this.reverse();
            this.lightning();
            this.drawText();
            setTimeout(() => {
                this.reset();
            }, timer);
        }

        //反轉(zhuǎn)畫布
        reverse() {
            let imgData = this.ctx.getImageData(0, 0, this.w, this.h);
            for (var i = 0, len = imgData.data.length; i < len; i += 4) {
                imgData.data[i] = 255 - imgData.data[i];
                imgData.data[i + 1] = 255 - imgData.data[i + 1];
                imgData.data[i + 2] = 255 - imgData.data[i + 2];
                imgData.data[i + 3] = 255;
            }
            this.ctx.putImageData(imgData, 0, 0);
        }

        //重置畫布
        reset() {
            this.ctx.putImageData(this.data, 0, 0);
        }
    }
    let halloween = new Halloween("canvas");
    let canvas = document.getElementById("canvas");
    let ctx = canvas.getContext("2d");
    let img = new Image();
    img.src = "/images/halloween.png";
    img.onload = () => {
        halloween.initDraw(img);
    }

    document.getElementById("click").onclick = () => {
        halloween.thunder();
    }

})();

# 總結(jié)

萬圣節(jié)快樂?

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/92011.html

相關(guān)文章

  • JavaScript中的圖片處理與合成(四)

    摘要:算法性能提升圖片算法處理實質(zhì)原理其實是遍歷像素點,對像素點的值進(jìn)行改造。而像素點的數(shù)量與圖片的大小尺寸成正向指數(shù)級增長,因此適當(dāng)?shù)目s放圖片源后再去處理,對性能的提升十分巨大。 引言: 本系列現(xiàn)在構(gòu)思成以下4個部分: 基礎(chǔ)類型圖片處理技術(shù)之縮放、裁剪與旋轉(zhuǎn)(傳送門); 基礎(chǔ)類型圖片處理技術(shù)之圖片合成(傳送門); 基礎(chǔ)類型圖片處理技術(shù)之文字合成(傳送門); 算法類型圖片處理技術(shù)(傳送門)...

    Coding01 評論0 收藏0
  • 【30分鐘學(xué)完】canvas動畫|游戲基礎(chǔ)(extra1-1):美圖我也行

    摘要:前言本文是接續(xù)系列教程的,主要是介紹顏色系統(tǒng)在中的應(yīng)用。本來是與一起成文的,因為莫名其妙的字?jǐn)?shù)限制只能分割放送了。提供可以獲取畫布上任何一個像素,并可以自由的操作他們。繪制指定的位置繪制對象的內(nèi)容。 前言 本文是接續(xù)系列教程的extra1,主要是介紹顏色系統(tǒng)在canvas中的應(yīng)用。 本來是與extra1一起成文的,因為segmentfault莫名其妙的字?jǐn)?shù)限制bug只能分割放送了。 ...

    G9YH 評論0 收藏0
  • 【30分鐘學(xué)完】canvas動畫|游戲基礎(chǔ)(extra1-1):美圖我也行

    摘要:前言本文是接續(xù)系列教程的,主要是介紹顏色系統(tǒng)在中的應(yīng)用。本來是與一起成文的,因為莫名其妙的字?jǐn)?shù)限制只能分割放送了。提供可以獲取畫布上任何一個像素,并可以自由的操作他們。繪制指定的位置繪制對象的內(nèi)容。 前言 本文是接續(xù)系列教程的extra1,主要是介紹顏色系統(tǒng)在canvas中的應(yīng)用。 本來是與extra1一起成文的,因為segmentfault莫名其妙的字?jǐn)?shù)限制bug只能分割放送了。 ...

    Hydrogen 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<