javascript - 我想用 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>
编写的代码有很多帮助,所以我可能不理解某些东西(我想我大部分都明白了)。
解决方案
给你伙计,刚刚为此调整了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>
推荐阅读
- python - 我无法通过单击红色的“X”按钮退出 pymsgbox 模块
- vue.js - 如何让 vue 路由器正确地将对象推送到 URL 查询字符串,而不是“[Object Object]”?
- c# - 调用 FirebaseAdmin 加载凭据不起作用
- java - Ant Classloader 任务和 java 13
- java - Flyway Maven 插件从外部配置文件执行 GOLS
- android-studio - 为什么我的 Android Studio 3.5 缺少设置 > 编辑器 > 代码样式?
- paypal - 如何在 Paypal SDK createOrder 中设置商品详情?
- visual-studio-code - 如何确定导致崩溃的 VS Code 扩展?
- apache-spark - 在 Databricks / Spark 中的 SQL 中为变量分配动态值
- php - 跟踪用户日志的最佳方式?