成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

萬圣節(jié)動(dòng)畫-canvas像素點(diǎn)

tainzhi / 904人閱讀

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

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

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

效果

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

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

## 開始路徑

context.beginPath();

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

移動(dòng)路徑
context.moveTo();

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

添加線條
context.lineTo();

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

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

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

獲取像素?cái)?shù)據(jù)
context.getImageData(x,y,width,height);

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

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

放回像素?cái)?shù)據(jù)
context.putImageData(imageData,x,y);

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

實(shí)現(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://m.hztianpu.com/yun/92011.html

相關(guān)文章

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

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

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

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

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

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

    Hydrogen 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<