编程语言
首页 > 编程语言> > javascript-如何在创建js中更改图形的填充颜色

javascript-如何在创建js中更改图形的填充颜色

作者:互联网

我正在使用它来更改创建js的填充颜色,这不起作用

var shape_rect = new createjs.Shape();
shape_rect.graphics.beginFill("#FFD64B").drawRect(61, 253, 398, 25);
shap_rect3.addEventListener('mouseover', function (evt) {
    shap_rect3.graphics.beginFill("#FFD64B").drawRect(61, 253, 398, 25);
    stage.update();
});

解决方法:

这里只有几件事:

首先,我注意到eventListener已链接到shap_rect3,但尚未定义(至少在您的示例中未定义).鼠标悬停的颜色也与声明的颜色相同,因此您不会看到任何更改.

假设shap_rect3是一个错字,这应该对您有用.

loadGame = function() {
    var canvas = document.getElementById('canvas');
    var stage = new createjs.Stage(canvas);

    stage.enableMouseOver(); //Enable MouseOver events

    var shape_rect = new createjs.Shape(); //Create your shape variable
    shape_rect.graphics.beginFill("#FFD64B").drawRect(61, 253, 398, 25); //Draw initial rectangle

    shape_rect.on('mouseover', function(evt) { //Change colour on mouseOver
        shape_rect.graphics.beginFill("#FF5400").drawRect(61, 253, 398, 25);
        stage.update(evt);
    });

    //Add this if you want to simulate a hover state and return the rectangle back to it's original colour
    shape_rect.on('mouseout', function(evt) { //.on gives the same result as .addEventListener (at least from a visual perspective, I'm not sure about resource usage)
        shape_rect.graphics.beginFill("#FFD64B").drawRect(61, 253, 398, 25);
        stage.update(evt);
    });

    stage.addChild(shape_rect);
    stage.update();
}

我是createJS和easylJS的新手,但这是实现此目的的一种方法.然后只需运行loadGame函数onLoad:

<body onl oad="loadGame()">
    <canvas id="canvas" width="640" height="640"></canvas>
</body>

标签:createjs,javascript
来源: https://codeday.me/bug/20191029/1961051.html