Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Utils = {}
- /**
- * get
- *
- * Requisição HTTP GET.
- *
- * @param url URL
- * @param callback Método de callback ao terminar o download
- * @param json Especifica se a resposta é um arquivo JSON
- */
- Utils.get = function(url, callback, json) {
- var xhr = new XMLHttpRequest();
- xhr.addEventListener('load', function(e) {
- if (json) { callback(JSON.parse(xhr.response)); }
- else { callback(xhr.response); }
- });
- xhr.open('GET', url);
- xhr.send(null);
- }
- var Cache = { stack: {} }
- Cache.loadImage = function(url, callback) {
- var image = document.createElement('image');
- image.src = url;
- image.onload = callback;
- this.stack[url] = image;
- }
- Cache.getImage = function(url) {
- if (url in this.stack) { return this.stack[url]; }
- else { this.loadImage(url, null); }
- }
- /**
- * Point
- */
- function Point(x, y) {
- this.x = x;
- this.y = y;
- }
- /**
- * Rectangle
- */
- function Rectangle(x, y, width, height) {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- }
- /**
- * Font
- *
- * Objeto que armazena dados da fonte usada.
- *
- * @param name Nome
- * @param size Tamanho
- */
- function Font(name, size) {
- this._name = name;
- this._size = size;
- }
- /**
- * getCanvasFont
- *
- * Obtém o objeto da fonte usado no canvas.
- *
- * @return string
- */
- Font.prototype.getCanvasFont = function() {
- return this._size + 'px ' + this._name;
- }
- /**
- * getSize
- *
- * Obtém o tamanho da fonte.
- *
- * @return int
- */
- Font.prototype.getSize = function() {
- return this._size;
- }
- /**
- * Bitmap
- *
- * Objeto para representação de imagens ou planos 2d para desenho.
- *
- * @param x Coordenada X
- * @param y Coordenada Y
- * @param z Coordenada Z
- * @param w Largura
- * @param h Altura
- */
- function Bitmap(x, y, z, w, h) {
- this.x = x;
- this.y = y;
- this.z = z;
- this.visible = true;
- this.font = new Font('Arial', 14);
- this._canvas = document.createElement('canvas');
- this._canvas.width = w;
- this._canvas.height = h;
- this._context = this._canvas.getContext('2d');
- Graphics.appendBitmap(this);
- }
- /**
- * getImage
- *
- * Obtém a imagem correspondente ao bitmap.
- *
- * @return image
- */
- Bitmap.prototype.getImage = function() {
- return this._canvas;
- }
- /**
- * clear
- *
- * Limpa o bitmap.
- */
- Bitmap.prototype.clear = function() {
- this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
- }
- /**
- * fillRect
- *
- * Preenche uma área com uma cor.
- *
- * @param x Coordenada X
- * @param y Coordenada Y
- * @param w Largura
- * @param h Altura
- * @param c Cor
- */
- Bitmap.prototype.fillRect = function(x, y, w, h, c) {
- this._context.fillStyle = c;
- this._context.fillRect(x, y, w, h);
- }
- /**
- * drawText
- *
- * Desenha um texto.
- *
- * @param x Coordenada X
- * @param Y Coordenada Y
- * @param text Texto
- * @param color Cor
- */
- Bitmap.prototype.drawText = function(x, y, text, color) {
- this._context.font = this.font.getCanvasFont();
- this._context.fillStyle = color;
- // Acresce ao y o tamanho da fonte como correção da posição.
- this._context.fillText(text, x, y + this.font.getSize());
- }
- /**
- *
- * drawImage
- *
- * Desenha uma imagem.
- *
- * @param image Imagem
- * @param sr Retângulo de corte
- * @param dr Retângulo de destino
- */
- Bitmap.prototype.drawImage = function(image, sr, dr) {
- if (dr == null) { this._context.drawImage(image, sr.x, sr.y, sr.width, sr.height); }
- else { this._context.drawImage(image, sr.x, sr.y, sr.width, sr.height, dr.x, dr.y, dr.width, dr.height); }
- }
- var Graphics = {
- canvas: null,
- bitmaps: [],
- context: null,
- fps: 0
- };
- Graphics.initialize = function() {
- this.canvas = document.getElementById('canvas');
- this.context = this.canvas.getContext('2d');
- }
- Graphics.sort = function() {
- var swapped;
- do {
- swapped = false;
- for (var i = 0; i < this.bitmaps.length - 1; i++) {
- if (this.bitmaps[i].z > this.bitmaps[i + 1].z) {
- var temp = this.bitmaps[i];
- this.bitmaps[i] = this.bitmaps[i + 1];
- this.bitmaps[i + 1] = temp;
- swapped = true;
- }
- }
- } while (swapped);
- }
- Graphics.appendBitmap = function(bitmap) {
- this.bitmaps.push(bitmap);
- this.sort();
- }
- Graphics.draw = function(fps) {
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
- this.fps = fps | 0;
- if (this.bitmaps == 'undefined') { return; }
- for (var i = 0; i < this.bitmaps.length; i++) {
- if (!this.bitmaps[i].visible)) { continue; }
- this.context.drawImage(this.bitmaps[i].getImage(), this.bitmaps[i].x, this.bitmaps[i].y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement