亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在香草javascript中制作移動線?

如何在香草javascript中制作移動線?

喵喵時光機 2022-10-27 14:44:49
我正在嘗試在香草 javascript 中創建下雨效果。下面是我嘗試過的代碼。我在y每個間隔更新坐標,但需要清除前一行,使其看起來像下降線效果。謝謝var canvas = document.getElementById("DemoCanvas");var ctx = canvas.getContext("2d");class Drop {  constructor() {    this.x = canvas.width / 2;    this.y = 0;    this.yspeed = 10;  }  fall() {    this.y = this.y + this.yspeed;  }  show() {    ctx.moveTo(this.x, this.y);    ctx.lineTo(this.x, this.y + 10);    ctx.stroke();  }}if (canvas.getContext) {  let d = new Drop();  setInterval(() => {    d.show();    d.fall();  }, 500);}<body>  <canvas id="DemoCanvas" width="1400" height="1400"></canvas></body>
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

只需調用ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);,同樣,調用時stroke,您需要先調用ctx.beginPath(),所有這些:


var canvas = document.getElementById("DemoCanvas");

      var ctx = canvas.getContext("2d");

      class Drop{

        constructor(){

          this.x = canvas.width / 2;

          this.y = 0;

          this.yspeed = 10;

        }

        fall(){

          this.y = this.y + this.yspeed;

        }

      

        show(){

          ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)

          ctx.beginPath()

          ctx.moveTo(this.x, this.y);

          ctx.lineTo(this.x, this.y+10);

          ctx.stroke();

        }

      }

      if (canvas.getContext) {

        let d = new Drop();

        

        setInterval(()=>{

          d.show();

          d.fall();

        },500);

      }

<body>

    <canvas id="DemoCanvas" width="1400" height="1400"></canvas>

  </body>


查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號