JSDM

HTML

 
1
<p>Use arrow keys to move</p>

CSS

x
 
1
html{
2
  background-color: black;
3
  color: #ccc;
4
  text-align: center;
5
  overflow: hidden;
6
}
7
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
191
 
1
// Interesting fact! These are not more colorful when moving, that is an optical illusion.
2
3
// requestAnimFrame() As explained by Mr Irish: http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
4
window.requestAnimFrame = (function(){
5
  return  window.requestAnimationFrame       ||
6
    window.webkitRequestAnimationFrame ||
7
    window.mozRequestAnimationFrame    ||
8
    window.oRequestAnimationFrame      ||
9
    window.msRequestAnimationFrame     ||
10
    function( callback ){
11
    window.setTimeout(callback, 1000 / 60);
12
  };
13
})();
14
15
var starfield = (function() {
16
  // Variables - a quantity which during a calculation is assumed to vary or be capable of varying in value
17
  var stars = [],
18
      star_density = 25,
19
      velocity = {x:0, y: 0},
20
      star_colors = ["rgba(0,0,0,.5)", "#ffe9c4", "#d4fbff"],
21
      stars_bg = "rgba(0,0,0,.5)",
22
      frame,
23
      star_canvas,
24
      star_context,
25
      viewport_width,
26
      viewport_height; 
27
28
  function initialize() {
29
    // Make canvas
30
    star_canvas = document.createElement('canvas');
31
    star_context = star_canvas.getContext('2d');
32
33
    // Get viewport size and resize canvas
34
    resize_canvas();
35
       
36
    // Draw all the stars, I decided stars come in units of 10 
37
    for (var i = 0; i < (star_density*10); i++) {
38
      var rad = Math.random() * 2;
39
      create_star(rad);
40
      // twice as many small stars as big
41
      var rad = Math.random();
42
      create_star(rad);
43
      create_star(rad);
44
    }
45
   
46
    // Put the canvas on the page
47
    document.body.appendChild(star_canvas);
48
    
49
    // When you press keys stuff happens 
50
    document.addEventListener('keydown', function(e) {
51
      e = e || window.event;
52
      // We add to the existing velocity, this dampens the speed changes and makes changing directions more gradual just like flying a real spaceship... I imagine
53
      if (e.keyCode == 39) {
54
        velocity = {
55
          x: velocity.x -5, 
56
          y: velocity.y
57
        };
58
      }
59
      if (e.keyCode == 37) {
60
        velocity = {
61
          x: velocity.x +5, 
62
          y: velocity.y
63
        };
64
      }
65
      if (e.keyCode == 40) {
66
        velocity = {
67
          x: velocity.x, 
68
          y: velocity.y -5
69
        };
70
      }
71
      if (e.keyCode == 38) {
72
        velocity = {
73
          x: velocity.x,
74
          y: velocity.y +5
75
        };
76
      }
77
    }, false);
78
    
79
    // Drawing our first frame starts the drawing loop
80
    draw_frame();
81
82
    function clear_canvas() {
83
      // Each frame the canvas is painted wiht semi transparent black - this gives the trail effect behind moving stars
84
      star_context.fillStyle=stars_bg;
85
      star_context.fillRect(0, 0, viewport_width, viewport_height);
86
      
87
    }
88
89
    function draw_star() {
90
      var s = stars.length;
91
      // for every star
92
      while(s--) {
93
        var star = stars[s];
94
        // update individual stars position
95
        star.update();
96
        // render the star to the canvas
97
        star.render(star_context);
98
      }
99
    }
100
101
102
    function create_star(rad) {
103
      // I don't really need a function for create_star, but reads better and easy to expand upon
104
      stars.push(new star(rad));
105
    }
106
107
    function draw_frame() {
108
      clear_canvas();
109
      // The infinate loop!
110
      frame = requestAnimFrame(draw_frame);
111
      draw_star();
112
    }
113
114
  }
115
116
  function resize_canvas(){
117
    viewport_width = window.innerWidth;
118
    viewport_height = window.innerHeight;
119
    star_canvas.width = viewport_width;
120
    star_canvas.height = viewport_height;
121
  }
122
123
124
  var star = function(rad) {
125
    
126
    this.alpha    = Math.round((Math.random() * 100 - 70) + 70); // Random brightness
127
    this.radius = rad || Math.random() * 2; // Radius
128
    this.color    = star_colors[Math.round(Math.random() * star_colors.length)]; // Random color from array
129
130
    this.pos = {
131
      // Initial random position
132
      x: Math.random() * viewport_width, 
133
      y: Math.random() * viewport_height
134
    };
135
136
137
  };
138
139
  star.prototype = {
140
141
    update: function() {
142
143
      // Depending on the radius the star will move at a differnt speed (slower where a greater perception of depth) 
144
      // Yes! 3 is a magic number :)
145
      this.pos.y += velocity.y === 0 ? velocity.y : (velocity.y / (3 - this.radius));
146
      this.pos.x += velocity.x === 0 ? velocity.x : (velocity.x / (3 - this.radius));
147
      
148
      // Keep the stars on the canvas
149
      if(this.pos.y > viewport_height){
150
        this.pos.y = 0;        
151
      } else if(this.pos.y < 0){
152
        this.pos.y = viewport_height;
153
      }
154
      // Keep the stars on the canvas in a different direction
155
      if(this.pos.x > viewport_width){
156
        this.pos.x = 0;        
157
      } else if(this.pos.x < 0){
158
        this.pos.x = viewport_width;        
159
      }
160
      // Dampen the velocity, ie slow down when you stop telling it to move
161
      velocity.x = velocity.x /1.00005;
162
      velocity.y = velocity.y /1.00005;
163
    },
164
165
    render: function(context) {
166
      // Draw the star at its current position
167
      var x = Math.round(this.pos.x),
168
          y = Math.round(this.pos.y);
169
170
      context.save();
171
      context.globalCompositeOperation = 'lighter';
172
      context.globalAlpha = this.alpha;      
173
      context.fillStyle = this.color;
174
      context.beginPath();
175
      context.moveTo(x, y);
176
      context.arc(x, y, this.radius, 0, Math.PI * 2, true);
177
      context.closePath();
178
      context.fill();
179
      context.restore();
180
    }
181
182
  };
183
  
184
  return {
185
    // Always kick things off with a really cool function name!
186
    lets_roll: initialize
187
  };
188
  
189
})();
190
191
starfield.lets_roll();
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................