Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding color property to Sprite (again) #78

Merged
merged 1 commit into from
May 7, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var jaws = (function(jaws) {
* @property {bool} flipped Flip sprite horizontally, usefull for sidescrollers
* @property {string} anchor String stating how to anchor the sprite to canvas, @see Sprite#anchor ("top_left", "center" etc)
* @property {int} scale_image Scale the sprite by this factor
* @property {string,gradient} color If set, draws a rectangle of dimensions rect() with specified color or gradient (linear or radial)
*
* @example
* // create new sprite at top left of the screen, will use jaws.assets.get("foo.png")
Expand Down Expand Up @@ -58,6 +59,8 @@ jaws.Sprite.prototype.default_options = {
scale_x: 1,
scale_y: 1,
scale: 1,
width: null,
height: null,
_constructor: null,
dom: null
}
Expand All @@ -73,7 +76,15 @@ jaws.Sprite.prototype.set = function(options) {
if(this.image) this.setImage(this.image);
if(this.scale_image) this.scaleImage(this.scale_image);
if(this.anchor) this.setAnchor(this.anchor);


if (this.color && this.width && this.height) {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.fillStyle = this.color;
context.fillRect(0, 0, this.width, this.height);
this.image = context.getImageData(0, 0, this.width, this.height);
}

this.cacheOffsets()

return this
Expand Down