简体   繁体   English

KineticJS更改对象的坐标

[英]KineticJS changing coordinates for objects

I can not understand why there is a differential displacement Result . 我不明白为什么会有差分位移结果 Polygons and text shifted uniformly, line relative error polygons shifted. 多边形和文本均匀移动,线相对误差多边形移动。

    function setNewPosition(x, y) {
                var lines = stage.find('Line');
                if (lines.length > 0) {
                    lines.forEach(function inLines(line) {
                        var points = line.getPoints();
                        if (points.length > 0) {
                            points.forEach(function pSet(p) {
                                p.x += x;
                                p.y += y;
                            });
                        }
                    });
                };
                polygons = stage.find('Polygon');
                if (polygons.length > 0) {
                    polygons.forEach(function inPoly(polygon) {
                        var points = polygon.getPoints();
                        if (points.length > 0) {
                            points.forEach(function pSet(p) {
                                p.x += x;
                                p.y += y;
                            });
                        }
                    });
                };
                texts = stage.find('Text');
                if (texts.length > 0)
                    texts.forEach(function inText(text) {
                        oX = text.getX();
                        oY = text.getY();
                        text.setX(oX + x);
                        text.setY(oY + y);
                    }
                );
               layer.draw();
            }

I think you shouldn't fiddle with the points. 我认为您不应该摆弄这些要点。 There is a move() method for every shape, why don't you use that? 每个形状都有一个move()方法,为什么不使用它呢?

move() moves the shape relative to the position it has before, so your call would be move(-80, 0). move()相对于其之前的位置移动形状,因此您的调用将为move(-80,0)。

Besides, why do you check for the find array length? 此外,为什么还要检查查找数组的长度? If you use forEach after it's useless because forEach would just do nothing when you didn't find anything. 如果在无用之后再使用forEach,因为forEach在您什么都找不到的情况下什么也不会做。

And another thought: You use forEach, that's for IE9+. 还有一个想法:您使用forEach,即IE9 +。 Why don't you use the Kineticjs Collection.each() method instead? 为什么不改用Kineticjs Collection.each()方法呢?

EDIT: And you can move the whole layer too, no need to loop over found objects. 编辑:并且您也可以移动整个图层,而无需遍历找到的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM