gsap实现图片转换成粒子动画效果代码

代码语言:html

所属分类:粒子

代码描述:gsap实现图片转换成粒子动画效果代码

代码标签: 换成 粒子 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开


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

<head>

  <meta charset="UTF-8">
  

  
  
  
  
<style>
body,html{width:100%;height:100%;overflow: hidden;background: black;}
canvas{
	position: absolute;
	top:50%;
	left:50%;
	transform: translate(-50%,-50%);
  border: 1px solid white;
}
</style>



</head>

<body translate="no" >
  <canvas id="scene"></canvas>
<!-- Relaunch the pen if you missed the animation ;) -->
    <script src="https://static.codepen.io/assets/common/stopExecutionOnTimeout-157cd5b220a5c80d4ff8e0e70ac069bffd87a61252088146915e8726e5d9f147.js"></script>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.5.2.js"></script>
  
      <script>
var canvas = document.getElementById("scene");
var ctx = canvas.getContext("2d");
var particles = [];

function drawScene() {
  canvas.width = png.width * 6;
  canvas.height = png.height * 6;

  ctx.drawImage(png, 0, 0);

  var data = ctx.getImageData(0, 0, png.width, png.height);
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  ctx.fillStyle = "white";

  for (var y = 0, y2 = data.height; y < y2; y++) {
    for (var x = 0, x2 = data.width; x < x2; x++) {
      if (data.data[y * 4 * data.width + x * 4 + 3] > 128) {
        var particle = {
          x0: x,
          y0: y,
          x1: png.width / 2,
          y1: png.height / 2,
          speed: Math.random() * 4 + 2 };

        gsap.to(particle, {
          duration: particle.speed,
          x1: particle.x0,
          y1: particle.y0,
          delay: y / 30,
          ease: Elastic.easeOut });

        particles.push(particle);
      }
    }
  }

  requestAnimationFrame(render);

}
var render = function () {
  requestAnimationFrame(render);

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for (var i = 0, j = particles.length; i < j; i++) {
    var particle = particles[i];
    ctx.fillRect(particle.x1 * 6, particle.y1 * 6, 3, 3);
  }

};

var png = new Image();
png.onload = drawScene;
png.src = "data:image/png;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