Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Utils = {}
  2.  
  3. /**
  4.  * get
  5.  *
  6.  * Requisição HTTP GET.
  7.  *
  8.  * @param   url         URL
  9.  * @param   callback    Método de callback ao terminar o download
  10.  * @param   json        Especifica se a resposta é um arquivo JSON
  11. */
  12. Utils.get = function(url, callback, json) {
  13.     var xhr = new XMLHttpRequest();
  14.     xhr.addEventListener('load', function(e) {
  15.         if (json) { callback(JSON.parse(xhr.response)); }
  16.         else { callback(xhr.response); }
  17.     });
  18.     xhr.open('GET', url);
  19.     xhr.send(null);
  20. }
  21.  
  22. var Cache = { stack: {} }
  23. Cache.loadImage = function(url, callback) {
  24.     var image = document.createElement('image');
  25.     image.src = url;
  26.     image.onload = callback;
  27.     this.stack[url] = image;
  28. }
  29.  
  30. Cache.getImage = function(url) {
  31.     if (url in this.stack) { return this.stack[url]; }
  32.     else { this.loadImage(url, null); }
  33. }
  34.  
  35. /**
  36.  * Point
  37. */
  38. function Point(x, y) {
  39.     this.x = x;
  40.     this.y = y;
  41. }
  42.  
  43. /**
  44.  * Rectangle
  45. */
  46. function Rectangle(x, y, width, height) {
  47.     this.x = x;
  48.     this.y = y;
  49.     this.width = width;
  50.     this.height = height;
  51. }
  52.  
  53. /**
  54.  * Font
  55.  *
  56.  * Objeto que armazena dados da fonte usada.
  57.  *
  58.  * @param   name    Nome
  59.  * @param   size    Tamanho
  60. */
  61. function Font(name, size) {
  62.     this._name = name;
  63.     this._size = size;
  64. }
  65.  
  66. /**
  67.  * getCanvasFont
  68.  *
  69.  * Obtém o objeto da fonte usado no canvas.
  70.  *
  71.  * @return  string
  72. */
  73. Font.prototype.getCanvasFont = function() {
  74.     return this._size + 'px ' + this._name;
  75. }
  76.  
  77. /**
  78.  * getSize
  79.  *
  80.  * Obtém o tamanho da fonte.
  81.  *
  82.  * @return  int
  83. */
  84. Font.prototype.getSize = function() {
  85.     return this._size;
  86. }
  87.  
  88.  
  89. /**
  90.  * Bitmap
  91.  *
  92.  * Objeto para representação de imagens ou planos 2d para desenho.
  93.  *
  94.  * @param   x   Coordenada X
  95.  * @param   y   Coordenada Y
  96.  * @param   z   Coordenada Z
  97.  * @param   w   Largura
  98.  * @param   h   Altura
  99. */
  100. function Bitmap(x, y, z, w, h) {
  101.     this.x = x;
  102.     this.y = y;
  103.     this.z = z;
  104.     this.visible = true;
  105.     this.font = new Font('Arial', 14);
  106.  
  107.     this._canvas = document.createElement('canvas');
  108.     this._canvas.width = w;
  109.     this._canvas.height = h;
  110.     this._context = this._canvas.getContext('2d');
  111.  
  112.     Graphics.appendBitmap(this);
  113. }
  114.  
  115. /**
  116.  * getImage
  117.  *
  118.  * Obtém a imagem correspondente ao bitmap.
  119.  *
  120.  * @return  image
  121. */
  122. Bitmap.prototype.getImage = function() {
  123.     return this._canvas;
  124. }
  125.  
  126. /**
  127.  * clear
  128.  *
  129.  * Limpa o bitmap.
  130. */
  131. Bitmap.prototype.clear = function() {
  132.     this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
  133. }
  134.  
  135. /**
  136.  * fillRect
  137.  *
  138.  * Preenche uma área com uma cor.
  139.  *
  140.  * @param   x   Coordenada X
  141.  * @param   y   Coordenada Y
  142.  * @param   w   Largura
  143.  * @param   h   Altura
  144.  * @param   c   Cor
  145. */
  146. Bitmap.prototype.fillRect = function(x, y, w, h, c) {
  147.     this._context.fillStyle = c;
  148.     this._context.fillRect(x, y, w, h);
  149. }
  150.  
  151. /**
  152.  * drawText
  153.  *
  154.  * Desenha um texto.
  155.  *
  156.  * @param   x       Coordenada X
  157.  * @param   Y       Coordenada Y
  158.  * @param   text    Texto
  159.  * @param   color   Cor
  160. */
  161. Bitmap.prototype.drawText = function(x, y, text, color) {
  162.     this._context.font = this.font.getCanvasFont();
  163.     this._context.fillStyle = color;
  164.     // Acresce ao y o tamanho da fonte como correção da posição.
  165.     this._context.fillText(text, x, y + this.font.getSize());
  166. }
  167.  
  168. /**
  169.  *
  170.  * drawImage
  171.  *
  172.  * Desenha uma imagem.
  173.  *
  174.  * @param   image   Imagem
  175.  * @param   sr      Retângulo de corte
  176.  * @param   dr      Retângulo de destino
  177.  */
  178. Bitmap.prototype.drawImage = function(image, sr, dr) {
  179.     if (dr == null) { this._context.drawImage(image, sr.x, sr.y, sr.width, sr.height); }
  180.     else { this._context.drawImage(image, sr.x, sr.y, sr.width, sr.height, dr.x, dr.y, dr.width, dr.height); }
  181. }
  182.  
  183. var Graphics = {
  184.     canvas: null,
  185.     bitmaps: [],
  186.     context: null,
  187.     fps: 0
  188. };
  189.  
  190. Graphics.initialize = function() {
  191.     this.canvas = document.getElementById('canvas');
  192.     this.context = this.canvas.getContext('2d');
  193. }
  194.  
  195. Graphics.sort = function() {
  196.     var swapped;
  197.     do {
  198.         swapped = false;
  199.         for (var i = 0; i < this.bitmaps.length - 1; i++) {
  200.             if (this.bitmaps[i].z > this.bitmaps[i + 1].z) {
  201.                 var temp = this.bitmaps[i];
  202.                 this.bitmaps[i] = this.bitmaps[i + 1];
  203.                 this.bitmaps[i + 1] = temp;
  204.                 swapped = true;
  205.             }
  206.         }
  207.     } while (swapped);
  208. }
  209.  
  210. Graphics.appendBitmap = function(bitmap) {
  211.     this.bitmaps.push(bitmap);
  212.     this.sort();
  213. }
  214.  
  215. Graphics.draw = function(fps) {
  216.     this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  217.     this.fps = fps | 0;
  218.     if (this.bitmaps == 'undefined') { return; }
  219.     for (var i = 0; i < this.bitmaps.length; i++) {
  220.         if (!this.bitmaps[i].visible)) { continue; }
  221.         this.context.drawImage(this.bitmaps[i].getImage(), this.bitmaps[i].x, this.bitmaps[i].y);
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement