Ajax Loader
×
HTML
<canvas id="c"></canvas>
1
<canvas id="c"></canvas>
 
CSS
body {
1
body {
2
  background: #EEE;
3
}
 
JavaScript
 
1
 
2
###
3
  Text masked spring particles
4
    Author: Kushagra Gour a.k.a. Chin Chang
5
    (@chinchang457)
6
###
7
# requestanimationframe polyfill
8
window.requestAnimFrame = (->
9
  return  window.requestAnimationFrame       || 
10
      window.webkitRequestAnimationFrame || 
11
      window.mozRequestAnimationFrame    || 
12
      window.oRequestAnimationFrame      || 
13
      window.msRequestAnimationFrame     ||  
14
      (callback) ->
15
      window.setTimeout callback, 1000 / 60
16
)();
17
 
18
PI_BY_180 = Math.PI / 180
19
 
20
# variables
21
canvas = null
22
ctx = null
23
num_particles = 500
24
display_list = []
25
last_time = null
26
spring_constant = 0.2
27
damping_constant = 0.08
28
colors = ['#0066CC']
29
W = window.innerWidth
30
H = window.innerHeight
31
cx = W / 2
32
cy = H / 2
33
time = 1
34
 
35
init = (e) ->
36
  canvas = document.getElementById 'c'
37
  ctx = canvas.getContext '2d'
38
  canvas.width = W
39
  canvas.height = H
40
  canvas.addEventListener 'mousemove', onMouseMove
41
 
42
  # create particles
43
  for i in [10...W/2] by 10
44
    for j in [0...10]
45
      display_list.push new Particle(i)
46
 
47
onMouseMove = (e) ->
48
  mx = e.offsetX || e.pageX
49
  my = e.offsetY || e.pageY
50
  time = mx / W
51
      
52
animate = ->
53
  animationLoop()
54
  requestAnimFrame animate
55
 
56
animationLoop = ->
57
  if !last_time then last_time = (new Date).getTime()
58
  current_time = (new Date).getTime()
59
  dt = (current_time - last_time) / 1000
60
  last_time = current_time
61
  window.fps = 1 / dt
62
  
63
  draw()
64
  update()
65
 
66
draw = ->
67
  ctx.fillStyle = "#111"
68
  ctx.fillRect 0, 0, W, H
69
 
70
  for child in display_list
71
    continue if typeof child.draw isnt 'function'
72
    ctx.save();
73
    ctx.translate child.x, child.y unless isNaN child.x or isNaN child.y 
74
    ctx.scale child.scale_x, child.scale_y unless isNaN child.scale_x or isNaN child.scale_y 
75
    ctx.globalAlpha = child.alpha unless isNaN child.alpha 
76
    child.draw()
77
    ctx.restore();
78
 
79
update = ->
80
  # update particles
81
  for child in display_list
82
    child.update() if typeof child.update is 'function'
83
 
84
class Particle
85
  constructor: (@radius = 1) ->
86
    @theta = ~~(Math.random() * 360)
87
    @speed_theta = 0.5 + Math.random() * @radius * 0.005
88
    @alpha = @radius / (W / 2)
89
    if Math.random() > 0.5 then @dir = 1 else @dir = -1
90
    @color = colors[0]
91
    @scale_x = @scale_y = 0.5
92
    
93
  draw: ->
94
    #ctx.drawImage canvas2, 0, 0
95
    ctx.fillStyle = @color
96
    ctx.beginPath()
97
    ctx.arc 0, 0, @radius*0.04, 0, Math.PI*2, true
98
    ctx.fill()
99
 
100
  update: ->
101
    @scale_x += @dir * 0.06 * time
102
    @scale_y += @dir * 0.06 * time
103
    @theta += @speed_theta * time
104
    @x = cx + @radius * Math.cos @theta * PI_BY_180
105
    @y = cy + @radius * Math.sin @theta * PI_BY_180
106
 
107
    if @scale_x > 1.5
108
      @scale_x = 1.5
109
      @dir = -1
110
    else if @scale_x < 0
111
      @scale_x = 0
112
      @dir = 1
113
        
114
  reset: ->
115
    @dir *= -1
116
    @scale_x = @scale_y = 0.5
117
    @color = colors[~~(Math.random() * colors.length)]
118
 
119
  reposition: ->
120
    @ox = @x = ~~(Math.random() * W)
121
    @oy = @y = 100 + ~~(Math.random() * 5)
122
 
123
init()
124
animate()
125
 
 

Test Number 1

CSSDeck G+