If I change the e1 attribute y to 1 or some other positive value this code works, but if the y is 0 or negative it fails. There are no errors but the shape does not appear. If I draw other kind of shapes, same kind of problem occurs. Anyway, rotation values such as 0, 90 and 271 work with y: 0. There is not such a problem with the x value. Why is that? Is it a bug related to Crafty.js?
<script>
Crafty.init(480,680, document.getElementById('test'));
Crafty.c("myComponent", {
init: function () {
this.requires("2D, Canvas");
this.bind("Draw", this._draw_me);
this.ready = true;
},
_draw_me: function (e) {
var ctx = e.ctx;
ctx.beginPath();
ctx.moveTo(e.pos._x, e.pos._y);
ctx.lineTo(e.pos._x + e.pos._w, e.pos._y);
ctx.lineTo(e.pos._x + e.pos._w/2, e.pos._y + e.pos._h);
ctx.lineTo(e.pos._x, e.pos._y);
ctx.fillStyle = "blue";
ctx.fill();
}
});
var e1 = Crafty.e("myComponent")
.attr({x: 100, y: 0, w: 60, h: 60, rotation:180})
.origin("center");
</script>
Set
wandhbefore setting origin. Set the rotation origin before setting the rotation.This should be clearly documented in the API documentation, I went ahead and opened an issue for that on Crafty's issue tracker.
If you set the origin after the rotation, things get messed up somehow, and the
_draw_mefunction never gets called, as Crafty thinks the triangle sits outside the viewport (camera) and does not need to be drawn. (Observe what happens when you setCrafty.viewport.y = 100- the triangle appears)The following snippet works for me. The code sets
wandh,origin&rotation, in that order.