简体   繁体   English

HTML5 / Javascript-多个画布之间无法拖放的完美示例

[英]HTML5/Javascript - Perfect example of Drag and Drop between multiple canvases not working

I have have a problem I am hoping someone will be able to help with... 我有一个问题,我希望有人能够为您提供帮助。

On my application I require the facility to be able to drag and drop images between multiple canvases. 在我的应用程序上,我要求该工具能够在多个画布之间拖放图像。

There are a few pre-made examples of dragging and dropping between multiple canvases avaliable online, and I have found the perfect example for my needs courtesy of 'Richard Heyes' of RGraph which you can see here (NOTE: you must click the image before you can start dragging it). 在网上可以找到几个在多个画布之间拖放的预制示例,我发现了一个完美的示例,可以满足我的需求,您可以在此处看到RGraph“ Richard Heyes” (注意:您必须先单击图像您可以开始拖动它)。

As you can see, on his website this drag and drop feature works perfectly, however when I transfer the javascript, html and css to my application the ability to drag and drop the image does not work. 如您所见,在他的网站上,此拖放功能完美地起作用,但是当我将javascript,html和css传输到我的应用程序时,拖放图像的功能不起作用。

What I am doing: 我在做什么:

<!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 -->

I have created the same thing on this JSFiddle but the dragging and dropping still does not work. 我已经在此JSFiddle上创建了相同的东西,但是拖放仍然无法进行。

I am new to html5 and javascript so I am sure it must be something very simple I am overlooking, but I cannot work out what it is. 我是html5和javascript的新手,所以我确定它一定是我所忽略的非常简单的东西,但是我无法弄清它是什么。

Your help with this would be much appreciated, thank you very much. 非常感谢您的帮助,非常感谢。

I have inserted your JavaScript code between tags <script> and </script> and move it from JavaScript to HTML and I have added new script link from the example page: 我已将您的JavaScript代码插入标签<script></script>并将其从JavaScript移至HTML,并从示例页面添加了新的脚本链接:

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

JSFiddle - working example JSFiddle-工作示例

So I think, that you must download and insert core files of RGraph to your code from this page . 因此,我认为您必须从此页面下载RGraph的核心文件并将其插入到您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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