我想把这个"矩阵效果"设置为我的html5网站的背景,但是每当我试图把元素放在画布的"上方"时,这些元素就会随着画布的文本消失.同样的文本也会呈现在图像上方.

我试着做一个单独的画布来放置图像,但它只是移动图像. 我也try 过这种"矩阵效应"的几种变体,但它们都是用同样的方法来淡出字母.

const canvas = document.getElementById('background_canvas');
const context = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const katakana = 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン';
const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const nums = '0123456789';

const alphabet = katakana + latin + nums;

const fontSize = 16;
const columns = canvas.width / fontSize;

const rainDrops = [];

for (let x = 0; x < columns; x++) {
  rainDrops[x] = 1;
}

const draw = () => {
  context.fillStyle = 'rgba(0, 0, 0, 0.05)';
  context.fillRect(0, 0, canvas.width, canvas.height);

  context.fillStyle = '#0F0';
  context.font = fontSize + 'px monospace';

  for (let i = 0; i < rainDrops.length; i++) {
    const text = alphabet.charAt(Math.floor(Math.random() * alphabet.length));
    context.fillText(text, i * fontSize, rainDrops[i] * fontSize);

    if (rainDrops[i] * fontSize > canvas.height && Math.random() > 0.975) {
      rainDrops[i] = 0;
    }
    rainDrops[i]++;
  }
};

setInterval(draw, 30);
html {
  background: black;
  height: 100%;
  overflow: hidden;
}

body .canvas #background_canvas {
  margin: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
  display: block;
  height: 100%;
  width: 100%;
  z-index: -1;
}

body #foreground {
  background-color: transparent;
  fill-opacity: 100%;
}
<div id="background">
  <script src="assets/js/background.js" type="module"></script>
  <canvas id="background_canvas" style='position:absolute; left:0; top:0;'>Cannot Render Canvas</canvas>
</div>
<div id="foreground">
  <script src="assets/js/app.js" type="module"></script>
  <canvas id="foreground_canvas"></canvas>
  <img src="assets/images/pfp.png" alt="My Profile Picture">
</div>

推荐答案

您需要将#foreground设置为z-index,才能将其显示在#background的顶部.您还需要将position设置为z-index才能产生任何效果:

body #foreground {
  /* existing properties... */
  position: relative;
  z-index: 10;
  color: #FFF;
}

完整的工作示例:

const canvas = document.getElementById('background_canvas');
const context = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const katakana = 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン';
const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const nums = '0123456789';

const alphabet = katakana + latin + nums;

const fontSize = 16;
const columns = canvas.width / fontSize;

const rainDrops = [];

for (let x = 0; x < columns; x++) {
  rainDrops[x] = 1;
}

const draw = () => {
  context.fillStyle = 'rgba(0, 0, 0, 0.05)';
  context.fillRect(0, 0, canvas.width, canvas.height);

  context.fillStyle = '#0F0';
  context.font = fontSize + 'px monospace';

  for (let i = 0; i < rainDrops.length; i++) {
    const text = alphabet.charAt(Math.floor(Math.random() * alphabet.length));
    context.fillText(text, i * fontSize, rainDrops[i] * fontSize);

    if (rainDrops[i] * fontSize > canvas.height && Math.random() > 0.975) {
      rainDrops[i] = 0;
    }
    rainDrops[i]++;
  }
};

setInterval(draw, 30);
html {
  background: black;
  height: 100%;
  overflow: hidden;
}

body .canvas #background_canvas {
  margin: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
  display: block;
  height: 100%;
  width: 100%;
  z-index: -1;
}

body #foreground {
  position: relative;
  background-color: transparent;
  fill-opacity: 100%;
  z-index: 10;
  color: #FFF;
}
<div id="background">
  <script src="assets/js/background.js" type="module"></script>
  <canvas id="background_canvas" style='position:absolute; left:0; top:0;'>Cannot Render Canvas</canvas>
</div>
<div id="foreground">
  <img src="http://placekitten.com/g/120/120" alt="My Profile Picture">
</div>

Javascript相关问答推荐

类型脚本中只有字符串或数字键而不是符号键的对象

了解Node.js中的EventEums和浏览器中的addEventEums之间的关系

如何找出摆线表面上y与x相交的地方?

使搜索栏更改语言

仅针对RTK查询中的未经授权的错误取消maxRetries

为什么当我解析一个promise时,输出处于挂起状态?

如何在Obsidian dataview中创建进度条

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

Eval vs函数()返回语义

try 将Redux工具包与MUI ToggleButtonGroup组件一起使用时出错

如何使用<;Link>;执行与JS Reaction中的";window.Location=/GameOverDied;";类似的操作?

不同表的条件API端点Reaction-redux

处理app.param()中的多个参数

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?

对不同目录中的Angular material 表列进行排序

有没有办法在R中创建一张具有多个色标的热图?

脚本语法错误只是一个字符串,而不是一个对象?

Rails 7:在不使用导入映射的情况下导入Java脚本

如何在Java脚本中添加一个可以在另一个面板中垂直调整大小的面板?

如何在不获取其子元素的文本内容的情况下获取元素的文本内容?