누군가 나에게 설명 할 수 있을까요?Javascript에서 완전히 생성되기 전에 변수를 사용하고있는 것처럼 보입니다. 그러나 이것이 작동합니다 - 이유는 무엇입니까?
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();
});
이 diagramImage
이 운동 생성자 돌아갈 때까지 존재하지 않는 날 것으로 보인다,하지만 난 수 있어요 (그리고해야 할 것 같다) 문맥의 strokeStyle
의 diagramImage
에 색상 지정 - diagramImage
가 생성되기 전에? 왜이게 효과가 있니?
편집 : 전체 코드 :
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);
}
포함 된 코드 부분 앞에'diagramImage'가 이미 정의되어 있습니까? – ziesemer
아니요.이 자습서를 참조하십시오. http://www.html5canvastutorials.com/labs/html5-canvas-interactive-flower/ 변수 'center'를 사용하여 동일한 작업을 수행합니다. –
게시 할 수있는 추가 코드가 있습니까? 당신의 주장? – shanabus