1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var sprites = (function() {
6  var objs;
7  var width;
8  var height;
9  var SPRITE_SPEED = 2;
10
11  var sprites = {};
12  sprites.init = function(w, h) {
13    objs = [];
14    width = w;
15    height = h;
16  };
17  sprites.add = function(img) {
18    var obj = { img: img,
19                  x: Math.random() * (width - img.width),
20                  y: Math.random() * (height - img.height),
21                 dx: SPRITE_SPEED * (Math.random() < .5 ? -1 : 1),
22                 dy: SPRITE_SPEED * (Math.random() < .5 ? -1 : 1) };
23    objs.push(obj);
24  };
25  sprites.draw = function(context) {
26    for (var i = 0, len = objs.length; i < len; ++i) {
27      var obj = objs[i];
28
29      obj.x += obj.dx;
30      if ((obj.x > (width - obj.img.width)) || (obj.x < 0))
31        obj.dx *= -1;
32
33      obj.y += obj.dy;
34      if ((obj.y > (height - obj.img.height)) || (obj.y < 0))
35        obj.dy *= -1;
36
37      context.drawImage(obj.img, obj.x, obj.y);
38  };
39  };
40  return sprites;
41})();
42