分享JS四种好玩的黑客背景效果代码

yizhihongxing

下面我将详细讲解“分享JS四种好玩的黑客背景效果代码”的完整攻略。

1. 简介

黑客背景效果是一种常见的网页背景效果,其通过在页面背景上显示类似黑客攻击的效果,给人一种神秘而高科技的感觉。本文将分享四种好玩的JS黑客背景效果代码,具体包括:

  1. 随机字符滚动效果
  2. 矩阵雨效果
  3. 烟花爆炸效果
  4. 扫描线效果

以下将对每种效果进行具体讲解。

2. 随机字符滚动效果

随机字符滚动效果是一种很有趣的黑客背景效果,它会在页面上随机滚动显示字符。下面是代码:

<body id="body"></body>

<script>
const characters = "!<>-_\\/[]{}—=+*^?#________";
const numberOfCharacters = 200;
const duration = 50;

for (let i = 0; i < numberOfCharacters; i++) {
    const span = document.createElement("span");
    span.innerText = characters.charAt(Math.floor(Math.random() * characters.length));
    span.style.animationDelay = `${i * duration}ms`;
    document.getElementById("body").appendChild(span);
}
</script>

这段代码使用了CSS的动画特性,使用了 animation-delay 来对每个字符的滚动速度进行控制。

3. 矩阵雨效果

矩阵雨效果是一种非常经典的黑客背景效果,它会在页面上滚动显示矩阵代码的效果。下面是代码:

<body>

<canvas id="c"></canvas>

<script type="module">
import * as THREE from "https://esm.sh/three@0.125.2/build/three.module.js";

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

const numberOfColumns = 100;
const fontSize = 12;
const fontName = "Consolas";
const columnSpeed = 30;
const columnDelay = 150;
const letterSpacing = 10;

const characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const charactersLength = characters.length;

const text = [];
for (let i = 0; i < 60; i++) {
    text[i] = {
        char: characters.charAt(Math.floor(Math.random() * charactersLength)),
        y: -i * fontSize
    };
}

class Column {
    constructor(x) {
        this.x = x;
        this.maxHeight = c.height;
        this.speed = columnSpeed;
        this.delay = x * columnDelay;
        this.transitionCounter = 0;
        this.letters = [];
        this.numberOfLetters = Math.floor(Math.random() * 20);
        for (let i = 0; i < this.numberOfLetters; i++) {
            this.letters.push(characters.charAt(Math.floor(Math.random() * charactersLength)));
        }
    }

    update(dt) {
        this.transitionCounter += dt;
        if (this.transitionCounter > this.delay && this.letters.length <= text.length) {
            this.letters.unshift(text[this.letters.length]);
            this.transitionCounter = 0;
        }

        if (this.letters.length > text.length + 1) {
            this.letters.pop();
        }

        this.maxHeight = text.length * fontSize + columnSpeed;
    }

    draw() {
        ctx.font = `${fontSize}px ${fontName}`;
        ctx.fillStyle = "#33FF00";
        ctx.textAlign = "center";
        ctx.textBaseline = "bottom";

        for (let i = 0; i < this.letters.length; i++) {
            const letter = this.letters[i];
            const y = letter.y + columnSpeed;
            const x = this.x;
            const alpha = (text.length - letter.y) / text.length;
            const color = `rgba(51, 255, 0, ${alpha})`;
            ctx.fillStyle = color;
            ctx.fillText(letter.char, x, y);
        }
    }
}

const columns = [];
for (let i = 0; i < numberOfColumns; i++) {
    columns.push(new Column(i * letterSpacing));
}

let lastTime = Date.now();

function frame() {
    const currentTime = Date.now();
    const dt = (currentTime - lastTime) / 1000;
    ctx.clearRect(0, 0, c.width, c.height);
    for (let i = 0; i < columns.length; i++) {
        const column = columns[i];
        column.update(dt);
        column.draw();
    }

    lastTime = currentTime;
    requestAnimationFrame(frame);
}

requestAnimationFrame(frame);
</script>
</body>

矩阵雨效果使用了 canvas 元素和 three.js 库来处理动画效果。该代码使用了字母淡入淡出和运动效果,让矩阵代码的效果展示得更加出色。

4. 烟花爆炸效果

烟花爆炸效果是一种非常神奇的黑客背景效果,它会在页面上显示烟花爆炸的效果。下面是代码:

<body>

<div id="container"></div>

<script>
const container = document.querySelector("#container");

class Firework {
    constructor() {
        this.x = Math.random() * container.clientWidth;
        this.y = container.clientHeight;
        this.radius = 3;
        this.launchTime = Date.now();
        this.speed = {
            x: Math.random() * 6 - 3,
            y: -Math.random() * 15 - 10
        };
        this.time = 0;
        this.maxTime = Math.random() * 200 + 300;
        this.color = {
            r: 200 + Math.round(Math.random() * 55),
            g: 200 + Math.round(Math.random() * 55),
            b: 200 + Math.round(Math.random() * 55),
        }
    }

    update() {
        const dt = (Date.now() - this.launchTime) / 1000;
        this.x += this.speed.x;
        this.y += this.speed.y + dt * 50;
        this.time += dt;
        if (this.time >= this.maxTime) {
            createFireworks(this.x, this.y);
        }
    }

    draw() {
        const style = `rgba(${this.color.r}, ${this.color.g}, ${this.color.b}, ${1 - this.time / this.maxTime})`;
        container.insertAdjacentHTML("beforeend", `<div class="particle" style="background-color:${style};top:${this.y}px;left:${this.x}px;"></div>`);
    }

    get isDone() {
        return this.y > container.clientHeight || this.time >= this.maxTime;
    }
}

const fireworks = [];

function updateFireworks() {
    for (let i = fireworks.length - 1; i >= 0; i--) {
        const firework = fireworks[i];
        firework.update();
        if (firework.isDone) {
            fireworks.splice(i, 1);
        }
    }
}

function drawFireworks() {
    for (let i = fireworks.length - 1; i >= 0; i--) {
        const firework = fireworks[i];
        firework.draw();
    }
}

function createFireworks(x, y) {
    for (let i = 0; i < 50; i++) {
        const firework = new Firework();
        fireworks.push(firework);
    }
}

function frame() {
    updateFireworks();
    drawFireworks();
    requestAnimationFrame(frame);
}

requestAnimationFrame(frame);
</script>

<style>
#container {
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: #000;
    overflow: hidden;
}

.particle {
    position: absolute;
    width: 6px;
    height: 6px;
    border-radius: 50%;
}
</style>
</body>

烟花爆炸效果使用了 div 元素和 setTimeout 来处理动画效果。该代码使用了类似于粒子爆炸的效果,让页面显示得更加抢眼。

5. 扫描线效果

扫描线效果是一种非常独特的黑客背景效果,它会在页面上扫描显示线条的效果。下面是代码:

<body>

<div id="container">
    <div class="line"></div>
    <div class="line"></div>
</div>

<script>
const container = document.querySelector("#container");
const lines = document.querySelectorAll(".line");

let animationFrameId;
let lineIndex = 0;
let lineHeight = "100%";

function update() {
    lines[lineIndex].style.height = lineHeight;
    if (lineHeight === "100%") {
        lineIndex -= 1;
        lineHeight = "0%";
    } else if (lineHeight === "0%") {
        lineIndex += 1;
        lineHeight = "100%";
    }
    if (lineIndex < 0) {
        lineIndex = lines.length - 1;
    }
    if (lineIndex >= lines.length) {
        lineIndex = 0;
    }
    animationFrameId = requestAnimationFrame(update);
}

update();
</script>

<style>
#container {
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: #000;
}

.line {
    position: absolute;
    width: 100%;
    height: 0%;
    top: 0;
    left: 0;
    background-color: #0F0;
    opacity: 0.1;
    transition: .3s ease-out;
    transform-origin: 50% 0%;
}

.line:nth-child(even) {
    background-color: #0F0;
}

.line:nth-child(odd) {
    background-color: #00FF00;
}

.line:nth-child(1) {
    animation: none !important;
    height: 100% !important;
    width: 1px;
    left: 50%;
    top: 0;
    transform: translate(-50%, 0);
}
</style>
</body>

扫描线效果使用了 div 元素的 transitiontransform 属性来处理动画效果。该代码使用了类似于电视机扫描效果的线条扫描效果,让页面显示得更加科幻。

以上就是关于“分享JS四种好玩的黑客背景效果代码”的详细攻略。希望对你有帮助!

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:分享JS四种好玩的黑客背景效果代码 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • css 字体设置(不同浏览器设置效果)

    CSS字体设置是网页设计中的一个重要部分。网站开发者需要确保相同字体在不同浏览器或操作系统下都可以正常显示。以下是关于CSS字体设置的攻略: 1. 使用通用字体族名称 在CSS中,可以使用通用字体族名称来设置字体。这些名称适用于大多数操作系统和浏览器。比如: font-family: Arial, Helvetica, sans-serif; 在这个例子中,…

    css 2023年6月9日
    00
  • Pjblog模板制作教程 超强推荐

    当您想要给自己的网站使用Pjblog模板时,可以按照以下攻略进行制作: 确定模板风格和结构 首先,需要确定模板的风格和结构。可以根据自己的需要,在已有的模板基础上进行修改,或者全新设计一个模板,确定好后就需要开始裁剪模板了。 对模板文件进行裁剪 根据模板结构,需要裁剪出若干个文件,然后分别放到对应的目录下。关于文件的格式和使用方法,可以参考Pjblog文档中…

    css 2023年6月9日
    00
  • W3C标准以及XHTML和HTML

    W3C标准是Web标准的制定机构,它的全称为World Wide Web Consortium,是万维网联盟的意思。W3C的标准包括XML、XSL、CSS、SOAP、HTML、XHTML等,其中HTML和XHTML是前端开发所需掌握的重要标准。 HTML标准是Hyper Text Markup Language的缩写,是Web前端开发中最为重要和基本的标准之…

    css 2023年6月9日
    00
  • HTML中img标签只显示图片中心位置的方法(三种方法)

    下面我将详细讲解三种方法让HTML中的img标签只显示图片中心位置。 方法一:使用background-image 通过将图片作为 background-image 设置在 div 或者其他块元素上,然后设置 background-position 属性为 center,即可实现只显示图片中心位置。 示例代码: <div class="ima…

    css 2023年6月9日
    00
  • css控制UL LI 的样式详解(推荐)

    我来为您详细讲解“CSS控制UL LI的样式详解(推荐)”这篇文章的完整攻略。 前言 这篇文章主要讲解的是如何使用CSS控制UL LI的样式,并推荐一些实用的技巧和经验。UL和LI分别代表无序列表和列表项,在网页中常常被使用。 CSS控制UL样式 1. 列表项前面显示小图标 使用list-style-type属性来设置列表项前面的小图标的样式。常见的小图标包…

    css 2023年6月10日
    00
  • CSS图片翻转动画技术详解(IE也实现了)

    以下是CSS图片翻转动画技术的详细攻略: CSS图片翻转动画技术详解(IE也实现了) 1. 概述 CSS图片翻转动画技术是一种常用的Web前端开发技术,通过CSS3中的transform属性和transition属性实现图片的翻转、旋转等动画效果。同时,这种技术在IE浏览器中也有多种实现方式,可以兼容更多用户。 2. 实现过程 2.1 基本过程 实现图片翻转…

    css 2023年6月10日
    00
  • 浏览器端如何使用Less

    Less 是一种 CSS 预处理器,它扩展了 CSS 语言,使其具有变量、嵌套、混合、函数等功能。本文将详细讲解浏览器端如何使用 Less 的完整攻略,包括安装 Less、编写 Less 文件、编译 Less 文件、使用 Less 文件等。 1. 安装 Less 在浏览器端使用 Less,需要先安装 Less.js。可以通过以下两种方式进行安装: 1.1 下…

    css 2023年5月18日
    00
  • css3实例教程 一款纯css3实现的环形导航菜单

    下面我将为您详细讲解“css3实例教程 一款纯css3实现的环形导航菜单”的完整攻略。 1. 环形导航菜单的原理 环形导航菜单是一种比较复杂的导航菜单,其实现原理是利用CSS3的旋转和位移功能,通过对导航菜单容器进行旋转,并对导航菜单项进行位移来达到环形排列的效果。 2. HTML结构 HTML结构比较简单,主要包含一个导航菜单容器和若干导航菜单项。 代码如…

    css 2023年6月10日
    00
合作推广
合作推广
分享本页
返回顶部