好,所以我想使用Javascript重新制作矩阵雨,效果很好(这是我的第一个“程序”),但是我想更改“雨”中的第一个字符,使其开始为白色并过渡变为绿色,然后逐渐变为黑色。 现在,我正确地完成了最后两部分,但我无法设法制作出第一部分。 (是白色文字)。
我需要前导**字符为白色。因此导致“雨”的角色不断变白(我不确定是否可能)。例如:if(drops [i] == 1),1是屏幕上的第一行,那是我需要更改值的地方,以便如果亮起的字符在屏幕的最底部雨点变成白色,然后下一个亮起时变成绿色,而后一个变成白色(从上到下)。
这是预期的结果: Wanted Result
这是完整的代码:
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>
所编写的代码有很多帮助,所以我可能不了解某些内容(我想我大部分时间都可以理解。)
答案 0 :(得分:1)
在这里您要交配,为此调整了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>