1

我有一个问题,我希望有人能够帮助解决...

在我的应用程序中,我需要能够在多个画布之间拖放图像的功能。

在线提供了一些在多个画布之间拖放的预制示例,我找到了满足我需要的完美示例,由RGraph 的“Richard Heyes”提供,您可以在此处看到(注意:您必须先单击图像你可以开始拖动它)。

如您所见,在他的网站上,此拖放功能完美运行,但是当我将 javascript、html 和 css 传输到我的应用程序时,拖放图像的功能不起作用。

我在做什么:

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

<style type="text/css">

canvas {
    border: 1px solid #808080;
}

</style>

<canvas style="float: left" height="125" width="400" id="cvs1">[No canvas support]</canvas>
<canvas style="float: left; margin-left: 100px" height="125" width="400" id="cvs2">[No canvas support]</canvas>

<script>
    window.onload = function ()
    {
        var canvas1 = document.getElementById("cvs1");
        var canvas2 = document.getElementById("cvs2");
        var context1 = canvas1.getContext('2d');
        var context2 = canvas2.getContext('2d');
        var imageXY  = {x: 5, y: 5};




        /**
        * This draws the image to the canvas
        */
        function Draw ()
        {
            //Clear both canvas first
            canvas1.width = canvas1.width
            canvas2.width = canvas2.width

            //Draw a red rectangle around the image
            if (state && state.dragging) {
                state.canvas.getContext('2d').strokeStyle = 'red';
                state.canvas.getContext('2d').strokeRect(imageXY.x - 2.5,
                                                         imageXY.y - 2.5,
                                                         state.image.width + 5,
                                                         state.image.height + 5);
            }

            // Now draw the image
            state.canvas.getContext('2d').drawImage(state.image, imageXY.x, imageXY.y);
        }




        canvas2.onclick =
        canvas1.onclick = function (e)
        {

            if (state && state.dragging) {
                state.dragging = false;
                Draw();
                return;
            }





            var mouseXY = RGraph.getMouseXY(e);

            state.canvas    = e.target;

            if (   mouseXY[0] > imageXY.x
                && mouseXY[0] < (imageXY.x + state.image.width)
                && mouseXY[1] > imageXY.y
                && mouseXY[1] < (imageXY.y + state.image.height)) {

                state.dragging       = true;
                state.originalMouseX = mouseXY[0];
                state.originalMouseY = mouseXY[1];
                state.offsetX         = mouseXY[0] - imageXY.x;
                state.offsetY         = mouseXY[1] - imageXY.y;

            }
        }

        canvas1.onmousemove =
        canvas2.onmousemove = function (e)
        {

            if (state.dragging) {

                state.canvas = e.target;

                var mouseXY = RGraph.getMouseXY(e);

                // Work how far the mouse has moved since the mousedon event was triggered
                var diffX = mouseXY[0] - state.originalMouseX;
                var diffY = mouseXY[1] - state.originalMouseY;

                imageXY.x = state.originalMouseX + diffX - state.offsetX;
                imageXY.y = state.originalMouseY + diffY - state.offsetY;

                Draw();

                e.stopPropagation();
            }
        }

        /**
        * Load the image on canvas1 initially and set the state up with some defaults
        */
        state = {}
        state.dragging     = false;
        state.canvas       = document.getElementById("cvs1");
        state.image        =  new Image();
        state.image.src    = 'http://www.rgraph.net/images/logo.png';
    state.offsetX      = 0;
        state.offsetY      = 0;

        state.image.onload = function ()
        {
            Draw();
        }
    }

</script>
</body>
</html>

<!-- CODE COURTESY OF RICHARD HEYES OF RGRAPH 
     http://www.rgraph.net/blog/2013/january/an-example-of-html5-canvas-drag-n-drop.html -->

我在这个JSFiddle上创建了同样的东西,但是拖放仍然不起作用。

我是 html5 和 javascript 的新手,所以我确信它一定是我忽略的非常简单的东西,但我无法弄清楚它是什么。

非常感谢您对此提供的帮助,非常感谢。

4

1 回答 1

2

我已在标签之间插入您的 JavaScript 代码<script></script>并将其从 JavaScript 移动到 HTML,并从示例页面添加了新的脚本链接:

<script src="http://www.rgraph.net/libraries/RGraph.common.core.js" ></script>

JSFiddle - 工作示例

所以我认为,您必须从此页面下载 RGraph 的核心文件并将其插入到您的代码中。

于 2013-10-25T10:01:05.030 回答