首页 > 其他分享 >基于HTML5的爱心表白动画特效

基于HTML5的爱心表白动画特效

时间:2022-11-21 22:05:56浏览次数:64  
标签:function 动画 canvas firstActive 特效 particles length HTML5 var

从视觉上看,它也是一个爱心轮廓,但是不同的是这个爱心轮廓由许多跳动的小爱心组成,更加有一种3D立体的效果。

1. 运行效果

基于HTML5的爱心表白动画特效_i++

2. 代码构成

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>爱心表白</title>

<style>
html, body {
height: 100%;
padding: 0;
margin: 0;
background: #000;
}
canvas {
width: 100%;
height: 100%;
}
</style>

</head>
<body>

<div style="text-align:center;clear:both;">
<script src="/gg_bd_ad_720x90.js" type="text/javascript"></script>
<script src="/follow.js" type="text/javascript"></script>
</div>

<canvas id="pinkboard"></canvas>

<script>
var settings = {
particles: {
length: 500,
duration: 2,
velocity: 100,
effect: -0.75,
size: 30,
},
};

(function(){var b=0;var c=["ms","moz","webkit","o"];for(var a=0;a<c.length&&!window.requestAnimationFrame;++a){window.requestAnimationFrame=window[c[a]+"RequestAnimationFrame"];window.cancelAnimationFrame=window[c[a]+"CancelAnimationFrame"]||window[c[a]+"CancelRequestAnimationFrame"]}if(!window.requestAnimationFrame){window.requestAnimationFrame=function(h,e){var d=new Date().getTime();var f=Math.max(0,16-(d-b));var g=window.setTimeout(function(){h(d+f)},f);b=d+f;return g}}if(!window.cancelAnimationFrame){window.cancelAnimationFrame=function(d){clearTimeout(d)}}}());

var Point = (function() {
function Point(x, y) {
this.x = (typeof x !== 'undefined') ? x : 0;
this.y = (typeof y !== 'undefined') ? y : 0;
}
Point.prototype.clone = function() {
return new Point(this.x, this.y);
};
Point.prototype.length = function(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;
};
Point.prototype.normalize = function() {
var length = this.length();
this.x /= length;
this.y /= length;
return this;
};
return Point;
})();

var Particle = (function() {
function Particle() {
this.position = new Point();
this.velocity = new Point();
this.acceleration = new Point();
this.age = 0;
}
Particle.prototype.initialize = function(x, y, dx, dy) {
this.position.x = x;
this.position.y = y;
this.velocity.x = dx;
this.velocity.y = dy;
this.acceleration.x = dx * settings.particles.effect;
this.acceleration.y = dy * settings.particles.effect;
this.age = 0;
};
Particle.prototype.update = function(deltaTime) {
this.position.x += this.velocity.x * deltaTime;
this.position.y += this.velocity.y * deltaTime;
this.velocity.x += this.acceleration.x * deltaTime;
this.velocity.y += this.acceleration.y * deltaTime;
this.age += deltaTime;
};
Particle.prototype.draw = function(context, image) {
function ease(t) {
return (--t) * t * t + 1;
}
var size = image.width * ease(this.age / settings.particles.duration);
context.globalAlpha = 1 - this.age / settings.particles.duration;
context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
};
return Particle;
})();

var ParticlePool = (function() {
var particles,
firstActive = 0,
firstFree = 0,
duration = settings.particles.duration;

function ParticlePool(length) {
particles = new Array(length);
for (var i = 0; i < particles.length; i++)
particles[i] = new Particle();
}
ParticlePool.prototype.add = function(x, y, dx, dy) {
particles[firstFree].initialize(x, y, dx, dy);

firstFree++;
if (firstFree == particles.length) firstFree = 0;
if (firstActive == firstFree ) firstActive++;
if (firstActive == particles.length) firstActive = 0;
};
ParticlePool.prototype.update = function(deltaTime) {
var i;

if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i++)
particles[i].update(deltaTime);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i++)
particles[i].update(deltaTime);
for (i = 0; i < firstFree; i++)
particles[i].update(deltaTime);
}

while (particles[firstActive].age >= duration && firstActive != firstFree) {
firstActive++;
if (firstActive == particles.length) firstActive = 0;
}


};
ParticlePool.prototype.draw = function(context, image) {
if (firstActive < firstFree) {
for (i = firstActive; i < firstFree; i++)
particles[i].draw(context, image);
}
if (firstFree < firstActive) {
for (i = firstActive; i < particles.length; i++)
particles[i].draw(context, image);
for (i = 0; i < firstFree; i++)
particles[i].draw(context, image);
}
};
return ParticlePool;
})();

(function(canvas) {
var context = canvas.getContext('2d'),
particles = new ParticlePool(settings.particles.length),
particleRate = settings.particles.length / settings.particles.duration, // particles/sec
time;

function pointOnHeart(t) {
return new Point(
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;
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;
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;
})();

function render() {
requestAnimationFrame(render);

var newTime = new Date().getTime() / 1000,
deltaTime = newTime - (time || newTime);
time = newTime;

context.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.update(deltaTime);
particles.draw(context, image);
}

function onResize() {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
}
window.onresize = onResize;

setTimeout(function() {
onResize();
render();
}, 10);
})(document.getElementById('pinkboard'));
</script>

</body>
</html>

以上就是采用HTML代码编写的程序,还可以外加CSS进行点缀,让效果更佳美观。

3. 代码截图

基于HTML5的爱心表白动画特效_html_02

标签:function,动画,canvas,firstActive,特效,particles,length,HTML5,var
From: https://blog.51cto.com/u_15641375/5875308

相关文章

  • 移动端整屏翻动特效
    写在前面:转自:http://www.wfuyu.com/s/jquerygroup/limq12lf.html1.HTML部分:<!DOCTYPEhtml><html><head><metacharset="UTF-8"/><metahttp-equiv="X......
  • 网页动画的12原则,帮你做出漂亮的动画效果
    译者| @EthonLau作者|@donovanh原文| https://cssanimation.rocks/principles/作为前端的设计师和工程师,我们用css去做样式、定位并创建出好看的网站。我们经常用......
  • 动画
    animation[animation-name]:检索或设置对象所应用的动画名称[animation-duration]:检索或设置对象动画的持续时间[animation-timing-function......
  • 烟花特效
    1、 <!DOCTYPEhtml><html><head><metacharset="utf-8"/><title></title><metacharset="utf-8"><title>3D烟花</title><st......
  • 轻松学习jQuery事件和动画
    ✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。......
  • 微信小程序使用Animate.css来实现动画效果
    1、安装官网:https://animate.style/视图--终端,打开终端(新建)npminstallanimate.css--save2、安装完了以后,会多一个文件夹:  打开文件,找到animate.css 3、......
  • 【UE动画】虚幻动画系统
     一.动画简析 1. 早期游戏动画被分为2D和3D两种实现手段2D游戏动画无骨骼的实现手段,最简单暴力,通过图片帧的切换,完成动画系统的动画播放,最贴近动画实质性,切帧播......
  • vue中控制组件过渡动画
     基本用法首先先要给组件添加自定义名字<button@click="show=!show">Toggle</button><Transitionname="自定义名称"> <pv-if="show">hello</p></Transition>......
  • osgearth仿真平台之特效(4)
    osgearth特效主要是开发了圆锥波、菱形波、干扰、通信、爆炸等特效,因为特效开发起来比较麻烦,有时候在osg上效果很好,放到osgearth上效果就不行了,特效如下:卫星轨道的添加: ......
  • 移动端-常见特效
    移动端-常见特效1.classList属性classList属性是HTML5新增的一个属性,返回元素的类名。但是ie10以上版本支持。该属性用于在元素中添加,移除及切换CSS类。有以下方法添加......