首页 > web前端 > js教程 > 实例详解jQuery和HTML5实现WebGL高性能烟花绽放动画效果

实例详解jQuery和HTML5实现WebGL高性能烟花绽放动画效果

小云云
发布: 2017-12-30 15:27:34
原创
2553 人浏览过

本文主要介绍了jQuery+HTML5实现WebGL高性能烟花绽放动画效果,可实现烟花升空爆炸的绚丽动画效果,完成以后非常好看,希望能帮助到大家掌握jQuery和HTML5实现WebGL高性能烟花绽放动画效果。

运行效果:

完整代码如下:


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

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

<!DOCTYPE html>

<html class=" -webkit- js flexbox canvas canvastext webgl no-touch geolocation postmessage websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths">

<head>

<meta charset="UTF-8">

<meta name="viewport" content=" initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<meta name="format-detection" content="telephone=no">

<title>HTML5/WebGL高性能烟花绽放动画</title>

<meta name="description" content="HTML5/WebGL高性能烟花绽放动画">

<meta name="keywords" content="HTML5/WebGL高性能烟花绽放动画">

<style>

canvas {

  position: absolute;

  top: 0;

  left: 0;

  background-color: #111;

}

</style>

</head>

<body>

<script src="jquery-1.11.1.min.js"></script>

<canvas id="c" width="1440" height="304"></canvas><script src="prefixfree.min.js"></script><script src="modernizr.js"></script><script>var gl = c.getContext(&#39;webgl&#39;, {

    preserveDrawingBuffer: true

  }),

  w = c.width = window.innerWidth,

  h = c.height = window.innerHeight

, webgl = {}, opts = {

  projectileAlpha: .8,

  projectileLineWidth: 1.3,

  fireworkAngleSpan: .5,

  baseFireworkVel: 3,

  addedFireworkVel: 3,

  gravity: .03,

  lowVelBoundary: -.2,

  xFriction: .995,

  baseShardVel: 1,

  addedShardVel: .2,

  fireworks: 1000,

  baseShardsParFirework: 10,

  addedShardsParFirework: 10,

  shardFireworkVelMultiplier: .3,

  initHueMultiplier: 1 / 360,

  runHueAdder: .1 / 360

}

webgl.vertexShaderSource = `

uniform int u_mode;

uniform vec2 u_res;

attribute vec4 a_data;

varying vec4 v_color;

vec3 h2rgb( float h ){

 return clamp( abs( mod( h * 6. + vec3( 0, 4, 2 ), 6. ) - 3. ) -1., 0., 1. );

}

void clear(){

 gl_Position = vec4( a_data.xy, 0, 1 );

 v_color = vec4( 0, 0, 0, a_data.w );

}

void draw(){

 gl_Position = vec4( vec2( 1, -1 ) * ( ( a_data.xy / u_res ) * 2. - 1. ), 0, 1 );

 v_color = vec4( h2rgb( a_data.z ), a_data.w );

}

void main(){

 if( u_mode == 0 )

 draw();

 else

 clear();

}

`;

webgl.fragmentShaderSource = `

precision mediump float;

varying vec4 v_color;

void main(){

 gl_FragColor = v_color;

}

`;

webgl.vertexShader = gl.createShader(gl.VERTEX_SHADER);

gl.shaderSource(webgl.vertexShader, webgl.vertexShaderSource);

gl.compileShader(webgl.vertexShader);

webgl.fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);

gl.shaderSource(webgl.fragmentShader, webgl.fragmentShaderSource);

gl.compileShader(webgl.fragmentShader);

webgl.shaderProgram = gl.createProgram();

gl.attachShader(webgl.shaderProgram, webgl.vertexShader);

gl.attachShader(webgl.shaderProgram, webgl.fragmentShader);

gl.linkProgram(webgl.shaderProgram);

gl.useProgram(webgl.shaderProgram);

webgl.dataAttribLoc = gl.getAttribLocation(webgl.shaderProgram, &#39;a_data&#39;);

webgl.dataBuffer = gl.createBuffer();

gl.enableVertexAttribArray(webgl.dataAttribLoc);

gl.bindBuffer(gl.ARRAY_BUFFER, webgl.dataBuffer);

gl.vertexAttribPointer(webgl.dataAttribLoc, 4, gl.FLOAT, false, 0, 0);

webgl.resUniformLoc = gl.getUniformLocation(webgl.shaderProgram, &#39;u_res&#39;);

webgl.modeUniformLoc = gl.getUniformLocation(webgl.shaderProgram, &#39;u_mode&#39;);

gl.viewport(0, 0, w, h);

gl.uniform2f(webgl.resUniformLoc, w, h);

gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);

gl.enable(gl.BLEND);

gl.lineWidth(opts.projectileLineWidth);

webgl.data = [];

webgl.clear = function() {

  gl.uniform1i(webgl.modeUniformLoc, 1);

  var a = .1;

  webgl.data = [-1, -1, 0, a,

    1, -1, 0, a, -1, 1, 0, a, -1, 1, 0, a,

    1, -1, 0, a,

    1, 1, 0, a

  ];

  webgl.draw(gl.TRIANGLES);

  gl.uniform1i(webgl.modeUniformLoc, 0);

  webgl.data.length = 0;

}

webgl.draw = function(glType) {

  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(webgl.data), gl.STATIC_DRAW);

  gl.drawArrays(glType, 0, webgl.data.length / 4);

}

var fireworks = [],

  tick = 0,

  sins = [],

  coss = [],

  maxShardsParFirework = opts.baseShardsParFirework + opts.addedShardsParFirework,

  tau = 6.283185307179586476925286766559;

for (var i = 0; i < maxShardsParFirework; ++i) {

  sins[i] = Math.sin(tau * i / maxShardsParFirework);

  coss[i] = Math.cos(tau * i / maxShardsParFirework);

}

function Firework() {

  this.reset();

  this.shards = [];

  for (var i = 0; i < maxShardsParFirework; ++i)

    this.shards.push(new Shard(this));

}

Firework.prototype.reset = function() {

  var angle = -Math.PI / 2 + (Math.random() - .5) * opts.fireworkAngleSpan,

    vel = opts.baseFireworkVel + opts.addedFireworkVel * Math.random();

  this.mode = 0;

  this.vx = vel * Math.cos(angle);

  this.vy = vel * Math.sin(angle);

  this.x = Math.random() * w;

  this.y = h;

  this.hue = tick * opts.initHueMultiplier;

}

Firework.prototype.step = function() {

  if (this.mode === 0) {

    var ph = this.hue,

      px = this.x,

      py = this.y;

    this.hue += opts.runHueAdder;

    this.x += this.vx *= opts.xFriction;

    this.y += this.vy += opts.gravity;

    webgl.data.push(

      px, py, ph, opts.projectileAlpha * .2,

      this.x, this.y, this.hue, opts.projectileAlpha * .2);

    if (this.vy >= opts.lowVelBoundary) {

      this.mode = 1;

      this.shardAmount = opts.baseShardsParFirework + opts.addedShardsParFirework * Math.random() | 0;

      var baseAngle = Math.random() * tau,

        x = Math.cos(baseAngle),

        y = Math.sin(baseAngle),

        sin = sins[this.shardAmount],

        cos = coss[this.shardAmount];

      for (var i = 0; i < this.shardAmount; ++i) {

        var vel = opts.baseShardVel + opts.addedShardVel * Math.random();

        this.shards[i].reset(x * vel, y * vel)

        var X = x;

        x = x * cos - y * sin;

        y = y * cos + X * sin;

      }

    }

  } else if (this.mode === 1) {

    this.ph = this.hue

    this.hue += opts.runHueAdder;

    var allDead = true;

    for (var i = 0; i < this.shardAmount; ++i) {

      var shard = this.shards[i];

      if (!shard.dead) {

        shard.step();

        allDead = false;

      }

    }

    if (allDead)

      this.reset();

  }

}

function Shard(parent) {

  this.parent = parent;

}

Shard.prototype.reset = function(vx, vy) {

  this.x = this.parent.x;

  this.y = this.parent.y;

  this.vx = this.parent.vx * opts.shardFireworkVelMultiplier + vx;

  this.vy = this.parent.vy * opts.shardFireworkVelMultiplier + vy;

  this.starty = this.y;

  this.dead = false;

  this.tick = 1;

}

Shard.prototype.step = function() {

  this.tick += .05;

  var px = this.x,

    py = this.y;

  this.x += this.vx *= opts.xFriction;

  this.y += this.vy += opts.gravity;

  var proportion = 1 - (this.y - this.starty) / (h - this.starty);

  webgl.data.push(

    px, py, this.parent.ph, opts.projectileAlpha / this.tick,

    this.x, this.y, this.parent.hue, opts.projectileAlpha / this.tick);

  if (this.y > h)

    this.dead = true;

}

function anim() {

  window.requestAnimationFrame(anim)

  webgl.clear();

  ++tick;

  if (fireworks.length < opts.fireworks)

    fireworks.push(new Firework);

  fireworks.map(function(firework) {

    firework.step();

  });

  webgl.draw(gl.LINES);

}

anim();

window.addEventListener(&#39;resize&#39;, function() {

  w = c.width = window.innerWidth;

  h = c.height = window.innerHeight;

  gl.viewport(0, 0, w, h);

  gl.uniform2f(webgl.resUniformLoc, w, h);

})

window.addEventListener(&#39;click&#39;, function(e) {

  var firework = new Firework();

  firework.x = e.clientX;

  firework.y = e.clientY;

  firework.vx = 0;

  firework.vy = 0;

  fireworks.push(firework);

});

</script>

</body>

</html>

登录后复制

相关推荐:

实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码_html5教程技巧

HTML5和Webkit实现树叶飘落动画

实例详解基于jQuery实现图片推拉门动画效果

以上是实例详解jQuery和HTML5实现WebGL高性能烟花绽放动画效果的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板