首页 > 解决方案 > 我想用 javascript 为文本着色

问题描述

好的,所以我想使用 Javascript 重新制作矩阵雨,并且进展顺利(这是我的第一个“程序”),但我想将“雨”中的第一个字符更改为从白色开始然后过渡到绿色逐渐变黑。现在我正确地完成了最后两部分,但我无法制作第一部分。(这是白色文本)。

我需要领先的**字符是白色的。所以导致“雨”不断变白的角色(我不确定是否可能)。例如: if(drops[i] == 1), the 1 是屏幕上的第一行,这就是我需要更改值的地方,以便如果点亮的字符位于最底部下雨它变成白色,然后当下一个亮起时它变为绿色,而后一个变为白色(从上到下)。

这是预期的结果: 想要的结果

这是完整的代码:

var c = document.getElementById("c");
var ctx = c.getContext("2d");

//making the canvas full screen
c.height = window.innerHeight;
c.width = window.innerWidth;

var chinese = "ハミヒーウシナモニサワツオリアホテマケメエカキムユラセネスタヌヘヲイクコソチトノフヤヨルレロン012345789:・.\"=*+-<>";
//converting the string into an array of single characters
chinese = chinese.split("");

var font_size = 12;
var columns = c.width / font_size; //number of columns for the rain
//an array of drops - one per column
var drops = [];
//x below is the x coordinate
//1 = y co-ordinate of the drop(same for every drop initially)
for (var x = 0; x < columns; x++)
  drops[x] = 1;
//drawing the characters

function draw() {
  //Black BG for the canvas
  //translucent BG to show trail
  ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
  ctx.fillRect(0, 0, c.width, c.height);
  // Create gradient
  ctx.fillStyle = "#0F0" //green text
  ctx.font = font_size + "px arial";
  //looping over drops
  for (var i = 0; i < drops.length; i++) {
    //a random chinese character to print
    var text = chinese[Math.floor(Math.random() * chinese.length)];
    //x = i*font_size, y = value of drops[i]*font_size
    ctx.fillText(text, i * font_size, drops[i] * font_size);

    //sending the drop back to the top randomly after it has crossed the screen
    //adding a randomness to the reset to make the drops scattered on the Y axis

    if (drops[i] * font_size > c.height && Math.random() > 0.975)
      drops[i] = 0;

    //incrementing Y coordinate
    drops[i]++;
  }
}

setInterval(draw, 95);
* {
  margin: 0;
  padding: 0;
}

body {
  background: black
}

canvas {
  display: block
}
<canvas id="c"></canvas>

编写的代码有很多帮助,所以我可能不理解某些东西(我想我大部分都明白了)。

标签: javascriptcsshtmlmatrix

解决方案


给你伙计,刚刚为此调整了JS。我只是让它保存生成的文本,以便它以白色绘制新生成的文本,然后在其上方的空间上以绿色重新绘制先前的文本,然后最后用新文本替换先前的文本。

var c = document.getElementById("c");
var ctx = c.getContext("2d");

//making the canvas full screen
c.height = window.innerHeight;
c.width = window.innerWidth;

var chinese = "ハミヒーウシナモニサワツオリアホテマケメエカキムユラセネスタヌヘヲイクコソチトノフヤヨルレロン012345789:・.\"=*+-<>";
//converting the string into an array of single characters
chinese = chinese.split("");

var font_size = 12;
var columns = c.width / font_size; //number of columns for the rain
//an array of drops - one per column
var drops = [];
//x below is the x coordinate
//1 = y co-ordinate of the drop(same for every drop initially)
for (var x = 0; x < columns; x++)
  drops[x] = 1;

//Hold text for next redraw
var previousText = [];
 
//drawing the characters
function draw() {
  //Black BG for the canvas
  //translucent BG to show trail
  ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
  ctx.fillRect(0, 0, c.width, c.height);
  
  ctx.font = font_size + "px arial";
  
  //looping over drops
  for (var i = 0; i < drops.length; i++) {

    //a random chinese character to print
    var text = chinese[Math.floor(Math.random() * chinese.length)];

    //Draw in white
    ctx.fillStyle = "#FFF";
    //x = i*font_size, y = value of drops[i]*font_size
    ctx.fillText(text, i * font_size, drops[i] * font_size);

    //Draw in green
    ctx.fillStyle ="#0F0";
    ctx.fillText(previousText[i], i * font_size, (drops[i] - 1) * font_size);
    previousText[i] = text;


    //sending the drop back to the top randomly after it has crossed the screen
    //adding a randomness to the reset to make the drops scattered on the Y axis

    if (drops[i] * font_size > c.height && Math.random() > 0.975)
      drops[i] = 0;

    //incrementing Y coordinate
    drops[i]++;
  }
}

setInterval(draw, 95);
* {
  margin: 0;
  padding: 0;
}

body {
  background: black
}

canvas {
  display: block
}
<canvas id="c"></canvas>


推荐阅读