gpt4 book ai didi

canvas - EaselJS:单击鼠标更改形状填充颜色

转载 作者:行者123 更新时间:2023-12-04 02:09:50 26 4
gpt4 key购买 nike

我在使用 EaselJS 时遇到了困难。基本上我想创建一个简单的网格并突出显示所选的实际元素:

var stageWidth = 800,
stageHeight = 600,
cell_size = 50,
w = 16,
h = 12,
n = w * h,
canvas,
stage,
background;

$(document).ready(function(){

canvas = $("#container")[0];
stage = new createjs.Stage(canvas);

var x_pos = 0,
y_pos = 0,
res = 0,
grid_el;

for(i=0;i<n;i++){

res = i%w;

if(i>0){
if(res===0){
x_pos = 0;
y_pos++;
} else {
x_pos++;
}
}

grid_el = new createjs.Shape();
grid_el.graphics.setStrokeStyle(2,"square").beginStroke("#000000");
grid_el.graphics.beginFill("#70FF85");
grid_el.graphics.rect(x_pos * cell_size,y_pos * cell_size,cell_size,cell_size);
grid_el.x_pos = x_pos * cell_size;
grid_el.y_pos = y_pos * cell_size;
grid_el.name = "quad";

stage.addChild(grid_el);
}

stage.update();

stage.onMouseDown = function(e){
console.log(e);
var quad = this.getObjectsUnderPoint(e.stageX,e.stageY);

var clone = quad[0].graphics.clone();
clone.beginFill("#51D9FF");

quad[0].graphics = new createjs.Graphics(clone);

stage.update();
}

});

我尝试克隆实际元素的图形属性,更改填充颜色,然后更新舞台,但我只获得了一个白色元素,就像它无法识别新的图形属性一样。

感谢您的帮助。

最佳答案

我建议对您正在创建的形状或显示对象使用 onMouseOver/onMouseOut 回调。当你可以更新你的颜色时。

这是一个可能有助于说明我的观点的简单演示。

Link to demo

var stage = new createjs.Stage("test");
createjs.Touch.enable(stage);
stage.enableMouseOver(10);
var padding = 1;
var width = 55;
var height = 55;
var cols = 15;
for(var i=0;i<200;i++) {
var s = new createjs.Shape();
s.overColor = "#3281FF"
s.outColor = "#FF0000"
s.graphics.beginFill(s.outColor).drawRect(0, 0, width, height).endFill();
s.x = (width + padding) * (i%cols);
s.y = (height + padding) * (i/cols|0);
s.onMouseOver = handleMouseOver;
s.onMouseOut = handleMouseOut;
stage.addChild(s)
}

createjs.Ticker.addListener(stage);

function handleMouseOver(event) {
var target = event.target;
target.graphics.clear().beginFill(target.overColor).drawRect(0, 0, width, height).endFill();
}

function handleMouseOut(event) {
var target = event.target;
target.graphics.clear().beginFill(target.outColor).drawRect(0, 0, width, height).endFill();
}

function tick() {
stage.update();
}

希望这对您有所帮助。

关于canvas - EaselJS:单击鼠标更改形状填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com