七夕表白,不给女朋友来场炫酷的烟花?

简介: 七夕表白,不给女朋友来场炫酷的烟花?

canvas + JS 炫酷实现烟花效果

一、效果图

二、代码

1、创建画布

<canvas id="canvas">Canvas is not supported in your browser.</canvas>

2、浏览器兼容

window.requestAnimFrame=(function(){
  return window.requestAnimationFrame||
    window.webkitRequestAnimationFrame||
    window.mozRequestAnimationFrame||
    function(callback){
      window.setTimeout(callback,1000/60)
      }
  })();

3、创建烟花对象

function Firework(sx,sy,tx,ty){
    this.x=sx;
    this.y=sy;
    this.sx=sx;
    this.sy=sy;
    this.tx=tx;
    this.ty=ty;
    //计算发射点和绽放点的距离
    this.distanceToTarget=calculateDistance(sx,sy,tx,ty);
    //运动距离
    this.distanceTraveled=0;
    //生成的运动轨迹
    this.coordinates=[];
    this.coordinateCount=3;
    while(this.coordinateCount--){
        this.coordinates.push([this.x,this.y])
    }
    this.angle=Math.atan2(ty-sy,tx-sx);
    this.speed=2;
    this.acceleration=1.05;
    this.brightness=random(50,70);//烟花的亮度
    this.targetRadius=1//烟花圈的半径
}

4、更新烟花位置

Firework.prototype.update=function(index){
    this.coordinates.pop();
    this.coordinates.unshift([this.x,this.y]);
    if(this.targetRadius<8){
        this.targetRadius+=0.3
    }else{
        this.targetRadius=1
    }
    this.speed*=this.acceleration;
    var vx=Math.cos(this.angle)*this.speed,
        vy=Math.sin(this.angle)*this.speed;
    this.distanceTraveled=calculateDistance(this.sx,this.sy,this.x+vx,this.y+vy);
    //如果烟花运行距离大于或等于初始位置到目标位置之间的距离,生成新烟花并移除当前烟花,否则更新烟花位置
    if(this.distanceTraveled>=this.distanceToTarget){
        //烟花绽放
        createParticles(this.tx,this.ty);
        //销毁烟花点
        fireworks.splice(index,1)
    }else{
        this.x+=vx;this.y+=vy
    }
};

5、烟花射线发射效果

Firework.prototype.draw=function(){
    // 重新开始新路径,把之前的路径都清空掉
    ctx.beginPath();
    //先保存一个坐标
    ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
    //从moveTo提供的坐标绘制到指定坐标
    ctx.lineTo(this.x,this.y);
    //设置线条样式
    ctx.strokeStyle="hsl("+hue+", 100%, "+this.brightness+"%)";
    //通过此函数将以上绘制的图形绘制到画布上
    ctx.stroke();
    ctx.beginPath();
    //画出鼠标点击时的圆圈
    //arc(圆心的x坐标,圆心的y坐标,圆的半径,起始角(以弧度计,即l圆心的3点钟位置是0度),结束角,规定应该是顺时针还是逆时针画图(可选))
    ctx.arc(this.tx,this.ty,this.targetRadius,0,Math.PI*2);
    //描边绘制
    ctx.stroke()
};

6、烟花粒子绽放方法定义

function Particle(x,y){
    this.x=x;this.y=y;this.coordinates=[];
    this.coordinateCount=5;
    while(this.coordinateCount--){
        this.coordinates.push([this.x,this.y])
    }
    // 往各个角度绽放
    this.angle=random(0,Math.PI*2);
    this.speed=random(1,10);
    this.friction=0.95;
    this.gravity=1;
    this.hue=random(hue-20,hue+20);
    this.brightness=random(50,80);
    this.alpha=1;
    this.decay=random(0.015,0.03);//粒子消失时间(透明度增加速度)
}

7、烟花粒子绽放

// 烟花绽放 
Particle.prototype.draw=function(){
    ctx.beginPath();
    ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
    ctx.lineTo(this.x,this.y);
    //烟花的颜色
    ctx.strokeStyle="hsla("+this.hue+", 100%, "+this.brightness+"%, "+this.alpha+")";
    ctx.stroke()
};

三、完整代码

1、GitHub地址直接复制:

https://github.com/yongtaozheng/html-css-js-Achieve-cool-results.git

2、个人博客

http://47.113.84.128:8090/

目录
相关文章
|
前端开发 JavaScript
HTML+CSS+JAVASCRIPT实现——情人节表白情书
本文主要介绍如何使用HTML三件套来实现制作一封情人节表白情书,富含情谊与爱,打动女生的心灵
699 2
HTML+CSS+JAVASCRIPT实现——情人节表白情书
|
3月前
情人节浪漫3D照片墙【附源码】
情人节浪漫3D照片墙【附源码】
41 4
|
4月前
|
前端开发 安全 JavaScript
html+css+js实现的新年烟花
html+css+js实现的新年烟花
53 0
除夕最炫烟花代码
除夕最炫烟花代码
104 0
【代码分享】【像极了恋爱】甜甜的汤圆,祝丽姿元宵快乐(表白特效)
【代码分享】【像极了恋爱】甜甜的汤圆,祝丽姿元宵快乐(表白特效)
104 0
|
前端开发 机器人 程序员
六一 特效~ 你也是大小孩
六一 特效~ 你也是大小孩
98 0
|
C语言 C++
C/C++实现跨年表白烟花
C/C++实现跨年表白烟花
371 0
浪漫的表白
浪漫的表白
149 0
|
小程序 开发工具
樱花飘落模拟器-情人节祝你表白成功
看着樱花缓缓的飘落,然后不觉间竟下起了绵绵的细雨。因为今天我所在的城市正下着小雨,所以就在这个小应用中增加了阵阵的细雨功能。 下面我们就学习一下如何实现一个这样温暖的小程序。 首先准备一下素材。一个粉色的背景,两个樱花花瓣,一个模拟雨滴的长方形,以及两句要显示的话。
121 0
|
传感器 前端开发 芯片
画一个 “月饼” 陪我过中秋,使用 ESP32-C3 制作炫彩月饼
今年的中秋节又要到了,各平台活动也搞起来了,我有点坐不住了,作为一个嵌入式工程师,我可以做一个实实在在的月饼,拿在手上陪我们过中秋!
461 1
画一个 “月饼” 陪我过中秋,使用 ESP32-C3 制作炫彩月饼