如何从鼠标事件 evt.currentTarget.children[0] 返回形状


var stage, output;
function init() {
    stage = new createjs.Stage("testCanvas");
    // this lets our drag continue to track the mouse even when it leaves the canvas:
    // play with commenting this out to see the difference.
    stage.mouseMoveOutside = true; 

    var circle = new createjs.Shape();
    circle.graphics.beginFill("red").drawRoundRect(0, 0, 100,20,10);
    //console.log(circle.graphics.command.radius);
    var label = new createjs.Text("drag me", "bold 14px Arial", "#FFFFFF");
    label.textAlign = "center";
    label.y = -7;
    var dragger = new createjs.Container();
    dragger.x = 50;
    dragger.y = 10;
    dragger.addChild(circle, label);
    stage.addChild(dragger);
    dragger.on("pressmove",function(evt) {
        // currentTarget will be the container that the event listener was added to:
        //evt.currentTarget.x = evt.stageX;
        //evt.currentTarget.y = evt.stageY;
        // make sure to redraw the stage to show the change:
        //console.log(evt.currentTarget.children[0].graphics.command);
        var newWidth=  evt.stageX - evt.currentTarget.x;
        console.log(evt.currentTarget.children[0].graphics);
        if(newWidth<0)
            newWidth = 0; 
        evt.currentTarget.children[0].graphics.command.w= newWidth;
        evt.currentTarget.children[1].x= newWidth/2;
        stage.update();   
    });
    stage.update();
}

此代码在 http://www.createjs.com/demos 下工作正常

(我可以到达这个 evt.currentTarget.children[0].graphics.command.w,因为 evt.currentTarget.children[0] 返回形状)但不是在你自己的 HTML 上。我需要在标题中添加任何JS吗?

您是否检查过"按移动"是否触发?

也许您应该使用此stage.enableMouseOver(20);来启用鼠标事件。

最新更新