首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Craftyjs画布旋转

Craftyjs画布旋转
EN

Stack Overflow用户
提问于 2016-04-08 19:22:51
回答 1查看 104关注 0票数 1

如果我将e1属性y更改为1或其他正值,则此代码可以工作,但如果y为0或负值,则代码将失败。没有错误,但形状不显示。如果我绘制其他类型的形状,同样的问题也会发生。无论如何,像0、90和271这样的旋转值适用于y: 0。X值就不存在这样的问题。为什么会这样呢?这是一个与Crafty.js相关的bug吗?

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2016-04-12 03:04:20

在设置原点之前设置wh。在设置旋转之前设置旋转原点。

这应该清楚地记录在API文档中,我继续并打开了an issue for that on Crafty's issue tracker

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

下面的代码片段适用于我。代码按此顺序设置whorigin & rotation

代码语言:javascript
复制
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});
代码语言:javascript
复制
<script src="https://github.com/craftyjs/Crafty/releases/download/0.7.1/crafty-min.js"></script>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36498365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档