1

如果我将 e1 属性 y 更改为 1 或其他一些正值,则此代码有效,但如果 y 为 0 或负数,则会失败。没有错误,但形状没有出现。如果我绘制其他类型的形状,则会出现同样的问题。无论如何,0、90和271等旋转值与y:0一起工作。x值没有这样的问题。这是为什么?它是与 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>
4

1 回答 1

0

设置wh设置原点之前。在设置旋转之前设置旋转原点。
这应该清楚地记录在 API 文档中,我继续在 Crafty 的问题跟踪器上打开了一个问题

如果您在旋转后设置原点,事情就会以某种方式搞砸,并且该_draw_me函数永远不会被调用,因为 Crafty 认为三角形位于视口(相机)之外并且不需要绘制。(观察设置时会发生什么Crafty.viewport.y = 100- 出现三角形)

以下片段对我有用。代码按顺序设置wand 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>

于 2016-04-11T19:04:19.607 回答