Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
UI
|
输入
|
媒体
|
导航
|
其他
|
网页模板
|
APP模板
|
常用代码
|
在线代码
背景
对话框和灯箱
筛选及排序
反馈
弹出层
悬停
布局
图表
加载
圆边
滚动
标签
文本链接
工具提示
网络类型
拾色器
定制和风格
日期和时间
拖和放
通用输入
自动完成
密码
投票率
搜索
选择框
快捷键
触摸
丰富的输入
上传
验证
音频和视频
幻灯片和轮播图
图片展示
图像
地图
滑块和旋转
Tabs
水平导航
垂直导航
文件树
分页
手风琴菜单
其他导航
动画效果
浏览器调整
移动
独立的部件
杂项
游戏
PROMULGATOR
qiqi1112
四川省成都市
关注作者
(2)
收藏此代码
(16)
← 好看的css滚动条
→ CSS会动的大嘴鸟动画
相关代码
canvas
画ladingview
canvas
画图板
canvas
画板
canvas
绘图
canvas
炫酷时钟
canvas
时针
canvas
雪花
Html
Css
Js
body,html { overflow:hidden; }
var canvas = document.getElementById("canvas") var ctx = canvas.getContext("2d") function onResize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } window.onresize = onResize; var settings = { particles: { length: 500, // 最大颗粒量 duration: 2, // 粒子持续时间(秒) velocity: 100, // 粒子速度(像素/秒) effect: -0.75, // play with this for a nice effect size: 30, // 颗粒尺寸(像素) }, }; var time, particleRate = settings.particles.length / settings.particles.duration; class bagainPoint { constructor(x, y) { this.x = (typeof x !== 'undefined') ? x : 0; this.y = (typeof y !== 'undefined') ? y : 0; } clone() { return new bagainPoint(this.x, this.y) } length(length) { if (typeof length == 'undefined') return Math.sqrt(this.x * this.x + this.y * this.y); this.normalize(); this.x *= length; this.y *= length; return this } normalize() { var length = this.length(); this.x /= length; this.y /= length; return this } } class Point { constructor() { this.x = new bagainPoint().x; this.y = new bagainPoint().y; this.velocitx = new bagainPoint().x; this.velocity = new bagainPoint().y; this.accelerationx = new bagainPoint().x; this.accelerationy = new bagainPoint().y; this.age = .1; } initialize(x, y, dx, dy) { this.x = x; this.y = y; this.velocitx = dx; this.velocity = dy; this.accelerationx = dx * settings.particles.effect; this.accelerationy = dy * settings.particles.effect; this.age = 0.1; } draw() { function ease(t) { return (--t) * t * t + 1; } var size = image.width * ease(this.age / settings.particles.duration); ctx.globalAlpha = 1 - this.age / settings.particles.duration; ctx.drawImage(image, this.x - size / 2, this.y - size / 2, size, size); } updata(deltaTime) { this.x += this.velocitx * deltaTime; this.y += this.velocity * deltaTime; this.velocitx += this.accelerationx * deltaTime; this.velocity += this.accelerationy * deltaTime; this.age += deltaTime; } } class PointPool { constructor(length) { this.particles = new Array(length); for (var i = 0; i < this.particles.length; i++) { this.particles[i] = new Point(); } this.firstActive = 0 this.firstFree = 0 this.duration = settings.particles.duration; } add(x, y, dx, dy) { this.particles[this.firstFree].initialize(x, y, dx, dy); // handle circular queue this.firstFree++; if (this.firstFree == this.particles.length) this.firstFree = 0; if (this.firstActive == this.firstFree) this.firstActive++; if (this.firstActive == this.particles.length) this.firstActive = 0; } updata(deltaTime) { var i; if (this.firstActive < this.firstFree) { for (i = this.firstActive; i < this.firstFree; i++) this.particles[i].updata(deltaTime); } if (this.firstFree < this.firstActive) { for (i = this.firstActive; i < this.particles.length; i++) this.particles[i].updata(deltaTime); for (i = 0; i < this.firstFree; i++) this.particles[i].updata(deltaTime); } while (this.particles[this.firstActive].age >= this.duration && this.firstActive != this.firstFree) { this.firstActive++; if (this.firstActive == this.particles.length) this.firstActive = 0; } } draw(ctx, image) { if (this.firstActive < this.firstFree) { for (let i = this.firstActive; i < this.firstFree; i++) this.particles[i].draw(ctx, image); } if (this.firstFree < this.firstActive) { for (let i = this.firstActive; i < this.particles.length; i++) this.particles[i].draw(ctx, image); for (let i = 0; i < this.firstFree; i++) this.particles[i].draw(ctx, image); } } } // 星星运动轨迹 function length(dir, length) { let lx = '' let ly = JSON.parse(JSON.stringify(dir)) lx = Math.sqrt(ly.x * ly.x + ly.y * ly.y); ly.x /= lx * length ly.y /= lx * length return ly; } //星星的XY轴关系 function pointOnHeart(t) { return new bagainPoint( 160 * Math.pow(Math.sin(t), 3), 130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25 ); } // 先画一个小星星 var image = (function() { var canvas = document.createElement('canvas'), context = canvas.getContext('2d'); canvas.width = settings.particles.size; canvas.height = settings.particles.size; // Helper函数来创建路径 function to(t) { var point = pointOnHeart(t); point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350; point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350; return point; } // 创建路径 context.beginPath(); var t = -Math.PI; var point = to(t); context.moveTo(point.x, point.y); while (t < Math.PI) { t += 0.01; // baby steps! point = to(t); context.lineTo(point.x, point.y); } context.closePath(); // 创建填充 context.fillStyle = '#ea80b0'; context.fill(); // 创建图片 var image = new Image(); image.src = canvas.toDataURL(); return image; })() var particles = new PointPool(settings.particles.length) function render() { requestAnimationFrame(render); var newTime = new Date().getTime() / 1000, deltaTime = newTime - (time || newTime); time = newTime; ctx.clearRect(0, 0, canvas.width, canvas.height); var amount = particleRate * deltaTime; for (var i = 0; i < amount; i++) { var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random()); var dir = pos.clone().length(settings.particles.velocity); particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y); } particles.updata(deltaTime); particles.draw(); } window.onload = function() { onResize() render() }
↑上面代码改变,会自动显示代码结果 jQuery调用版本:
1.11.3
立即下载
爱心冲击,表白代码
代码描述:点燃我,温暖你,canvas爱心代码
更新时间:2022-11-13 22:46:36
0
最新
发表评论
全部评论
暂时没有评论!
登录后才可以评论
30秒后在评论吧!
发表评论
回复
取消回复
<!doctype html> <html> <head> <meta charset="utf-8"> <title>爱心冲击,表白代码-jq22.com</title> <script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号