如果我将e1属性y更改为1或其他正值,则此代码可以工作,但如果y为0或负值,则代码将失败。没有错误,但形状不显示。如果我绘制其他类型的形状,同样的问题也会发生。无论如何,像0、90和271这样的旋转值适用于y: 0。X值就不存在这样的问题。为什么会这样呢?这是一个与Crafty.js相关的bug吗?
<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>发布于 2016-04-12 03:04:20
在设置原点之前设置w和h。在设置旋转之前设置旋转原点。
这应该清楚地记录在API文档中,我继续并打开了an issue for that on Crafty's issue tracker。
如果在旋转后设置原点,则会以某种方式搞乱,并且_draw_me函数永远不会被调用,因为Crafty认为三角形位于视口(摄影机)之外,不需要绘制。(观察设置Crafty.viewport.y = 100时发生的情况-出现三角形)
下面的代码片段适用于我。代码按此顺序设置w和h、origin & rotation。
Crafty.init();
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({w: 60, h: 60})
.origin("center")
.attr({x: 100, y: 0, rotation: 180});<script src="https://github.com/craftyjs/Crafty/releases/download/0.7.1/crafty-min.js"></script>
https://stackoverflow.com/questions/36498365
复制相似问题