第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JS 清除不清除整個(gè)畫布

JS 清除不清除整個(gè)畫布

慕慕森 2022-09-23 18:34:02
我對js有點(diǎn)陌生,遇到了一個(gè)問題。我試圖創(chuàng)建一個(gè)天空中的星星動(dòng)畫。我通過在畫布上隨機(jī)創(chuàng)建小圓圈,然后隨機(jī)選擇閃爍的星星(不透明度已更改)來做到這一點(diǎn)。然后我意識(shí)到網(wǎng)站在縮放時(shí)無法正常工作,所以我決定為窗口大小調(diào)整實(shí)現(xiàn)一個(gè)onevent,當(dāng)窗口調(diào)整大小時(shí),我再次運(yùn)行相同的函數(shù)來重新創(chuàng)建相同的過程,但是在清除所有以前的星星的同時(shí),它們不會(huì)成倍增加。這里的問題是,clearRect方法似乎沒有為我清除先前繪制的星星。:),任何幫助將不勝感激。這是我的代碼。let starCollection  = [];let randomStars     = [];let numberofStars   = 100;let flickeringStars = 50;class Star{    constructor(x,y,color,radius){        this._canvas        = document.querySelector('canvas');        this._canvas.width  = window.innerWidth;        this._canvas.height = window.innerHeight;         this._c             = this._canvas.getContext('2d');        this._radius        = radius;        this._x       = x;        this._y       = y;        this._color   = color;         }    //drawing individual stars    draw(){        //Drawing        this._c.beginPath();        this._c.arc(this._x,this._y,this._radius,0,Math.PI * 2,false);        this._c.fillStyle   = this._color;        this._c.strokeStyle = 'black';        this._c.stroke();        this._c.fill();        this._c.closePath();    }    //Fade in and out for stars    flicker(){        setTimeout(()=>{this._color='#EBEBEB';},300);        setTimeout(()=>{this._color='#D9D9D9';},600);        setTimeout(()=>{this._color='#B6B6B6';},900);        setTimeout(()=>{this._color='#898787';},1200);        setTimeout(()=>{this._color='#4F4F4F';},1500);        setTimeout(()=>{this._color='black';},1800);        setTimeout(()=>{this._color='#4F4F4F';},2100);        setTimeout(()=>{this._color='#898787';},2400);        setTimeout(()=>{this._color='#B6B6B6';},2700);        setTimeout(()=>{this._color='#D9D9D9';},3000);        setTimeout(()=>{this._color='#EBEBEB';},3300);        setTimeout(()=>{this._color='#FFFFFF';},3600);    }}
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

您的問題出在閃爍功能中。即使在調(diào)整大小功能后,這些設(shè)置超時(shí)仍然會(huì)觸發(fā)。所以他們稱之為.draw(),即使這些星星已經(jīng)從你的數(shù)組中移除了。該函數(shù)仍然記得“這”是什么。


您可以像這樣清除所有超時(shí):


  window.addEventListener("resize", () => {

    let canvas = document.querySelector("canvas");

    let context = canvas.getContext("2d");

    canvas.width = window.innerWitdh;

    canvas.height = window.innerHeight;

    context.clearRect(0, 0, window.innerWidth, window.innerHeight);

    starCollection = [];

    randomStars = [];


    var id = window.setTimeout(function () { }, 0);

    while (id--) {

      window.clearTimeout(id); // will do nothing if no timeout with id is present

    }


    let event = new CustomEvent("showStars");

    dispatchEvent(event);

});

下面是一個(gè)有 10 顆星星的示例,其中 5 顆閃爍。我把星星做得更大,這樣更容易看到。


<html>


<head>

    <style>

        body {

            background: black;

        }

    </style>

</head>


<body>

    <canvas></canvas>


    <script>


        let starCollection = [];

        let randomStars = [];

        let numberofStars = 10;

        let flickeringStars = 5;


        class Star {

            constructor(x, y, color, radius) {

                this._canvas = document.querySelector('canvas');

                this._canvas.width = window.innerWidth;

                this._canvas.height = window.innerHeight;

                this._c = this._canvas.getContext('2d');

                this._radius = radius;


                this._x = x;

                this._y = y;

                this._color = color;

            }


            //drawing individual stars

            draw() {

                //Drawing

                this._c.beginPath();

                this._c.arc(this._x, this._y, this._radius, 0, Math.PI * 2, false);

                this._c.fillStyle = this._color;

                this._c.strokeStyle = 'black';

                this._c.stroke();

                this._c.fill();

                this._c.closePath();

            }

            //Fade in and out for stars

            flicker() {


                setTimeout(() => {

                    this._color = '#EBEBEB';

                }, 300);

                setTimeout(() => {

                    this._color = '#D9D9D9';

                }, 600);

                setTimeout(() => {

                    this._color = '#B6B6B6';

                }, 900);

                setTimeout(() => {

                    this._color = '#898787';

                }, 1200);

                setTimeout(() => {

                    this._color = '#4F4F4F';

                }, 1500);


                setTimeout(() => {

                    this._color = 'black';

                }, 1800);

                setTimeout(() => {

                    this._color = '#4F4F4F';

                }, 2100);

                setTimeout(() => {

                    this._color = '#898787';

                }, 2400);

                setTimeout(() => {

                    this._color = '#B6B6B6';

                }, 2700);

                setTimeout(() => {

                    this._color = '#D9D9D9';

                }, 3000);

                setTimeout(() => {

                    this._color = '#EBEBEB';

                }, 3300);

                setTimeout(() => {

                    this._color = '#FFFFFF';

                }, 3600);


                setTimeout(() => {

                    this.draw();

                }, 300);

                setTimeout(() => {

                    this.draw();

                }, 600);

                setTimeout(() => {

                    this.draw();

                }, 900);

                setTimeout(() => {

                    this.draw();

                }, 1200);

                setTimeout(() => {

                    this.draw();

                }, 1500);

                setTimeout(() => {

                    this.draw();

                }, 1800);

                setTimeout(() => {

                    this.draw();

                }, 2100);


                setTimeout(() => {

                    this.draw();

                }, 2400);

                setTimeout(() => {

                    this.draw();

                }, 2700);

                setTimeout(() => {

                    this.draw();

                }, 3000);

                setTimeout(() => {

                    this.draw();

                }, 3300);

                setTimeout(() => {

                    this.draw();

                }, 3600);


            }


        }


        window.addEventListener("showStars", () => {

            //Stars animation

            //Adding the stars to the array as objects

            for (let i = 0; i < numberofStars; i++) {

                let x = Math.floor(Math.random() * window.innerWidth);

                let y = Math.floor(Math.random() * window.innerHeight);

                // let starSize = (Math.random() + 1) - 0.7;

                let starSize = 10;

                starCollection.push(new Star(x, y, "white", starSize));

            }

            //Drawing all the stars on the screen

            for (let i = 0; i < starCollection.length; i++) {

                starCollection[i].draw();

            }

            //Storing random stars  

            const shuffleStars = () => {

                randomStars = [];

                for (let i = 0; i < flickeringStars; i++) {

                    randomStars.push(Math.floor(Math.random() * starCollection.length))

                }

            }

            shuffleStars();



            //Flickering stars randomly

            const starflicker = () => {

                console.log(starCollection);

                console.log(randomStars);

                setTimeout(() => {

                    requestAnimationFrame(starflicker);

                    for (let i = 0; i < randomStars.length; i++) {

                        starCollection[randomStars[i]].flicker();

                    }

                    shuffleStars();

                }, 500);

            }

            starflicker();

        })


        window.addEventListener("resize", () => {



            let canvas = document.querySelector("canvas");

            let context = canvas.getContext("2d");

            canvas.width = window.innerWitdh;

            canvas.height = window.innerHeight;

            context.clearRect(0, 0, window.innerWidth, window.innerHeight);


            starCollection = [];

            randomStars = [];

            var id = window.setTimeout(function () { }, 0);

            while (id--) {

                window.clearTimeout(id); // will do nothing if no timeout with id is present

            }


            let event = new CustomEvent("showStars");

            dispatchEvent(event);

        });

        dispatchEvent(new CustomEvent("showStars"))

    </script>


</body>


</html>

查看這篇文章,了解有關(guān)超時(shí)清除代碼如何工作的更多信息: javascript:清除所有超時(shí)?


查看完整回答
反對 回復(fù) 2022-09-23
  • 1 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)