<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>粒子漩涡特效</title>
<style>
html,body{
margin:0px
width:100%
height:100%
overflow:hidden
background:#000
}
#canvas{
position:absolute
width:100%
height:100%
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
function project3D(x,y,z,vars){
var p,d
x-=vars.camX
y-=vars.camY-8
z-=vars.camZ
p=Math.atan2(x,z)
d=Math.sqrt(x*x+z*z)
x=Math.sin(p-vars.yaw)*d
z=Math.cos(p-vars.yaw)*d
p=Math.atan2(y,z)
d=Math.sqrt(y*y+z*z)
y=Math.sin(p-vars.pitch)*d
z=Math.cos(p-vars.pitch)*d
var rx1=-1000
var ry1=1
var rx2=1000
var ry2=1
var rx3=0
var ry3=0
var rx4=x
var ry4=z
var uc=(ry4-ry3)*(rx2-rx1)-(rx4-rx3)*(ry2-ry1)
var ua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc
var ub=((rx2-rx1)*(ry1-ry3)-(ry2-ry1)*(rx1-rx3))/uc
if(!z)z=0.000000001
if(ua>0&&ua<1&&ub>0&&ub<1){
return {
x:vars.cx+(rx1+ua*(rx2-rx1))*vars.scale,
y:vars.cy+y/z*vars.scale,
d:(x*x+y*y+z*z)
}
}else{
return { d:-1 }
}
}
function elevation(x,y,z){
var dist = Math.sqrt(x*x+y*y+z*z)
if(dist &&z/dist>=-1 &&z/dist <=1) return Math.acos(z / dist)
return 0.00000001
}
function rgb(col){
col += 0.000001
var r = parseInt((0.5+Math.sin(col)*0.5)*16)
var g = parseInt((0.5+Math.cos(col)*0.5)*16)
var b = parseInt((0.5-Math.sin(col)*0.5)*16)
return "#"+r.toString(16)+g.toString(16)+b.toString(16)
}
function interpolateColors(RGB1,RGB2,degree){
var w2=degree
var w1=1-w2
return [w1*RGB1[0]+w2*RGB2[0],w1*RGB1[1]+w2*RGB2[1],w1*RGB1[2]+w2*RGB2[2]]
}
function rgbArray(col){
col += 0.000001
var r = parseInt((0.5+Math.sin(col)*0.5)*256)
var g = parseInt((0.5+Math.cos(col)*0.5)*256)
var b = parseInt((0.5-Math.sin(col)*0.5)*256)
return [r, g, b]
}
function colorString(arr){
var r = parseInt(arr[0])
var g = parseInt(arr[1])
var b = parseInt(arr[2])
return "#"+("0" + r.toString(16) ).slice (-2)+("0" + g.toString(16) ).slice (-2)+("0" + b.toString(16) ).slice (-2)
}
function process(vars){
if(vars.points.length<vars.initParticles) for(var i=0i<5++i) spawnParticle(vars)
var p,d,t
p = Math.atan2(vars.camX, vars.camZ)
d = Math.sqrt(vars.camX * vars.camX + vars.camZ * vars.camZ)
d -= Math.sin(vars.frameNo / 80) / 25
t = Math.cos(vars.frameNo / 300) / 165
vars.camX = Math.sin(p + t) * d
vars.camZ = Math.cos(p + t) * d
vars.camY = -Math.sin(vars.frameNo / 220) * 15
vars.yaw = Math.PI + p + t
vars.pitch = elevation(vars.camX, vars.camZ, vars.camY) - Math.PI / 2
var t
for(var i=0i<vars.points.length++i){
x=vars.points[i].x
y=vars.points[i].y
z=vars.points[i].z
d=Math.sqrt(x*x+z*z)/1.0075
t=.1/(1+d*d/5)
p=Math.atan2(x,z)+t
vars.points[i].x=Math.sin(p)*d
vars.points[i].z=Math.cos(p)*d
vars.points[i].y+=vars.points[i].vy*t*((Math.sqrt(vars.distributionRadius)-d)*2)
if(vars.points[i].y>vars.vortexHeight/2 || d<.25){
vars.points.splice(i,1)
spawnParticle(vars)
}
}
}
function drawFloor(vars){
var x,y,z,d,point,a
for (var i = -25i <= 25i += 1) {
for (var j = -25j <= 25j += 1) {
x = i*2
z = j*2
y = vars.floor
d = Math.sqrt(x * x + z * z)
point = project3D(x, y-d*d/85, z, vars)
if (point.d != -1) {
size = 1 + 15000 / (1 + point.d)
a = 0.15 - Math.pow(d / 50, 4) * 0.15
if (a >0) {
vars.ctx.fillStyle = colorString(interpolateColors(rgbArray(d/26-vars.frameNo/40),[0,128,32],.5+Math.sin(d/6-vars.frameNo/8)/2))
vars.ctx.globalAlpha = a
vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size)
}
}
}
}
vars.ctx.fillStyle = "#82f"
for (var i = -25i <= 25i += 1) {
for (var j = -25j <= 25j += 1) {
x = i*2
z = j*2
y = -vars.floor
d = Math.sqrt(x * x + z * z)
point = project3D(x, y+d*d/85, z, vars)
if (point.d != -1) {
size = 1 + 15000 / (1 + point.d)
a = 0.15 - Math.pow(d / 50, 4) * 0.15
if (a >0) {
vars.ctx.fillStyle = colorString(interpolateColors(rgbArray(-d/26-vars.frameNo/40),[32,0,128],.5+Math.sin(-d/6-vars.frameNo/8)/2))
vars.ctx.globalAlpha = a
vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size)
}
}
}
}
}
function sortFunction(a,b){
return b.dist-a.dist
}
function draw(vars){
vars.ctx.globalAlpha=.15
vars.ctx.fillStyle="#000"
vars.ctx.fillRect(0, 0, canvas.width, canvas.height)
drawFloor(vars)
var point,x,y,z,a
for(var i=0i<vars.points.length++i){
x=vars.points[i].x
y=vars.points[i].y
z=vars.points[i].z
point=project3D(x,y,z,vars)
if(point.d != -1){
vars.points[i].dist=point.d
size=1+vars.points[i].radius/(1+point.d)
d=Math.abs(vars.points[i].y)
a = .8 - Math.pow(d / (vars.vortexHeight/2), 1000) * .8
vars.ctx.globalAlpha=a>=0&&a<=1?a:0
vars.ctx.fillStyle=rgb(vars.points[i].color)
if(point.x>-1&&point.x<vars.canvas.width&&point.y>-1&&point.y<vars.canvas.height)vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size)
}
}
vars.points.sort(sortFunction)
}
function spawnParticle(vars){
var p,ls
pt={}
p=Math.PI*2*Math.random()
ls=Math.sqrt(Math.random()*vars.distributionRadius)
pt.x=Math.sin(p)*ls
pt.y=-vars.vortexHeight/2
pt.vy=vars.initV/20+Math.random()*vars.initV
pt.z=Math.cos(p)*ls
pt.radius=200+800*Math.random()
pt.color=pt.radius/1000+vars.frameNo/250
vars.points.push(pt)
}
function frame(vars) {
if(vars === undefined){
var vars={}
vars.canvas = document.querySelector("canvas")
vars.ctx = vars.canvas.getContext("2d")
vars.canvas.width = document.body.clientWidth
vars.canvas.height = document.body.clientHeight
window.addEventListener("resize", function(){
vars.canvas.width = document.body.clientWidth
vars.canvas.height = document.body.clientHeight
vars.cx=vars.canvas.width/2
vars.cy=vars.canvas.height/2
}, true)
vars.frameNo=0
vars.camX = 0
vars.camY = 0
vars.camZ = -14
vars.pitch = elevation(vars.camX, vars.camZ, vars.camY) - Math.PI / 2
vars.yaw = 0
vars.cx=vars.canvas.width/2
vars.cy=vars.canvas.height/2
vars.bounding=10
vars.scale=500
vars.floor=26.5
vars.points=[]
vars.initParticles=700
vars.initV=.01
vars.distributionRadius=800
vars.vortexHeight=25
}
vars.frameNo++
requestAnimationFrame(function() {
frame(vars)
})
process(vars)
draw(vars)
}
frame()
</script>
</body>
</html>
使用了particles.js
particles.js可以从github网站下载到最新的源码,网址是 https://github.com/VincentGarreau/particles.js/
使用方法非常简单
第一步,在html中引入脚本文件 particles.min.js,这个文件在下载的压缩包里可以找到
<script src="particles.min.js"></script>第二步,在html中放入一个div容器,设置id为particles-js。这个一般放在所有网页元素的最后面就可以。
<div id="particles-js"></div><style type="text/css">
#particles-js {
position: absolute
top:0
width:100%
}
</style>
第三步,设置窗口样式
<style type="text/css">#particles-js {
z-index: -1
position: absolute
top: 0
width: 100%
background: #aaa
}</style>
第四步,脚本生成粒子效果,可以单独放在一个js文件里,也可以放在<script>标签里。无论如何,这段脚本要出现在div容器之后。
particlesJS("particles-js", { "particles": { "number": { "value": 380, "density": { "enable": true, "value_area": 800}
}, "color": { "value": "#ffffff"
}, "shape": { "type": "circle", "stroke": { "width": 0, "color": "#000000"
}, "polygon": { "nb_sides": 5
}, "image": { "src": "img/github.svg", "width": 100, "height": 100
}
}, "opacity": { "value": 0.5, "random": false, "anim": { "enable": false, "speed": 1, "opacity_min": 0.1, "sync": false
}
}, "size": { "value": 3, "random": true, "anim": { "enable": false, "speed": 40, "size_min": 0.1, "sync": false
}
}, "line_linked": { "enable": true, "distance": 150, "color": "#ffffff", "opacity": 0.4, "width": 1
}, "move": { "enable": true, "speed": 6, "direction": "none", "random": false, "straight": false, "out_mode": "out", "bounce": false, "attract": { "enable": false, "rotateX": 600, "rotateY": 1200
}
}
}, "interactivity": { "detect_on": "canvas", "events": { "onhover": { "enable": true, "mode": "grab"
}, "onclick": { "enable": true, "mode": "push"
}, "resize": true
}, "modes": { "grab": { "distance": 140, "line_linked": { "opacity": 1
}
}, "bubble": { "distance": 400, "size": 40, "duration": 2, "opacity": 8, "speed": 3
}, "repulse": { "distance": 200, "duration": 0.4
}, "push": { "particles_nb": 4
}, "remove": { "particles_nb": 2
}
}
}, "retina_detect": true})
background-image不支持CSS3 transitionbackground-image 不支持CSS3 transition ,而CSS3 gradient渐变作为背景图片存在的时候,下面的CSS设置是不会有过渡效果的。
.gradient {
background-image: linear-gradient(to right, olive, green)
transition: background-image 0.5s linear
}
.gradient:hover {
background-image: linear-gradient(to right, green, purple)
}
鼠标hover会发现渐变的变化是很唐突的,一点过渡效果也没有。
下面问题来了,如果我们希望实现渐变hover时候有过渡变化的效果,该如何实现呢?我这里罗列的几种可行的方法。
二、借助background-position实现渐变过渡
background-image 虽然不支持CSS3 transition 过渡,但是 background-position 支持啊,于是,通过控制背景位置,我们是可以实现渐变过渡效果的。
实现效果如下(鼠标hover):
相关代码如下:
.box {
max-width: 400px
height: 200px
background: linear-gradient(to right, olive, green, purple)
background-size: 200%
transition: background-position .5s
}
.box:hover {
background-position: 100% 0
}
三、借助background-color实现渐变过渡
background-image 虽然不支持CSS3 transition 过渡,但是 background-color 支持啊,于是,通过控制背景颜色,和一个颜色呈现技巧,我们也是可以实现渐变过渡效果的。
鼠标hover前后效果对比:
相关代码如下:
.box {
max-width: 400px
height: 200px
background: olive linear-gradient(to right, rgba(0,255,0,0), rgba(0,255,0,.5))
transition: background-color .5s
}
.box:hover {
background-color: purple
}
四、借助伪元素和opacity实现渐变过渡
借助伪元素创建变换后的渐变效果,通过改变覆盖的渐变的opacity透明度变化实现渐变过渡效果。
下图为hover之后的效果:
相关代码如下:
.box {
max-width: 400pxheight: 200px
background: linear-gradient(to right, olive, green)
position: relative
z-index: 0
}
.box::before {
content: ''
position: absolute
left: 0top: 0right: 0bottom: 0
background: linear-gradient(to right, green, purple)
opacity: 0
transition: opacity .5s
z-index: -1
}
.box:hover::before {
opacity: 1
}
五、结束语
以上就是我所知道的几个方法,当然,肯定还有其他更好的实现,欢迎补充。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
html 图片的过渡效果
京东年货节特惠来袭!精华礼盒现购超值
京东
广告
《风尚坐火箭学习vue》-- 第十二章:Vue中的动画transition
174阅读·0评论·1点赞
2021年11月26日
image target behaviour 和image target的关系_CSS3中用background-image实现粒子动画效果
26阅读·0评论·0点赞
2020年11月20日
background-image的用法
13.8W阅读·2评论·33点赞
2016年9月9日
background-image 详解
1.0W阅读·1评论·7点赞
2014年11月17日
html文字图片过度效果,如何在图片上应用css3过渡属性?
510阅读·0评论·0点赞
2021年6月11日
HTML CSS 动画 实现图片过渡与变换(图片不超过边框范围局部放大)
1499阅读·1评论·4点赞
2021年11月25日
京东年货节,小年狂欢开启!酵素饮料现购超划算,速戳~
京东
广告
html 图片的过渡效果,3个CSS3图片过渡动画特效
1880阅读·0评论·1点赞
2021年6月11日
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)