javascript - 如何将拖放结果存储为图像

标签 javascript jquery html html2canvas

我想对拖放的结果进行截图,但我不知道该怎么做。

实际上,我找到了 2 个 javascript 并使用了 HTML5,例如 html2canvascanvas2image

我现在将它们组合在一起,但它仍然遇到了 canvas2image 的一些问题。

请有相同经历的帮我解决一下,非常感谢。

请帮帮我,我已经在这里存货好几天了。

拖放代码。

<script>
$(function() {
    $( "#draggable" ).draggable();
    $( "#draggable2" ).draggable();
    $( "#droppable" ).droppable({
        hoverClass: "ui-state-active",
        drop: function( event, ui ) {
            $( this )
                .addClass( "ui-state-highlight" )
                .find( "p" )
                    .html( "Dropped!" );
        }
    });

});
</script>

图片生成代码

<script>
window.onload = function() {
function convertCanvas(strType) {
    if (strType == "JPEG")
        var oImg = Canvas2Image.saveAsJPEG(oCanvas, true);

    if (!oImg) {
        alert("Sorry, this browser is not capable of saving " + strType + " files!");
        return false;
    }

    oImg.id = "canvasimage";

    oImg.style.border = oCanvas.style.border;
    oCanvas.parentNode.replaceChild(oImg, oCanvas);
}

function convertHtml(strType) {
    $('body').html2canvas();
    var queue = html2canvas.Parse();
    var canvas = html2canvas.Renderer(queue,{elements:{length:1}});
    var img = canvas.toDataURL();
    convertCanvas(strType);
    window.open(img);
    }

document.getElementById("html2canvasbtn").onclick = function() {
    convertHtml("JPEG");
    }
}
</script>

HTML代码

<body>
<h3>Picture:</h3>

<div id="draggable">
<img src='http://1.gravatar.com/avatar/1ea64135b09e00ab80fa7596fafbd340?   s=50&d=identicon&r=R'>
</div>

<div id="draggable2">
<img src='http://0.gravatar.com/avatar/2647a7d4b4a7052d66d524701432273b?s=50&d=identicon&r=G'>
</div>

<div id="dCanvas">
<canvas id="droppable" width="500" height="500" style="border: 2px solid gray"   class="ui-widget-header" />
</div>
<input type="button" id="bGenImage" value="Generate Image" />
<div id="dOutput"></div>

</body>

最佳答案

一个没有库的工作示例:

<html>
<head>
    <script>

        function allowDrop(e)
        {
            e.preventDefault();
        }

        function drag(e)
        {
            //store the position of the mouse relativly to the image position
            e.dataTransfer.setData("mouse_position_x",e.clientX - e.target.offsetLeft );
            e.dataTransfer.setData("mouse_position_y",e.clientY - e.target.offsetTop  );

            e.dataTransfer.setData("image_id",e.target.id);
        }

        function drop(e)
        {
            e.preventDefault();
            var image = document.getElementById( e.dataTransfer.getData("image_id") );

            var mouse_position_x = e.dataTransfer.getData("mouse_position_x");
            var mouse_position_y = e.dataTransfer.getData("mouse_position_y");

            var canvas = document.getElementById('canvas');
            var ctx = canvas.getContext('2d');

            // the image is drawn on the canvas at the position of the mouse when we lifted the mouse button
            ctx.drawImage( image , e.clientX - canvas.offsetLeft - mouse_position_x , e.clientY - canvas.offsetTop - mouse_position_y );
        }

        function convertCanvasToImage() {
            var canvas = document.getElementById('canvas');

            var image_src = canvas.toDataURL("image/png");
            window.open(image_src);

        }

    </script>
</head>
<body>
    <div style="float:left" >
        <div>
            <img id="img1" draggable="true" ondragstart="drag(event)" src='img1.png'>
            <img id="img2" draggable="true" ondragstart="drag(event)" src='img2.png'>
            <input type="button" onclick="convertCanvasToImage()" value="Generate Image" style="float:right"/>
        </div>
        <canvas id="canvas"  ondrop="drop(event)" ondragover="allowDrop(event)" width="500" height="500" style="border: 1px solid gray"  />

    </div>

</body>

图像必须具有相同的来源才能使用 toDataURL。 生成的图像在新窗口中打开,就像在您的代码中一样,但您也可以将其附加到页面、保存到磁盘或上传到服务器。

http://fiddle.jshell.net/gael/GF96n/4/

关于javascript - 如何将拖放结果存储为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13062885/

相关文章:

javascript - 使用按钮通过 JavaScript 切换文本框的状态

html - 如果提交的表单包含验证错误,我是否应该返回 400 错误?

html - 两个 div 中的段落对齐,它们之间有一个图像

jquery - 如何确定项目现在是否可见/在窗口中/在折叠下方

javascript - 为什么不能使用 JavaScript 改变 CSS 动画的速度

javascript - 你必须传递一个有效的 ReactElement |甜心 react

javascript - 查找字符串中的第一个字母 - Javascript

javascript - Safari 浏览器使用 javascript 中的类报告错误

javascript - 如何在不重新填充列表的情况下维护下拉列表的当前信息?

javascript - 无法从元素中的 data-n 获取数据