尝试使用 JQuery 将 x 和 y 坐标保存在数组中时未定义

Getting undefined while trying to save x and y coordinates in an array using JQuery

本文关键字:存在 保存 坐标 数组 未定义 JQuery      更新时间:2023-09-26

我正在使用Raphaeljs库绘制线条,我想存储线条的起点和终点的坐标,但我无法获得正确的语法。

这是代码的一部分:

     $(document).ready(function () {
            var startX = new Array();
            var startY = new Array();
            var endX = new Array();
            var endY = new Array();

            $('input[type="checkbox"][name="check"]').change(function () {
                // proceed only when checked
                if (this.checked) {
                    drawLine();
                }
            });
        });

        function drawLine() {
            var linewidth = $("#width").val();
            var color = $("#background").val();
            function Line(startX, startY, endX, endY, raphael) {
                for(var i=0; i< 25; i++) {
                    var start = {
                        x: startX[i],
                        y: startY[i]
                    };
                    var end = {
                        x: endX[i],
                        y: endY[i]

                    };
                    console.log(startX[i],startY[i]);
                    console.log(endX[i],endY[i]);
                }

                var getPath = function () {
                    return "M" + start.x + " " + start.y + " L" + end.x + " " + end.y;
                };

                var redraw = function () {
                    node.attr("path", getPath());
                };
                node = raphael.path(getPath());

                node.attr("stroke-width", linewidth); //sets the width of the line
                node.attr("stroke", color);
                startx = (node.getBBox().x);
                starty = (node.getBBox().y);
                //console.log(startx , starty);
                //sets the color of the line
                return {
                    updateStart: function (x, y) {
                        start.x = x;
                        start.y = y;
                        redraw();
                        return this;
                    },
                    updateEnd: function (x, y) {
                        end.x = x;
                        end.y = y;
                        redraw();
                        return this;
                    }
                };
            }

            $(document).ready(function () {
                var pathLength = 0;
                var paper = Raphael("droppable", 1280, 470, 0, 0);
                $("#droppable").mousedown(
                        function (e) {
                            x = e.offsetX;
                            y = e.offsetY;
                            line = Line(x, y, x, y, paper);
                            $("#droppable").bind('mousemove', function (e) {
                                x = e.offsetX;
                                y = e.offsetY;

                                line.updateEnd(x, y);

                            });
                        });
                $("#droppable").mouseup(
                        function (e) {
                            $("#droppable").unbind('mousemove');
                        });
});
        }

在控制台日志中,我未定义,错误消息Uncaught TypeError: Cannot read property '0' of undefined.

您正在将integers传递到Line函数中,而它需要arrays。然后,您尝试在 startX[i] 处访问 Line 函数中预期数组的第一个元素。您没有使用在代码顶部声明的数组来实现任何内容。数组也是空的,因此即使您将它们传递到 Line 函数而不是整数中,代码也不会起作用,因为没有任何要引用的元素。