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

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

在畫(huà)布中放置clearRect()(帶有文本動(dòng)畫(huà))

在畫(huà)布中放置clearRect()(帶有文本動(dòng)畫(huà))

桃花長(zhǎng)相依 2023-10-30 20:16:15
我是 HTML5 canvas 的新手,制作了一個(gè)簡(jiǎn)單的文本動(dòng)畫(huà)。但是,當(dāng)動(dòng)畫(huà)正在進(jìn)行時(shí),我無(wú)法清除以前繪制的文本。文字看起來(lái)很拖沓。請(qǐng)找到下面的代碼鏈接以獲得清晰的圖片。單擊動(dòng)畫(huà)按鈕即可觀看動(dòng)畫(huà)。function drawTextOnCanvas(can,  ctx,  text,  font,  backColor,  textColor,  maxWidth,  startingx,  startingy,  spacing) {  var linesArray = getLines(ctx, text, maxWidth);  ctx.save();  for (var i = 0; i < linesArray.length; i++) {    drawTextBG(    can,      ctx,      linesArray[i],      font,      backColor,      textColor,      startingx,      startingy    );    // ctx.fillText(linesArray[i], startingx, startingy);    /* startingx += spacing; */    startingy += spacing; // Remove this if you want everthing in line  }  ctx.restore();}function getLines(ctx, text, maxWidth) {  // Enter maxWidth depending on the resolution and canvas dimensions  var words = text.split(" ");  var lines = [];  var currentLine = words[0];    ctx.font = "54px bolder Arial"  for (var i = 1; i < words.length; i++) {    var word = words[i];    var width = ctx.measureText(currentLine + " " + word).width;    if (width < maxWidth) {      currentLine += " " + word;    } else {      lines.push(currentLine);      currentLine = word;    }  }  lines.push(currentLine);  return lines;}function drawTextBG(can,ctx, txt, font, backColor, textColor, x, y) {  /// set font  console.log(txt)  x= -300;  let speed = 15;  let distance = 0;   var startTime = new Date().getTime();var interval = setInterval(function() {    if (new Date().getTime() - startTime > 1000) {      clearInterval(interval);    }  if (distance >= 600) {    distance = 0;    // clearInterval(interval);    x = canv.width / 2;  }    distance += speed;    animateText(can,ctx, txt, font, backColor, textColor, x + distance, y);  }, 33);}function animateText(can,ctx, txt, font, backColor, textColor, x, y) {  ctx.font = font;  /// draw text from top - makes life easier at the moment  ctx.textBaseline = "top";  /// color for background  ctx.fillStyle = backColor;  /// get width of text}這是小提琴幫助我獲得完美的動(dòng)畫(huà)。提前致謝!
查看完整描述

1 回答

?
小怪獸愛(ài)吃肉

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

我無(wú)法從你的代碼中得到太多,最終重構(gòu)了所有。


希望這能讓您走上正確的方向


class Text {

  constructor(txt, font, backColor, textColor, x, y, speed) {

    this.txt = txt;

    this.font = font;

    this.backColor = backColor;

    this.textColor = textColor;

    this.initX = x;

    this.x = x;

    this.y = y;

    this.speed = speed;

  }

  draw(ctx) {

    ctx.beginPath()

    ctx.font = this.font;

    ctx.textBaseline = "top";

    ctx.fillStyle = this.backColor;


    var width = ctx.measureText(this.txt).width;

    ctx.fillRect(this.x, this.y, width, parseInt(this.font, 10));

    ctx.fillStyle = this.textColor;

    ctx.fillText(this.txt, this.x, this.y);

    this.x -= this.speed ;

    if (this.x < -width)

      this.x = this.initX

  }

}


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

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


initX = canvas.width

dogs = new Text("Dogs are cute animals", "20px Arial", "#f50", "#000", initX, 20, 1)

cats = new Text("Cats say Miauu", "22px Arial", "#000", "#0F0", initX, 50, 0.5)

hello = new Text("HELLO", "12px Arial", "#F00", "#FF0", initX, 80, 3)


setInterval(draw, 20)


function draw() {

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

  dogs.draw(ctx)

  cats.draw(ctx)

  hello.draw(ctx)

}

<canvas id="canvas" width=220 height=100></canvas>

應(yīng)該都清楚了,如果有什么讓你困惑的請(qǐng)告訴我



查看完整回答
反對(duì) 回復(fù) 2023-10-30
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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