Можно ли сделать так, чтобы текст исчезал слева направо на холсте? - PullRequest
0 голосов
/ 23 декабря 2018

Я хотел бы, чтобы текст на холсте появлялся и отображался слева направо.Я не знаю, возможно ли это.Я пытался скрыть текст с помощью функции clip(), но я не могу преобразовать клип, чтобы уменьшить ширину и сделать текст видимым.

ctx.fillStyle = "#1f2f90";
ctx.fillText('This is a text!', 150, 100);

ctx.rect(50, 20, 200, 120);
ctx.stroke();
ctx.clip();

var i = 200;
setInterval(function(){
	ctx.clearRect(0,0,300,150);
    ctx.rect(50,20,i,120);
    ctx.stroke();
	ctx.clip();
    i++;
},20);

Ответы [ 2 ]

0 голосов
/ 23 декабря 2018

Мое решение другое.Каждая буква - это объект с позицией и альфа-прозрачностью.Во время анимации прозрачность букв уменьшается, по одной букве за раз.

Вы можете перезапустить анимацию при нажатии.Пожалуйста, прочитайте комментарии в коде.

const canvas = document.getElementById("canvas");
const _canvas = document.getElementById("_canvas");
const ctx = canvas.getContext("2d");
const _ctx = _canvas.getContext("2d");
let cw = canvas.width = _canvas.width = 400,
  cx = cw / 2;
let ch = canvas.height = _canvas.height = 150,
  cy = ch / 2;
  
let rid = null;// request animation id
let theText = 'This is a text!';
let letters = [];// the letters array

let k = 20;// controls the speed of the animation

ctx.font = _ctx.font = "2em Verdana";

// every letter is an object
class Letter{
  constructor(char,x){
    this.char = char;// the letter
    // measure the letter
    _ctx.fillText(this.char, 0, cy);
    this.w = _ctx.measureText(this.char).width;
    //the position of the text
    this.pos = {}
    this.pos.y = cy;
    this.pos.x = x;
    // the transparency of the letter
    this.alpha = 1;
  }
  show(){// draw the letter
    ctx.fillStyle = `rgba(0,0,0,${this.alpha})`;
    ctx.fillText(this.char, this.pos.x, this.pos.y);
  }
  update(){
    //change the transparency of the text
    if(this.alpha > 0){this.alpha -= 1/k;}
    if(this.alpha < 0){this.alpha = 0; index++}
  }
}


let x = 0;

for(l=0; l<theText.length; l++){
  // a new letter object
letters.push(new Letter(theText[l],x))
//calculate the x position of the next letter
x = letters.reduce( function(a, b){return a + b.w}, 0); 
}

// draw all the letters
for(l=0; l<letters.length; l++){letters[l].show()}

// the actual letter. 
let index = 0;

function Draw() {
rid = window.requestAnimationFrame(Draw);
ctx.clearRect(0,0,cw,ch);
letters[index].update();//change the transparency of the actual letter
// draw all the letters
for(l=0; l<letters.length; l++){letters[l].show()}
// if the last letter is fully transparent stop the animation
if(letters[letters.length - 1].alpha <= 0){
  window.cancelAnimationFrame(rid);rid = null;
}
}
Draw();

//resume animation on click
canvas.addEventListener("click",()=>{
if(rid){window.cancelAnimationFrame(rid);rid = null;}
  index = 0;
  for(l=0; l<letters.length; l++){letters[l].alpha = 1;letters[l].show()}
  Draw();
})
#_canvas{display:none;}
canvas {
  border:1px solid;
}
<canvas id="canvas"></canvas>
<canvas id="_canvas"></canvas>
0 голосов
/ 23 декабря 2018

Вот пример использования fillRect для покрытия текста.

let ctx = document.getElementById('canvas').getContext('2d');
ctx.fillStyle = "#1f2f90";
ctx.fillText('This is a text!', 150, 100);

var i = 0;
setInterval(function(){
    ctx.fillStyle = "white";
    ctx.fillRect(50,20,i,120);
    ctx.strokeRect(50, 20, 200, 120);
    i++;
},20);
<canvas id="canvas"></canvas>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...