【问题标题】:HTML5/Javascript - Perfect example of Drag and Drop between multiple canvases not workingHTML5/Javascript - 多个画布之间拖放的完美示例不起作用
【发布时间】:2013-11-04 08:38:44
【问题描述】:

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

在我的应用程序中,我要求该工具能够在多个画布之间拖放图像。

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

如您所见,在他的网站上,此拖放功能完美运行,但是当我将 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 的新手,所以我确信它一定是我忽略的非常简单的东西,但我无法弄清楚它是什么。

非常感谢您的帮助,非常感谢。

【问题讨论】:

    标签: javascript html drag-and-drop html5-canvas


    【解决方案1】:

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

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

    JSFiddle - working example

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

    【讨论】:

    • 非常好,非常感谢,您的示例运行良好。另外,您将如何在这些画布上添加多个图像?我已经研究出如何创建第三个和第四个画布来拖放,但不能创建多个图像。非常感谢您的帮助
    • 对不起,我不知道如何编辑该代码......但我认为,您必须创建一个图像数组,然后为每个图像编辑和重复 Draw() 函数,但我不知道不知道怎么写 :) 我的 PHP5 和 JS 功底不太好 :(
    猜你喜欢
    • 2011-07-29
    • 2013-10-05
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-11-12
    相关资源
    最近更新 更多