基于JavaScript实现新年贺卡特效

 更新时间:2022年01月21日 09:08:46   作者:java李杨勇  
本文介绍了一款超级炫酷的2022新年快乐html网页特效,霓虹的城市夜景和绚烂的烟花很是特别,该html页面还有交互效果,点击鼠标就会呈现烟花绽放的特效。需要的可以参考一下

动图演示

一款超级炫酷的2022新年快乐html网页特效,霓虹的城市夜景和绚烂的烟花很是特别,该html页面还有交互效果,点击鼠标就会呈现烟花绽放的特效,唯美不过如此。图片可以换成自己喜欢的夜景或人物都可以。​

主要代码

图片选择引入:

html, body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            background: #000;
            font-family: Montserrat, sans-serif;
            background-image: url(img/pexels-photo-219692.jpeg);
            background-size: cover;
            background-position: center;
        }

css样式

html, body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            background: #000;
            font-family: Montserrat, sans-serif;
            background-image: url(img/pexels-photo-219692.jpeg);
            background-size: cover;
            background-position: center;
        }
 
        canvas {
            mix-blend-mode: lighten;
            cursor: pointer;
        }
 
        #hero {
            display: inline;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            mix-blend-mode: color-dodge;
        }
 
        #year {
            font-size: 30vw;
            color: #d0d0d0;
            font-weight: bold;
        }
 
        #timeleft {
            color: #fbfbfb;
            font-size: 2.5vw;
            text-align: center;
            font-family: Lora, serif;
        }

Javascirpt

const canvas = document.createElement('canvas'),
        context = canvas.getContext('2d'),
        width = canvas.width = window.innerWidth,
        height = canvas.height = window.innerHeight,
        HalfPI = Math.PI / 2,
        gravity = vector.create(0, 0.35),
        year = document.getElementById('year'),
        timeleft = document.getElementById('timeleft'),
        newyear = new Date('01/01/2020');
 
    let objects = [],
        startFireworks = false,
        newYearAlready = false;
 
    function renderTimeLeft() {
        let date = new Date();
 
        let delta = Math.abs(newyear - date) / 1000;
 
        let hours = Math.floor(delta / 3600) % 24;
        delta -= hours * 3600;
 
        let minutes = Math.floor(delta / 60) % 60;
        delta -= minutes * 60;
 
        let seconds = Math.floor(delta % 60) + 1;
 
        let string = '';
 
        let DaysLeft = Math.floor((newyear - date) / (1000 * 60 * 60 * 24)),
            HoursLeft = `${hours} ${hours > 1 ? 'hours' : 'hour'}`,
            MinutesLeft = `${minutes} ${minutes > 1 ? 'minutes' : 'minute'}`,
            SecondsLeft = `${seconds} ${seconds > 1 ? 'seconds' : 'second'}`;
 
        if (hours > 0) string = `${HoursLeft} & ${MinutesLeft}`;else
        if (minutes > 0) string = `${MinutesLeft} & ${SecondsLeft}`;else
            string = `${SecondsLeft}`;
 
        if (DaysLeft > 0) string = DaysLeft + ' days, ' + string;
        string += ' until 2020';
 
        timeleft.innerHTML = string;
    }
 
    renderTimeLeft();
 
    setInterval(function () {
        let date = new Date();
        if (date >= newyear) {
            if (!newYearAlready) {
                year.innerHTML = '2022';
                startFireworks = true;
                timeleft.innerHTML = 'Happy New Year!';
            }
 
            newYearAlready = true;
        } else renderTimeLeft();
    }, 500);
 
 
    document.body.appendChild(canvas);
 
    function random255() {
        return Math.floor(Math.random() * 100 + 155);
    }
 
    function randomColor() {
        let r = random255(),
            g = random255(),
            b = random255();
        return `rgb(${r}, ${g}, ${b})`;
    }
 
    class PhysicsBody {
        constructor() {
            objects.push(this);
        }
        PhysicsUpdate() {
            this.lastPosition = this.position.duplicate();
            this.position.addTo(this.velocity);
            this.velocity.addTo(gravity);
        }
        deleteObject() {
            objects[objects.indexOf(this)] = undefined;
        }}
 
 
    class firework extends PhysicsBody {
        constructor() {
            super();
            this.position = vector.create(Math.random() * width, height);
 
            let Velocity = vector.create(0, 0);
            Velocity.setLength(Math.random() * 10 + 15);
            Velocity.setAngle(Math.PI * 1.35 + Math.random() * Math.PI * 0.30);
            this.velocity = Velocity;
 
            this.trail = Math.floor(Math.random() * 4) != 1;
            this.trailColor = this.trail ? randomColor() : undefined;
            this.trailWidth = 2;
 
            this.TimeCreated = new Date().getTime();
            this.TimeExpired = this.TimeCreated + (Math.random() * 5 + 7) * 100;
 
            this.BlastParticleCount = Math.floor(Math.random() * 50) + 25;
            this.funky = Math.floor(Math.random() * 5) == 1;
 
            this.exposionColor = randomColor();
        }
 
        draw() {
            context.strokeStyle = this.trailColor;
            context.lineWidth = this.trailWidth;
 
            let p = this.position,
                lp = this.lastPosition;
 
            context.beginPath();
            context.moveTo(lp.getX(), lp.getY());
            context.lineTo(p.getX(), p.getY());
            context.stroke();
        }
 
        funkyfire() {
            var funky = this.funky;
            for (var i = 0; i < Math.floor(Math.random() * 10); i++) {
                new BlastParticle({ firework: this, funky });
            }
        }
 
        explode() {
            var funky = this.funky;
            for (var i = 0; i < this.BlastParticleCount; i++) {
                new BlastParticle({ firework: this, funky });
            }
            this.deleteObject();
        }
 
        checkExpire() {
            let now = new Date().getTime();
            if (now >= this.TimeExpired) this.explode();
        }
 
        render() {
            if (this.trail) this.draw();
            if (this.funky) this.funkyfire();
            this.checkExpire();
        }}
 
 
    class BlastParticle extends PhysicsBody {
        constructor({ firework, funky }) {
            super();
            this.position = firework.position.duplicate();
 
            let Velocity = vector.create(0, 0);
            if (!this.funky) {
                Velocity.setLength(Math.random() * 6 + 2);
                Velocity.setAngle(Math.random() * Math.PI * 2);
            } else {
                Velocity.setLength(Math.random() * 3 + 1);
                Velocity.setAngle(firework.getAngle + Math.PI / 2 - Math.PI * 0.25 + Math.PI * .5);
            }
 
            this.velocity = Velocity;
 
            this.color = firework.exposionColor;
 
            this.particleSize = Math.random() * 4;
 
            this.TimeCreated = new Date().getTime();
            this.TimeExpired = this.TimeCreated + (Math.random() * 4 + 3.5) * 100;
        }
 
        draw() {
            context.strokeStyle = this.color;
            context.lineWidth = this.particleSize;
            let p = this.position,
                lp = this.lastPosition;
 
            context.beginPath();
            context.moveTo(lp.getX(), lp.getY());
            context.lineTo(p.getX(), p.getY());
            context.stroke();
        }
 
        checkExpire() {
            let now = new Date().getTime();
            if (now >= this.TimeExpired) this.deleteObject();
        }
 
        render() {
            this.draw();
            this.checkExpire();
        }}
 
 
    document.body.addEventListener('mousedown', function (e) {
        let color = randomColor();
        for (var i = 0; i < Math.floor(Math.random() * 20) + 25; i++) {
            new BlastParticle({
                firework: {
                    position: vector.create(e.pageX, e.pageY),
                    velocity: vector.create(0, 0),
                    exposionColor: color },
 
                funky: false });
 
        }
    });
 
    setInterval(function () {
        if (!startFireworks) return;
        for (var i = 0; i < Math.floor(Math.random() * 4); i++) {
            new firework();
        }
    }, 500);
 
    function cleanupObjects() {
        objects = objects.filter(o => o != undefined);
    }
 
    function loop() {
        context.fillStyle = 'rgba(0,0,0,0.085)';
        context.fillRect(0, 0, width, height);
 
        let unusedObjectCount = 0;
        objects.map(o => {
            if (!o) {unusedObjectCount++;return;}
            o.PhysicsUpdate();
            o.render();
        });
        if (unusedObjectCount > 100) cleanupObjects();
 
        requestAnimationFrame(loop);
    }
 
    loop();

到此这篇关于基于JavaScript实现新年贺卡特效的文章就介绍到这了,更多相关JavaScript新年贺卡特效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • js获得当前系统日期时间的方法

    js获得当前系统日期时间的方法

    这篇文章主要介绍了js获得当前系统日期时间的方法,涉及javascript操作日期时间的相关技巧,非常简单实用,需要的朋友可以参考下
    2015-05-05
  • JavaScript实现点击出现子菜单效果

    JavaScript实现点击出现子菜单效果

    这篇文章主要为大家详细介绍了JavaScript实现点击出现子菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-02-02
  • JavaScript实现图片缩放功能

    JavaScript实现图片缩放功能

    本文,我们来讲讲如何使用 JavaScript 实现图片的缩放,当然,我们可以类比到其他的元素,比如视频的缩放,文中有详细的代码示例,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-07-07
  • 微信小程序动态生成二维码的实现代码

    微信小程序动态生成二维码的实现代码

    这篇文章主要介绍了微信小程序动态生成二维码的实现代码,需要的朋友可以参考下
    2018-07-07
  • bootstrap table表格客户端分页实例

    bootstrap table表格客户端分页实例

    这篇文章主要为大家详细介绍了bootstrap table表格客户端分页实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • js showModalDialog 弹出对话框的简单实例(子窗体)

    js showModalDialog 弹出对话框的简单实例(子窗体)

    本篇文章主要是对js_showModalDialog弹出对话框的简单实例(子窗体) 进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • JS数组操作大全对象数组根据某个相同的字段分组

    JS数组操作大全对象数组根据某个相同的字段分组

    这篇文章主要介绍了JS数组操作大全对象数组根据某个相同的字段分组,需要注意的是,在开发过程这种数组的处理函数,应当被编写到项目的公共工具函数库中全局调用,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • OpenLayers3实现地图鹰眼以及地图比例尺的添加

    OpenLayers3实现地图鹰眼以及地图比例尺的添加

    这篇文章主要为大家详细介绍了OpenLayers3实现地图鹰眼以及地图比例尺的添加,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Javascript 正则表达式校验数字的简单实例

    Javascript 正则表达式校验数字的简单实例

    下面小编就为大家带来一篇Javascript 正则表达式校验数字的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • js改变img标签的src属性在IE下没反应的解决方法

    js改变img标签的src属性在IE下没反应的解决方法

    在Chrome FF里都能改变成功,但在IE下却不行,网上搜了半天,大概了解了,这个是IE的一个bug,具体的解决方法如下,有类似问题的朋友可以参考下哈,希望对大家有所帮助
    2013-07-07

最新评论