首页 / 日本服务器 / 正文
JS特效代码分享,从基础到高级的视觉体验,Js特效代码

Time:2025年03月16日 Read:6 评论:42 作者:y21dr45

本文目录导读:

JS特效代码分享,从基础到高级的视觉体验,Js特效代码

  1. JS 基础特效代码:让页面动起来
  2. JS 高级特效代码:复杂动画效果
  3. JS 基础与高级特效的结合:创建复杂视觉效果
  4. JS 基础与高级特效的结合:创建复杂视觉效果
  5. JS 基础与高级特效的结合:创建复杂视觉效果

在现代 web 开发中,JavaScript(JS)以其强大的灵活性和丰富的库函数,成为前端开发者实现视觉效果的首选工具,特效代码不仅能够提升网站的用户体验,还能让页面更具吸引力,无论是动态交互、复杂动画,还是逼真的视觉效果,JS 都能够胜任,本文将带您深入探索 JS 的魅力,从基础特效到高级特效,逐步掌握如何用代码创造令人惊叹的视觉效果。


JS 基础特效代码:让页面动起来

1 挥动的发光效果

发光效果是一种非常流行的视觉效果,能够给页面增添一份神秘感,以下是一个简单的发光效果代码示例:

function发光Effect() {
    const canvas = document.createElement('canvas');
    canvas.width = 800;
    canvas.height = 600;
    const ctx = canvas.getContext('2d');
    // 设置画布样式
    ctx.fillStyle = '#ffffff';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    // 遍历画布,为每个像素赋予发光效果
    for(let x = 0; x < canvas.width; x++) {
        for(let y = 0; y < canvas.height; y++) {
            // 计算距离中心点的距离
            const distance = Math.sqrt(
                Math.pow(x - canvas.width/2, 2) + 
                Math.pow(y - canvas.height/2, 2)
            );
            // 根据距离计算发光强度
            const intensity = Math.exp(-distance * 0.005);
            // 设置像素的颜色
            ctx.fillStyle = `hsl(${Math.sin(distance * 0.1) * 360}, 70%, ${intensity})`;
            ctx.fillRect(x, y, 1, 1);
        }
    }
    document.body.appendChild(canvas);
    canvas.addEventListener('mousemove', function(e) {
        // 根据鼠标位置动态调整发光中心
        const centerX = e.clientX;
        const centerY = e.clientY;
        ctx.fillStyle = `hsl(${Math.sin((centerX - canvas.width/2) * 0.1) * 360}, 70%, ${Math.exp(-Math.sqrt(
            Math.pow(centerX - canvas.width/2, 2) + 
            Math.pow(centerY - canvas.height/2, 2)
        ) * 0.005) * 2})`;
    });
}

这段代码通过遍历画布的每个像素,计算其与中心点的距离,并根据距离动态调整发光强度和颜色,当鼠标移动时,中心点也会随之移动,创造出一种漂浮的发光效果。


2 挥动的发光效果优化

为了使发光效果更加流畅,可以对代码进行一些优化:

  1. 减少重复计算:将重复计算的部分,如 Math.powMath.sqrt,提前计算并存储。
  2. 使用渐变:将发光效果与 CSS 动画相结合,使效果更加自然。
  3. 动态更新:在代码中加入对鼠标位置的动态响应,使效果更具交互性。

JS 高级特效代码:复杂动画效果

1 粒子效果:动态的视觉冲击

粒子效果是一种非常流行的动态视觉效果,可以用来表现能量、活力或动态过程,以下是实现粒子效果的代码:

function粒子效果() {
    const canvas = document.createElement('canvas');
    canvas.width = 800;
    canvas.height = 600;
    const ctx = canvas.getContext('2d');
    // 设置画布样式
    ctx.fillStyle = '#ffffff';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    // 粒子数组
    const particles = [];
    function createParticle(x, y) {
        particles.push({
            x: x,
            y: y,
            vx: (Math.random() - 0.5) * 4,
            vy: (Math.random() - 0.5) * 4,
            life: 1
        });
    }
    function updateParticles() {
        particles.forEach(particle => {
            // 更新粒子的位置
            particle.x += particle.vx;
            particle.y += particle.vy;
            // 更新粒子的速度
            particle.vx += (particle.vx * 0.1) * Math.cos(particle.y * 0.1);
            particle.vy += (particle.vy * 0.1) * Math.sin(particle.x * 0.1);
            // 绘制粒子
            ctx.fillStyle = `hsl(${Math.sin(particle.x + particle.y) * 360}, 70%, ${particle.life})`;
            ctx.fillRect(particle.x - 0.5, particle.y - 0.5, 1, 1);
            // 当粒子寿命结束,移除
            if(particle.life < 1) {
                particles.splice(particles.indexOf(particle), 1);
            }
        });
    }
    // 初始创建粒子
    for(let i = 0; i < 100; i++) {
        createParticle(Math.random() * canvas.width, Math.random() * canvas.height);
    }
    // 动画循环
    setInterval(updateParticles, 50);
}

这段代码通过创建多个粒子,并为每个粒子计算速度、方向和寿命,最终在画布上形成动态的粒子效果,粒子的颜色和大小根据数学函数动态变化,创造出丰富的视觉效果。


2 变形效果:动态的几何形态

变形效果是一种通过数学变换实现的动态几何形态变化,以下是实现变形效果的代码:

function变形效果() {
    const canvas = document.createElement('canvas');
    canvas.width = 800;
    canvas.height = 600;
    const ctx = canvas.getContext('2d');
    // 设置画布样式
    ctx.fillStyle = '#ffffff';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    // 变形参数
    let scale = 1;
    let rotate = 0;
    function update() {
        // 更新变形参数
        scale += (Math.sin(Date.now() * 0.001) * 0.1);
        rotate += (Math.cos(Date.now() * 0.001) * 0.05);
        // 应用变形
        ctx.transform = `scale(${scale}) rotate(${rotate}rad)`;
        // 绘制原图
        ctx.drawImage('original.png', 0, 0, canvas.width, canvas.height);
    }
    // 初始化
    function init() {
        // 创建原图
        const img = document.createElement('img');
        img.src = 'original.png';
        img.style.width = '100%';
        img.style.height = '100%';
        canvas.appendChild(img);
        // 设置 animation
        const interval = setInterval(update, 50);
        // 当 animation 结束,清理 interval
        clearInterval(interval);
        interval = null;
    }
    init();
}

这段代码通过动态调整缩放和平移变换,使得画布上的图片不断变形,变形效果可以通过调整变换参数和频率,创造出不同的动态效果。


JS 基础与高级特效的结合:创建复杂视觉效果

在实际项目中,往往需要将基础特效与高级特效结合,创造出更复杂的视觉效果,以下是一个结合发光效果和粒子效果的代码示例:

function复杂视觉效果() {
    const canvas = document.createElement('canvas');
    canvas.width = 800;
    canvas.height = 600;
    const ctx = canvas.getContext('2d');
    // 设置画布样式
    ctx.fillStyle = '#ffffff';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    // 粒子数组
    const particles = [];
    function createParticle(x, y) {
        particles.push({
            x: x,
            y: y,
            vx: (Math.random() - 0.5) * 4,
            vy: (Math.random() - 0.5) * 4,
            life: 1
        });
    }
    function updateParticles() {
        particles.forEach(particle => {
            // 更新粒子的位置
            particle.x += particle.vx;
            particle.y += particle.vy;
            // 更新粒子的速度
            particle.vx += (particle.vx * 0.1) * Math.cos(particle.y * 0.1);
            particle.vy += (particle.vy * 0.1) * Math.sin(particle.x * 0.1);
            // 绘制粒子
            ctx.fillStyle = `hsl(${Math.sin(particle.x + particle.y) * 360}, 70%, ${particle.life})`;
            ctx.fillRect(particle.x - 0.5, particle.y - 0.5, 1, 1);
            // 当粒子寿命结束,移除
            if(particle.life < 1) {
                particles.splice(particles.indexOf(particle), 1);
            }
        });
    }
    // 初始创建粒子
    for(let i = 0; i < 100; i++) {
        createParticle(Math.random() * canvas.width, Math.random() * canvas.height);
    }
    // 动画循环
    setInterval(updateParticles, 50);
    // 添加发光效果
    const发光Effect = function() {
        const canvas2 = document.createElement('canvas');
        canvas2.width = 800;
        canvas2.height = 600;
        const ctx2 = canvas2.getContext('2d');
        ctx2.fillStyle = '#ffffff';
        ctx2.fillRect(0, 0, canvas2.width, canvas2.height);
        for(let x = 0; x < canvas2.width; x++) {
            for(let y = 0; y < canvas2.height; y++) {
                const distance = Math.sqrt(
                    Math.pow(x - canvas2.width/2, 2) + 
                    Math.pow(y - canvas2.height/2, 2)
                );
                const intensity = Math.exp(-distance * 0.005);
                ctx2.fillStyle = `hsl(${Math.sin(distance * 0.1) * 360}, 70%, ${intensity})`;
                ctx2.fillRect(x, y, 1, 1);
            }
        }
        canvas2.body.appendChild(canvas2);
        canvas2.addEventListener('mousemove', function(e) {
            const centerX = e.clientX;
            const centerY = e.clientY;
            ctx2.fillStyle = `hsl(${Math.sin((centerX - canvas2.width/2) * 0.1) * 360}, 70%, ${Math.exp(-Math.sqrt(
                Math.pow(centerX - canvas2.width/2, 2) + 
                Math.pow(centerY - canvas2.height/2, 2)
            ) * 0.005) * 2})`;
        });
    };
    // 在 updateParticles 函数中调用发光Effect
    updateParticles();
    // 添加发光效果的事件 listeners
    document.addEventListener('mousemove', function(e) {
       发光Effect();
    });
}

这段代码结合了粒子效果和发光效果,创造出一种动态的发光粒子效果,发光效果通过计算每个像素到中心点的距离,动态调整发光强度和颜色,而粒子效果则通过动态更新粒子的位置和速度,形成复杂的动态形态。


JS 基础与高级特效的结合:创建复杂视觉效果

在实际项目中,往往需要将基础特效与高级特效结合,创造出更复杂的视觉效果,以下是一个结合发光效果和粒子效果的代码示例:

function复杂视觉效果() {
    const canvas = document.createElement('canvas');
    canvas.width = 800;
    canvas.height = 600;
    const ctx = canvas.getContext('2d');
    // 设置画布样式
    ctx.fillStyle = '#ffffff';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    // 粒子数组
    const particles = [];
    function createParticle(x, y) {
        particles.push({
            x: x,
            y: y,
            vx: (Math.random() - 0.5) * 4,
            vy: (Math.random() - 0.5) * 4,
            life: 1
        });
    }
    function updateParticles() {
        particles.forEach(particle => {
            // 更新粒子的位置
            particle.x += particle.vx;
            particle.y += particle.vy;
            // 更新粒子的速度
            particle.vx += (particle.vx * 0.1) * Math.cos(particle.y * 0.1);
            particle.vy += (particle.vy * 0.1) * Math.sin(particle.x * 0.1);
            // 绘制粒子
            ctx.fillStyle = `hsl(${Math.sin(particle.x + particle.y) * 360}, 70%, ${particle.life})`;
            ctx.fillRect(particle.x - 0.5, particle.y - 0.5, 1, 1);
            // 当粒子寿命结束,移除
            if(particle.life < 1) {
                particles.splice(particles.indexOf(particle), 1);
            }
        });
    }
    // 初始创建粒子
    for(let i = 0; i < 100; i++) {
        createParticle(Math.random() * canvas.width, Math.random() * canvas.height);
    }
    // 动画循环
    setInterval(updateParticles, 50);
    // 添加发光效果
    const发光Effect = function() {
        const canvas2 = document.createElement('canvas');
        canvas2.width = 800;
        canvas2.height = 600;
        const ctx2 = canvas2.getContext('2d');
        ctx2.fillStyle = '#ffffff';
        ctx2.fillRect(0, 0, canvas2.width, canvas2.height);
        for(let x = 0; x < canvas2.width; x++) {
            for(let y = 0; y < canvas2.height; y++) {
                const distance = Math.sqrt(
                    Math.pow(x - canvas2.width/2, 2) + 
                    Math.pow(y - canvas2.height/2, 2)
                );
                const intensity = Math.exp(-distance * 0.005);
                ctx2.fillStyle = `hsl(${Math.sin(distance * 0.1) * 360}, 70%, ${intensity})`;
                ctx2.fillRect(x, y, 1, 1);
            }
        }
        canvas2.body.appendChild(canvas2);
        canvas2.addEventListener('mousemove', function(e) {
            const centerX = e.clientX;
            const centerY = e.clientY;
            ctx2.fillStyle = `hsl(${Math.sin((centerX - canvas2.width/2) * 0.1) * 360}, 70%, ${Math.exp(-Math.sqrt(
                Math.pow(centerX - canvas2.width/2, 2) + 
                Math.pow(centerY - canvas2.height/2, 2)
            ) * 0.005) * 2})`;
        });
    };
    // 在 updateParticles 函数中调用发光Effect
    updateParticles();
    // 添加发光效果的事件 listeners
    document.addEventListener('mousemove', function(e) {
       发光Effect();
    });
}

这段代码结合了粒子效果和发光效果,创造出一种动态的发光粒子效果,发光效果通过计算每个像素到中心点的距离,动态调整发光强度和颜色,而粒子效果则通过动态更新粒子的位置和速度,形成复杂的动态形态。


JS 基础与高级特效的结合:创建复杂视觉效果

在实际项目中,往往需要将基础特效与高级特效结合,创造出更复杂的视觉效果,以下是一个结合发光效果和粒子效果的代码示例:

function复杂视觉效果() {
    const canvas = document.createElement('canvas');
    canvas.width = 800;
    canvas.height

排行榜
关于我们
「好主机」服务器测评网专注于为用户提供专业、真实的服务器评测与高性价比推荐。我们通过硬核性能测试、稳定性追踪及用户真实评价,帮助企业和个人用户快速找到最适合的服务器解决方案。无论是云服务器、物理服务器还是企业级服务器,好主机都是您值得信赖的选购指南!
快捷菜单1
服务器测评
VPS测评
VPS测评
服务器资讯
服务器资讯
扫码关注
鲁ICP备2022041413号-1