【问题标题】:Resizing a rectangle in EaselJS在 EaselJS 中调整矩形的大小
【发布时间】:2013-03-16 07:15:16
【问题描述】:

我似乎完全迷失在 EaslJS 中。当我的函数被调用时,我不知道如何调整矩形的大小。

场景是这样的:我的玩家被击中,生命值减少,因此生命值条变小。

这是我创建健康栏的方法:

this.statusBar = new createjs.Shape()
this.statusBar.graphics
    .beginFill("red")
    .drawRect(0, 0, this.width, this.height);
this.statusBar.x = this.x+500;
this.statusBar.y = this.y;
this.statusBar.width = this.width;
this.statusBar.height = this.height;

这就是我要调整大小的地方:

this.decreaseHealth = function(amount) {
    percentageLeft = (player.health/player.fullPlayerHealth)*100;
    console.log(this.fullBarWidth*(percentageLeft/100));
    this.statusBar.width = this.fullBarWidth*(percentageLeft/100);
}

我的stage.update(); 在我的循环中,所以它正在按照您可能期望的方式进行更新。

【问题讨论】:

    标签: javascript html html5-canvas easeljs


    【解决方案1】:

    在easelJS 中,Shape 对象实际上是一个用于自定义绘图的控制器。

    如果要更改内容,必须重绘 Shape 的内容。

    您可以像这样在 statusBar 控制器中绘制健康矩形:

        // get a reference to the html canvas element
        var canvas=document.getElementById("canvas");
    
        // create a new Stage
        var stage=new createjs.Stage(canvas);
    
        // create a new Shape
        // the shape object is actually a controller for doing custom drawings
        var shape=new createjs.Shape();
    
        // add the shape controller to the stage
        stage.addChild(shape);
    
        // draw a rectangle using the shape controller
        shape.graphics.clear().beginFill("#F00").drawRect(30,20,150,25);
    
        // display all updated drawings
        stage.update();
    

    然后当你想改变一个球员的健康时,你会像这样重绘你的健康矩形:

        // redraw the health rectangle using the shape controller
        shape.graphics.clear().beginFill("#F00").drawRect(30,20,75,25);
    
        // display all updated drawings
        stage.update();
    

    【讨论】:

    • 代码是正确的,但您能否更改术语“容器” - 它可能会与 createjs.Container 混淆,Shape 不是继承自 :)
    • @olsn:可能是个好点 ;) 编辑为将 Shape Object 称为绘图控制器而不是容器。
    【解决方案2】:

    MarkE 的回答是正确的,但值得注意的是,虽然 EasleJS 不支持“宽度”或“高度”属性,但您可以设置 scaleX/scaleY。要调整 Shape 的大小,您可以将其绘制为 100%,然后在 0 和 1 之间缩放。

    干杯。

    【讨论】:

    • 通常我尽量不使用缩放作为首选,因为许多库都可能出现失真,但是 EaselJS 在缩放方面做得很好(即使是渐变!)我同意你也可以使用缩放: )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2023-03-09
    相关资源
    最近更新 更多