UNPKG

cdf

Version:

A library for creating oldschool demo-like animations with JavaScript

169 lines (157 loc) 5.19 kB
// TODO: this must die. var Canvas = require('./_Canvas'); var Sprite = function (imageURL) { var instance = this; this.img = new Image(); this.img.src = imageURL; this.handlex = 0; this.handley = 0; this.midhandled = false; this.tilew = 0; this.tileh = 0; this.tilestart = 0; this.loaders = []; this.loaded = false; this.img.addEventListener('load', function(e){ var w = instance.width = instance.w = this.width; var h = instance.height = instance.h = this.height; instance.cx = w / 2; instance.cy = h / 2; instance.loaded = true; instance.trigger('load', [e], instance); }); return this; }; Sprite.prototype = { initTile: function (tilew, tileh, tilestart) { this.tileh = tileh; this.tilew = tilew; if (typeof (tilestart) !== 'undefined') this.tilestart = tilestart; return this; }, draw: function (dst, x, y, alpha, rotInDeg, scaleX, scaleY) { var tmp = dst.ctx.globalAlpha; if (typeof (alpha) === 'undefined') alpha = 1; dst.ctx.globalAlpha = alpha; if (arguments.length === 3 || arguments.length === 4) dst.ctx.drawImage(this.img, x - this.handlex, y - this.handley); else if (arguments.length === 5) { dst.ctx.translate(x, y); dst.ctx.rotate(rotInDeg * lib.utils.DEG_TO_RAD); dst.ctx.translate(-this.handlex, -this.handley); dst.ctx.drawImage(this.img, 0, 0); dst.ctx.setTransform(1, 0, 0, 1, 0, 0); } else { dst.ctx.translate(x, y); dst.ctx.rotate(rotInDeg * lib.utils.DEG_TO_RAD); dst.ctx.scale(scaleX, scaleY || scaleX); dst.ctx.translate(-this.handlex, -this.handley); dst.ctx.drawImage(this.img, 0, 0); dst.ctx.setTransform(1, 0, 0, 1, 0, 0); } dst.ctx.globalAlpha = tmp; return this; }, drawTile: function (dst, nb, x, y, alpha, rotInDeg, w, h) { var tmp = dst.ctx.globalAlpha; if (typeof (alpha) === 'undefined') alpha = 1; dst.ctx.globalAlpha = alpha; var xw = Math.floor((nb % (this.img.width / this.tilew))) * this.tilew; var yw = Math.floor(nb / (this.img.width / this.tilew)) * this.tileh; this.drawPart(dst, x, y, xw, yw, this.tilew, this.tileh, alpha, rotInDeg, w, h); dst.ctx.globalAlpha = tmp; return this; }, drawPart: function (dst, x, y, partx, party, partw, parth, alpha, rotInDeg, zx, zy) { if (partx < 0) { x -= partx / (this.midhandled === true ? 2 : 1); partw += partx; partx = 0; } else { if (!this.midhandled) { partw = Math.min(partw, this.img.width - partx); } } if (party < 0) { y -= party / (this.midhandled === true ? 2 : 1); parth += party; party = 0; } else { if (!this.midhandled) { parth = Math.min(parth, this.img.height - party); } } if (partw <= 0 || parth <= 0) { return; } var tmp = dst.ctx.globalAlpha; if (typeof alpha === 'undefined') alpha = 1; dst.ctx.globalAlpha = alpha; if (arguments.length === 7 || arguments.length === 8) { dst.ctx.translate(x, y); if (this.midhandled) dst.ctx.translate(-partw / 2, -parth / 2); else dst.ctx.translate(-this.handlex, -this.handley); dst.ctx.drawImage(this.img, partx, party, partw, parth, null, null, partw, parth); dst.ctx.setTransform(1, 0, 0, 1, 0, 0); } else if (arguments.length === 9) { dst.ctx.translate(x, y); dst.ctx.rotate(rotInDeg * lib.utils.DEG_TO_RAD); if (this.midhandled) dst.ctx.translate(-partw / 2, -parth / 2); else dst.ctx.translate(-this.handlex, -this.handley); dst.ctx.drawImage(this.img, partx, party, partw, parth, null, null, partw, parth); dst.ctx.setTransform(1, 0, 0, 1, 0, 0); } else { dst.ctx.translate(x, y); dst.ctx.rotate(rotInDeg * lib.utils.DEG_TO_RAD); dst.ctx.scale(zx, zy); if (this.midhandled) dst.ctx.translate(-partw / 2, -parth / 2); else dst.ctx.translate(-this.handlex, -this.handley); dst.ctx.drawImage(this.img, partx, party, partw, parth, null, null, partw, parth); dst.ctx.setTransform(1, 0, 0, 1, 0, 0); } dst.ctx.globalAlpha = tmp; return this; }, setMidHandle: function (round) { var hx, hy; if(!this.loaded){ return this.on('load',function(){ this.setMidHandle(); }) } if(this.cx){ hx = this.cx; hy = this.cy; } else { //TODO: Do we need this? hx = this.img.width/2; hy = this.img.height/2; } this.sethandle(hx, hy, round); this.midhandled = true; return this; }, setHandle: function (x, y, round) { if(round){ x = Math.round(x); x = Math.round(x); } this.handlex = x; this.handley = y; this.midhandled = false; return this; }, print: function (dst, str, x, y, alpha, rot, w, h) { w = w || 1; for (var i = 0; i < str.length; i++) { this.drawTile(dst, str[i].charCodeAt(0) - this.tilestart, x + i * this.tilew * w, y, alpha, rot, w, h); } return this; }, asCanvas: function(){ var canvas = new Canvas(this.w,this.h); this.draw(canvas,0,0); return canvas; } }; module.exports = Sprite;