简体   繁体   中英

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() moves the shape relative to the position it has before, so your call would be 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.

And another thought: You use forEach, that's for IE9+. Why don't you use the Kineticjs Collection.each() method instead?

EDIT: And you can move the whole layer too, no need to loop over found objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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