【问题标题】:KineticJS Uncaught TypeError: Cannot read property 'x' of undefined after UpdateKineticJS Uncaught TypeError:更新后无法读取未定义的属性“x”
【发布时间】:2013-02-09 18:25:00
【问题描述】:

我正在使用本教程来构建可拖动的多边形。 Tutorial

现在我想在其中添加文本并尝试从 kinetic-v3.10.0.js 更新到更新的。 但是更新后我总是会收到上面提到的错误消息。

有人可以看看我的代码并帮助我找出我需要更改的地方,它可能会再次起作用吗?

以下是我的部分代码:

构建锚

function buildAnchor(layer, x, y, name) {
var anchor = new Kinetic.Circle({
    x: x,
    y: y,
    radius: 12,
    stroke: 'red',
    fill: '#ddd',
    strokeWidth: 4,
    draggable: true,
    name : name
});

// add hover styling
anchor.on('mouseover', function() {
    document.body.style.cursor = 'pointer';
    this.setStrokeWidth(6);
    layer.draw();
});
anchor.on('mouseout', function() {
    document.body.style.cursor = 'default';
    this.setStrokeWidth(4);
    layer.draw();
});

layer.add(anchor);
return anchor;}

构建线:

function buildLine(layer, x1, y1, x2, y2, name) {
var line = new Kinetic.Line({
    points: [x1, y1, x2, y2],
    stroke : '#000',
    strokeWidth : 2,
    name : name
});

var xDiff = x1 - x2;
var yDiff = y1 - y2;

var text = new Kinetic.Text({
    x: 0,
    y: 0,
    text: 'Test',
    fontSize: 20,
    fontFamily: 'Calibri',
    fill: 'green'
});


if (xDiff < 0 && yDiff == 0) {
    line.move(0,-30); //Point a -> b
}
else if (xDiff == 0 && yDiff < 0) {
    line.move(30,0); //Point b -> c
}
else if (xDiff > 0 && yDiff == 0) {
    line.move(0,30); //Point c -> d
}
else if (xDiff == 0 && yDiff > 0) {
    line.move(-30,0); //Point d -> a
}
else if (xDiff < 0 && yDiff < 0) {
    line.move(0,-30);       
}
else if (xDiff < 0 && yDiff > 0) {
    line.move(0,-30);
}
else if (xDiff > 0 && yDiff > 0) {
    line.move(-5,30);
}

layer.add(text);
layer.add(line);
return line;
return text;}

构建三角形:

function buildTriangle(layer, points, name) {
var triangle = new Kinetic.Polygon({
    stroke : '#666',
    strokeWidth : 8,
    name : name
});

triangle.scaleAnchor = function(anchor, factor) {
    anchor.attrs.x = ( anchor.attrs.x - this.center.x ) * factor + this.center.x;
    anchor.attrs.y = ( anchor.attrs.y - this.center.y ) * factor + this.center.y;
};
triangle.calculateCenter = function() {
    this.center.x = ( this.a.attrs.x + this.b.attrs.x + this.c.attrs.x ) / 3;
    this.center.y = ( this.a.attrs.y + this.b.attrs.y + this.c.attrs.y ) / 3;
};

triangle.a = buildAnchor(layer, points[0], points[1], 'anchor');
triangle.b = buildAnchor(layer, points[2], points[3], 'anchor');
triangle.c = buildAnchor(layer, points[4], points[5], 'anchor');
line1 = buildLine(layer, points[0], points[1], points[2], points[3], 'line');
line2 = buildLine(layer, points[2], points[3], points[4], points[5], 'line');
line3 = buildLine(layer, points[4], points[5], points[0], points[1], 'line');
triangle.was = { x : 0, y : 0 };
triangle.center = { x : 0, y : 0 };

layer.add(triangle);
return triangle;}

画三角形:

function drawtriangle() {
var triangle = this.get('.triangle')[0];

triangle.setPoints([    triangle.a.attrs.x - triangle.was.x, 
                        triangle.a.attrs.y - triangle.was.y,
                        triangle.b.attrs.x - triangle.was.x,
                        triangle.b.attrs.y - triangle.was.y,
                        triangle.c.attrs.x - triangle.was.x,
                        triangle.c.attrs.y - triangle.was.y ]);
line1.setPoints([triangle.a.attrs.x - triangle.was.x, triangle.a.attrs.y - triangle.was.y, triangle.b.attrs.x - triangle.was.x, triangle.b.attrs.y - triangle.was.y]);
line2.setPoints([triangle.b.attrs.x - triangle.was.x, triangle.b.attrs.y - triangle.was.y, triangle.c.attrs.x - triangle.was.x, triangle.c.attrs.y - triangle.was.y]);
line3.setPoints([triangle.c.attrs.x - triangle.was.x, triangle.c.attrs.y - triangle.was.y, triangle.a.attrs.x - triangle.was.x, triangle.a.attrs.y - triangle.was.y]);

triangle.calculateCenter.apply(triangle);}

【问题讨论】:

  • 这里是整个代码的一个jsfiddle:jsfiddle.net/GByKu/1/
  • 什么时候收到错误信息?我不明白。您在 jsFiddle 中使用了 kinetic-v3.10.0.js。这是重现您的问题的正确版本吗?
  • 当我尝试切换到较新版本(例如 kinetic-v4.3.2.js)以实现文本功能时,我收到错误消息,并且画布中不会显示任何内容。我的示例 jsfiddle 展示了它应该如何工作。
  • 这里是 jsfoddle 的 kinetic 4.3.1 版本。左上角的文本将正确显示,现在也可以通过设置“points:points”来显示多边形来构建函数,但是当抓取锚点时,多边形不会再改变了。 jsfiddle.net/GByKu/4/有什么想法吗?

标签: javascript html5-canvas kineticjs


【解决方案1】:

没有阅读和测试你的所有代码,但我发现了一个错误。

function buildLine(layer, x1, y1, x2, y2, name) {
  .... 
  ....
  return line;
  return text;
}

你只能在一个函数中返回一个。您的 buildLine 函数永远不会发短信,而只会发短信。并且所有与文本相关的操作都会失败。

【讨论】:

  • 感谢您的反馈。我会改变它,但它似乎可以工作,尽管它只能返回一个。请查看具有 kinetic 3.10.0 的两个版本,不工作文本,工作多边形拖动(jsfiddle.net/GByKu/1/)和动力学 4.3.1,工作文本,不工作多边形拖动(jsfiddle.net/GByKu/ 4/)。
猜你喜欢
  • 2021-04-28
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 2020-03-28
  • 2023-04-11
  • 2016-04-22
  • 2015-06-08
  • 2014-03-17
相关资源
最近更新 更多