Skip to content

Commit edbb558

Browse files
committed
Merge pull request #507 from starwed/sprite-scale
Scale sprites on DOM entities
2 parents b094a7e + a1366d9 commit edbb558

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/DOM.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Crafty.c("DOM", {
9999
draw: function () {
100100
var style = this._element.style,
101101
coord = this.__coord || [0, 0, 0, 0],
102-
co = { x: coord[0], y: coord[1] },
102+
co = { x: coord[0], y: coord[1], w: coord[2], h: coord[3] },
103103
prefix = Crafty.support.prefix,
104104
trans = [];
105105

src/sprite.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ Crafty.c("Sprite", {
4747
pos._h //height on canvas
4848
);
4949
} else if (e.type === "DOM") {
50-
this._element.style.background = "url('" + this.__image + "') no-repeat -" + co.x + "px -" + co.y + "px";
50+
//Get scale (ratio of entity dimensions to sprite's dimensions)
51+
// If needed, we will scale up the entire sprite sheet, and then modify the position accordingly
52+
var vscale = this._h/co.h, hscale =this._w/co.w;
53+
54+
this._element.style.background = "url('" + this.__image + "') no-repeat -" + co.x*hscale + "px -" + co.y*vscale + "px";
55+
// style.backgroundSize must be set AFTER style.background!
56+
if (vscale != 1 || hscale != 1){
57+
this._element.style.backgroundSize = (this.img.width * hscale) + "px" + " " + (this.img.height * vscale) + "px";
58+
}
5159
}
5260
};
5361

0 commit comments

Comments
 (0)