使用 jQuery 将对象发送到服务器

发布于 2024-10-31 16:39:36 字数 3785 浏览 1 评论 0原文

我设置了一个 jsfiddle 因为有相当多的代码,但我也会在这里 cp 它。

但首先是问题。我想将 Shape 对象发送到服务器以在 mouseup 上进行处理。我遇到的问题是,当我添加 jQuery ajax 或加载函数来测试 AJAX 时,我得到某种失控的代码,并且一切都冻结了。

首先是我添加的代码,以便您可以在 jsfiddle 中尝试,

        $('body').load('index.php/main/add_shape', shape, function () { shape.points["x"]});

现在是 js fiddle 上的代码为了能够在这里查看它并建立索引。

HTML

<body>
    <canvas id="drawn" height="200" width="200">
        <p>Your browser doesn't support HTML5 and the canvas element.  You should probably upgrade to one of the following<br>
            <a href="http://www.google.com/chrome">Google Chrome</a><br>
            <a href="http://www.mozilla.com/">Firefox</a><br>
            <a href="http://www.apple.com/safari/">Safari</a><br>
            <a href="http://www.opera.com/">Opera</a>
         </p>
    </canvas>
</body>

CSS

canvas {
    border: 1px solid #000;
}

JavaScript

var eventMap = { 
   mousemove: "move",
   touchmove: "move",
   mousedown: "down",
   touchstart: "down",
   mouseup: "up",
   touchend: "up"
};

function Shape (type, color, height, width, radius, x, y) {
    this.type = type;
    this.color = color;
    this.h = height;
    this.w = width; 
    this.r = radius;
    this.points = ["x","y"];
    this.points["x"] = [x];
    this.points["y"] = [y];
};

var tools = {}
$(window).ready(function () {
    function init () {
        hex = 000000;
        canvas = $('#drawn').get(0)
        c = canvas.getContext('2d');
        c.lineJoin = "round";
        c.lineCap = "round";
        c.strokeStyle = "#"+hex;
        c.lineWidth = 1;
        tool = new tools['pencil']();
        $('canvas').bind('mousedown mousemove mouseup', mouse_Draw);        
    }

        function mouse_Draw (e) {
        var pos = findPos(this);
        e._x = e.pageX - pos.x;
        e._y = e.pageY - pos.y;
        if (eventMap[e.type] == "down") {
            shapes = new Shape (1, 2, null, null, null, e._x, e._y);
        } else if (eventMap[e.type] == "move") {
            shapes.points["x"].push(e._x);
            shapes.points["y"].push(e._y);
        } else if (eventMap[e.type] == 'up') {
            shapes.points["x"].push(e._x);
            shapes.points["y"].push(e._y);
            alert(shapes.points["x"].toString());
        }

        var func = tool[eventMap[e.type]];
        if (func) {
            func(e);            
        }
    }

    init();
});

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
        return { x: curleft, y: curtop };
    }

}

/*****************TOOLS*************/
tools.pencil = function () {
    var tool = this;
    tool.started = false;

    tool.down = function (e) {
        c.beginPath();
        c.moveTo(e._x, e._y);
        tool.started = true;
        shape = new Shape (pencil, c.strokeStyle, null, null, null, e._x, e._y);
    };

    tool.move = function (e) {
        if (tool.started) {
            c.lineTo(e._x, e._y);
            c.stroke();
            shape.points["x"].push(e._x);
            shape.points["y"].push(e._y);
        }
    };

    tool.up = function (e) {
        if (tool.started) {
            tool.started = false;
        }
    };
}

正如您所看到的,当您将 jQuery 加载行添加到 tool.up 时,它会冻结。

I have a jsfiddle set up because there is a decent amount of code but I will also cp it here.

But first the problem. I would like to send the Shape object to the server for processing on mouseup. The problem I am having is when I add the jQuery ajax or load functions to test the AJAX I get some sort of run away code and everything freezes.

first the code I add so you can try it in the jsfiddle

        $('body').load('index.php/main/add_shape', shape, function () { shape.points["x"]});

Now for the code on the js fiddle for the sake of being able to look at it here and Indexing.

HTML

<body>
    <canvas id="drawn" height="200" width="200">
        <p>Your browser doesn't support HTML5 and the canvas element.  You should probably upgrade to one of the following<br>
            <a href="http://www.google.com/chrome">Google Chrome</a><br>
            <a href="http://www.mozilla.com/">Firefox</a><br>
            <a href="http://www.apple.com/safari/">Safari</a><br>
            <a href="http://www.opera.com/">Opera</a>
         </p>
    </canvas>
</body>

CSS

canvas {
    border: 1px solid #000;
}

JavaScript

var eventMap = { 
   mousemove: "move",
   touchmove: "move",
   mousedown: "down",
   touchstart: "down",
   mouseup: "up",
   touchend: "up"
};

function Shape (type, color, height, width, radius, x, y) {
    this.type = type;
    this.color = color;
    this.h = height;
    this.w = width; 
    this.r = radius;
    this.points = ["x","y"];
    this.points["x"] = [x];
    this.points["y"] = [y];
};

var tools = {}
$(window).ready(function () {
    function init () {
        hex = 000000;
        canvas = $('#drawn').get(0)
        c = canvas.getContext('2d');
        c.lineJoin = "round";
        c.lineCap = "round";
        c.strokeStyle = "#"+hex;
        c.lineWidth = 1;
        tool = new tools['pencil']();
        $('canvas').bind('mousedown mousemove mouseup', mouse_Draw);        
    }

        function mouse_Draw (e) {
        var pos = findPos(this);
        e._x = e.pageX - pos.x;
        e._y = e.pageY - pos.y;
        if (eventMap[e.type] == "down") {
            shapes = new Shape (1, 2, null, null, null, e._x, e._y);
        } else if (eventMap[e.type] == "move") {
            shapes.points["x"].push(e._x);
            shapes.points["y"].push(e._y);
        } else if (eventMap[e.type] == 'up') {
            shapes.points["x"].push(e._x);
            shapes.points["y"].push(e._y);
            alert(shapes.points["x"].toString());
        }

        var func = tool[eventMap[e.type]];
        if (func) {
            func(e);            
        }
    }

    init();
});

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
        return { x: curleft, y: curtop };
    }

}

/*****************TOOLS*************/
tools.pencil = function () {
    var tool = this;
    tool.started = false;

    tool.down = function (e) {
        c.beginPath();
        c.moveTo(e._x, e._y);
        tool.started = true;
        shape = new Shape (pencil, c.strokeStyle, null, null, null, e._x, e._y);
    };

    tool.move = function (e) {
        if (tool.started) {
            c.lineTo(e._x, e._y);
            c.stroke();
            shape.points["x"].push(e._x);
            shape.points["y"].push(e._y);
        }
    };

    tool.up = function (e) {
        if (tool.started) {
            tool.started = false;
        }
    };
}

As you can see when you add the jQuery load line to the tool.up it freezes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-11-07 16:39:36

如果你有一个不错的新浏览器...

tool.up = function (e) {
    if (tool.started) {
        tool.started = false;
        alert(JSON.stringify(shape));
    }
};

(或者你可以从 json.org 窃取 json 代码)那么你只需要解析 json 服务器端

if you have a nice new browser ...

tool.up = function (e) {
    if (tool.started) {
        tool.started = false;
        alert(JSON.stringify(shape));
    }
};

(or you can steal json code from json.org) then you just have to parse the json serverside

赴月观长安 2024-11-07 16:39:36

答案是该对象是一个无限循环,因为这条线

    shape = new Shape (pencil, c.strokeStyle, null, null, null, e._x, e._y);

需要

    shape = new Shape ('pencil', c.strokeStyle, null, null, null, e._x, e._y);

The answer was the object was an infinite loop because of this line

    shape = new Shape (pencil, c.strokeStyle, null, null, null, e._x, e._y);

needed to be

    shape = new Shape ('pencil', c.strokeStyle, null, null, null, e._x, e._y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文