Could someone explain this to me?
var diagramImage = new Kinetic.Shape(function () {
var context = this.getContext();
context.beginPath();
context.lineWidth = 1;
//This is crazy tricks. It's part of the KineticJS demo website, but how am I able to assign diagramImage.color here?
context.strokeStyle = diagramImage.color;
var lastVertice = polygon.Vertices[polygon.Vertices.length - 1];
context.moveTo(lastVertice.X, lastVertice.Y);
for (var i = 0; i < polygon.Vertices.length; i++) {
var vertice = polygon.Vertices[i];
context.lineTo(vertice.X, vertice.Y);
}
context.stroke();
context.closePath();
});
It seems to me that diagramImage
does not exist until the Kinetic constructor returns, but I am able (and seem to need to) assign context's strokeStyle
to diagramImage
's color -- before diagramImage
has been created? Why does this work?
EDIT: Full code:
function DrawPolygon(diagramLayer, polygon) {
var diagramImage = new Kinetic.Shape(function () {
var context = this.getContext();
context.beginPath();
context.lineWidth = 2;
//This is crazy tricks. It's part of the KineticJS demo website, but how am I able to assign diagramImage.color here?
context.strokeStyle = diagramImage.color;
var lastVertice = polygon.Vertices[polygon.Vertices.length - 1];
context.moveTo(lastVertice.X, lastVertice.Y);
for (var i = 0; i < polygon.Vertices.length; i++) {
var vertice = polygon.Vertices[i];
context.lineTo(vertice.X, vertice.Y);
}
context.stroke();
context.closePath();
});
diagramImage.color = "red";
diagramImage.on("mouseover", function () {
this.color = "green";
diagramLayer.draw();
});
diagramImage.on("mouseout", function () {
this.color = "red";
diagramLayer.draw();
});
diagramLayer.add(diagramImage);
planViewStage.add(diagramLayer);
}