烟花----事件对象event

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./utils.js"></script>
</head>

<body>
    <div id="tips"><a id="auto" href="javascript:;" class="">自动放烟花</a></div>

    <script>
        /*
            (1)对象:烟花 Fire
                【1】创建对象
                【2】描述对象
                    属性:位置(水平和垂直位置),目标值,烟火的数量,爆炸半径
                    方法:
                        初始化:创建节点,添加样式,添加在页面中
                        移动:从底部往光标的位置 动画
                        爆炸:随机生成很多的烟火
                【3】操作对象

            (2)对象:烟火 Spark
                【1】创建对象
                【2】描述对象
                    属性:随机半径,烟火角度,当前的烟火对象,随机背景颜色,位置(left 和top)
                    方法:
                        初始化:创建对象,添加样式,添加在烟花中
                        移动:指定到相应的位置
                        移出:移出整个烟花(让爷爷移出我爸爸)
                【3】操作对象
        */
        window.onload = function () {
            let auto = document.querySelector('#auto');
            let flag = true;
            let timer;

            document.onclick = function (e) {
                if (flag) {
                    new Fire(e.clientX, e.clientY);
                }
            }

            // 自动放烟花
            auto.onclick = function (e) {
                if (flag) {
                    timer = setInterval(() => {
                        new Fire();
                        flag = false;
                    }, 1500)
                } else {
                    flag = true;
                    clearInterval(timer);
                }
                e.stopPropagation()
            }

            // 创建烟花
            function Fire(x, y) {
                this.left = x || getRandomNum(5, window.innerWidth - 5);
                this.top = y || getRandomNum(20, window.innerHeight - 100);
                this.qty = getRandomNum(15, 30);
                this.r = getRandomNum(100, 200);

                // 执行初始化
                this.init()
                this.move();
            }
            Fire.prototype = {
                constructor: Fire,
                init: function () {
                    this.dv = document.createElement('div');
                    this.dv.classList.add('fire');
                    this.dv.style.left = this.left + 'px';
                    document.body.appendChild(this.dv);
                    return this;
                },
                move: function () {
                    move(this.dv, { top: this.top, height: 3 }, () => {
                        this.boom();
                    })
                },
                // 根据烟火个数创建烟火对象那个
                boom: function () {
                    let deg = 360 / this.qty;
                    for (let i = 0; i < this.qty; i++) {
                        this.dv.style.background = 'rgba(0,0,0,0)'
                        // 把角度和半径传到烟火对象中
                        new Spark(deg * i, this.r, this.dv)
                    }
                }
            }
            Object.defineProperty(Fire.prototype, "constructor", { enumerable: false, writable: false });

            function Spark(deg, r, parent) {
                this.deg = deg;
                this.r = r;
                this.bgColor = getRandomColor();
                this.rad = Math.PI * this.deg / 180;
                this.left = this.r * Math.cos(this.rad);
                this.top = this.r * Math.sin(this.rad);
                this.parent = parent;
                this.init().move();
            }
            Spark.prototype = {
                constructor: Spark,
                init: function () {
                    this.span = document.createElement('span');
                    this.span.classList.add('spark');
                    this.span.style.background = this.bgColor;
                    this.parent.appendChild(this.span);
                    return this;
                },
                move: function () {
                    move(this.span, { left: parseInt(this.left), top: parseInt(this.top), opacity: 30 }, () => {
                        this.remove();
                    })
                },
                remove: function () {
                    // 把整个烟花从body的中移除
                    this.parent.remove()
                }
            }
            Object.defineProperty(Spark.prototype, "constructor", { enumerable: false, writable: false });
        }
    </script>
</body>

</html>
        html,
        body {
            overflow: hidden;
            height: 100%;
        }

        body,
        div,
        p {
            margin: 0;
            padding: 0;
        }

        body {
            background: #000;
            font: 12px/1.5 arial;
            color: #7A7A7A;
        }

        a {
            text-decoration: none;
            outline: none;
        }

        #tips,
        #copyright {
            position: absolute;
            width: 100%;
            height: 50px;
            text-align: center;
            background: #171717;
            border: 2px solid #484848;
        }

        #tips {
            top: 0;
            border-width: 0 0 2px;
        }

        #tips a {
            font: 14px/30px arial;
            color: #FFF;
            background: #F06;
            display: inline-block;
            margin: 10px 5px 0;
            padding: 0 15px;
            border-radius: 15px;
        }

        #tips a.active {
            background: #FE0000;
        }

        .fire {
            width: 3px;
            height: 30px;
            background: white;
            position: absolute;
            top: 100%;
        }

        .spark {
            position: absolute;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            left: 0;
            top: 0;
        }
// 生成一个任意范围内的随机整数
function getRandomNum(a, b) {
    if (a > b) {
        return parseInt(Math.random() * (a - b + 1)) + b
    }
    return parseInt(Math.random() * (b - a + 1)) + a
}

// 封装一个随机颜色(不依赖 getRandomNum(a, b))
function getRandomColor(a) {
    if (a === 16) {
        var str = '0123456789abcdef';
        var res = '';
        for (var i = 0; i < 6; i++) {
            var idx = parseInt(Math.random() * 16);
            res += str[idx];
        }
        return '#' + res;
    }

    var r = parseInt(Math.random() * 256);
    var g = parseInt(Math.random() * 256);
    var b = parseInt(Math.random() * 256);
    return 'rgb(' + r + ',' + g + ',' + b + ')'
}

/* 
    封装获取元素的函数:
    函数的实参形式:
    【1】#id名    表示通过id名获取元素
    【2】.class名 表示通过class名获取元素
    【3】标签名    表示同给标签名获取元素
*/
function getEle(ele) {
    // substr(参数1,参数2)
    // 参数1表示开始的索引位置
    // 参数2表示截取个数,如果没有参数2表示从开始位置截取到最后
    var str1 = ele.substr(0, 1); //拿到前面的字符
    var str2 = ele.substr(1);

    if (str1 === '#') {
        return document.getElementById(str2)
    } else if (str1 === '.') {
        return document.getElementsByClassName(str2);
    } else {
        return document.getElementsByTagName(ele);
    }
}

// 获取非行内样式的
function getStyle(ele, attr) {
    var style;
    if (window.getComputedStyle) {
        style = window.getComputedStyle(ele)[attr];
    } else {
        style = ele.currentStyle[attr];
    }
    return style;
}

// 动画函数
/*
    【1】dom节点
    【2】对象,要改变属性和属性值
    【3】回调函数(动画结束之后要执行的操作)
*/
function move(ele, obj, callback) {

    let timerLen = 0;
    for (const attr in obj) {

        // 防止抖动
        clearInterval(ele[attr]);

        timerLen++;
        ele[attr] = setInterval(() => {
            let curStyle;
            if (attr === 'opacity') {
                curStyle = getStyle(ele, attr) * 100;
            } else {
                curStyle = parseInt(getStyle(ele, attr));
            }
            let speed = (obj[attr] - curStyle) / 5;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);  //取整
            if (curStyle === obj[attr]) {
                clearInterval(ele[attr]);
                timerLen--
                if (timerLen === 0) {
                    callback && callback();
                }
            } else {
                if (attr === 'opacity') {
                    ele.style[attr] = (speed + curStyle) / 100;
                } else {
                    ele.style[attr] = speed + curStyle + 'px';
                }
            }
        }, 30)
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您介绍一下新年快乐烟花的Python实现方法。根据您提供的引用内容,这个项目使用了Python的图形模块tkinter来实现烟花的效果。在代码中,通过设置烟花的重力向量和生命周期来模拟烟花在不同时期的展现效果。 由于您没有提供具体的代码示例,我无法给出详细的演示。但是,我可以为您提供一个基本的烟花效果的Python代码示例,供您参考: ```python import tkinter as tk import random # 创建画布 canvas_width = 800 canvas_height = 600 canvas = tk.Canvas(width=canvas_width, height=canvas_height) canvas.pack() # 定义烟花类 class Firework: def __init__(self, x, y): self.x = x self.y = y self.gravity = 0.1 self.lifespan = 100 self.vx = random.uniform(-1, 1) self.vy = random.uniform(-5, -1) self.color = random.choice(['red', 'green', 'blue', 'yellow', 'purple']) def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity self.lifespan -= 1 def draw(self): canvas.create_oval(self.x-2, self.y-2, self.x+2, self.y+2, fill=self.color) # 创建烟花列表 fireworks = [] # 创建烟花函数 def create_firework(event): x = event.x y = event.y fireworks.append(Firework(x, y)) # 更新烟花函数 def update_fireworks(): for firework in fireworks: firework.update() firework.draw() if firework.lifespan <= 0: fireworks.remove(firework) # 绑定鼠标点击事件 canvas.bind('<Button-1>', create_firework) # 主循环 def main_loop(): update_fireworks() canvas.after(10, main_loop) main_loop() canvas.mainloop() ``` 这段代码使用tkinter创建了一个画布,并定义了一个Firework类来表示烟花。通过鼠标点击事件,可以在画布上创建烟花对象,并在主循环中更新和绘制烟花烟花的颜色、位置和运动轨迹都是随机生成的。 希望这个示例能够帮助您实现新年快乐烟花的效果。如果您有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值