使用正确注入Createjs

时间:2014-12-05 08:02:38

标签: javascript createjs

所以我开始使用createjs,在绘制了一些立方体之后,我想改变立方体的颜色,这让我感到惊讶,因为这并不像我那么容易,但它与raphaeljs相比只是

object.attr ("stroke", "#0000FF");

或类似的东西,现在在createjs中有两种方法可以改变对象的颜色,第一种方法是再次完全重绘每一个属性的形状设置,这在我看来很可怕,因为我需要保存x, y,宽度,高度以及可能具有不同形状的其他东西到某些数据结构。另一种方法是使用一种叫做注入的东西,从我看到过的例子就是这样(link

var shape = new createjs.Shape().set({x:50,y:50});
var g = shape.graphics;

var fillCommand = g.beginFill("#ff0000").command;
// Note that this is equivalent to:
g.beginFill("#ff0000");
var fillCommand = g.command; // The last command

var shapeCommand = g.drawRect(0,0,100,100).command;

// Then later:
fillCommand.style = "#0000ff";
shapeCommand.w = 200;

为什么它确实解决了只需要改变我想改变的东西的问题也很复杂,我找不到任何关于实际变量的文档( .style 。w 在这种情况下)。所以任何人都有使用这种注射方式的经验?

1 个答案:

答案 0 :(得分:0)

查看文档中的Graphics方法(例如Graphics.Fill http://www.createjs.com/Docs/EaselJS/classes/Graphics.Fill.html)。我们肯定希望扩展文档,但它至少应该让您了解API期望的内容。

我不认为它太复杂,只要记住它是一般绘图API,并且没有具有单个笔划或填充的具体形状对象的概念。这使它更加强大,你可以做的不仅仅是定义彩色多边形。

您所包含的示例已被过度说明。要使用自定义笔划创建简单矩形,您可以执行以下操作:

var stroke = shape.graphics.beginStroke("#000").command; // Save this to modify it later
shape.graphics.beginFill("#f00").drawRect(0,0,100,100);
stage.addChild(shape);

//Then
stroke.style = "#00f";

这是一个简单的例子: http://jsfiddle.net/lannymcnie/L2tm9xdm/

或者你可以做一些更复杂的事情: http://jsfiddle.net/lannymcnie/L2tm9xdm/1/