html5 canvas五角形星型结构粒子动画特效

html5 canvas五角形星型结构粒子动画特效

html5 canvas五角形星型结构粒子动画特效,红蓝两色的“小蝌蚪”围绕着五角形边框游动的动画效果。

使用方法
在页面调用相关JS代码文件

1
2
3
<script src="js/p5.min.js"></script>
<script src="js/p5.dom.min.js"></script>
<script src="js/p5.sound.min.js"></script>

Javascript代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var blobs=[];
var numBlobs=200;
 
function setup() {
  createCanvas(windowWidth, windowHeight);
  for(var i=0; i<numBlobs; i++){
    blobs.push(new Blob(random(TWO_PI),height/4));
  }
}
 
function draw() {
  background(50,100);
  blobs.forEach(function(blob){
    blob.show(width/2, height/2);
  });
}
 
function Blob(a,r) {
  var rotSteps=random(300,700);
  var aInc=PI/rotSteps;
  var aWobbleInc=TWO_PI/(rotSteps/5);
  var aWobblePhase=random(PI/4);
  var aWobble=0;
  var rWobble=random(r/2, r/4);
  var trail=[];
  var trailMax=8;
  var dir=random(10)<5?-1:1;
   
  this.show=function(x,y){
    noStroke();
    if(dir>0){
      fill(50,100,255,100);
    } else {
      fill(255,100,120,100);
    }
    push();
    translate(x,y);
    rotate(-PI/6);
    var rNow=r+sin(a*5+(aWobblePhase*noise(frameCount/50)*5))*(r/5+rWobble*noise(0.5+frameCount/50));
    var xNow=cos(a)*rNow;
    var yNow=sin(a)*rNow;
    trail.push({x:xNow, y:yNow});
    if(trail.length>trailMax){
      trail.shift();
    }
    // ellipse(0,rNow,10);
    ellipse(xNow,yNow,height/50);
    trail.forEach(function(posThen,i){
      fill(255,i*130/trailMax);
      noStroke();
      ellipse(posThen.x, posThen.y,i*(height/150)/trailMax);
    });
    fill(255);
    ellipse(xNow,yNow,height/250);
    pop();
    a+=aInc*dir;
    aWobble+=aWobbleInc;
  };
}
当前内容只有登录了才能查看,如果您已经注册,请登录
3

发表评论