1

我已经构建了几个 Web 资源,以将签名功能添加到 Microsoft Dynamics CRM 2011 表单中,其中一个内置于 silverlight 作为文件上传器等,另一个内置于 javascript 和 HTML 中,用于在屏幕上进行签名并保存为图像但该代码仅适用于谷歌浏览器。

我已经阅读了无数的stackoverflow线程,但找不到任何答案。

无论如何我可以以某种方式

  • 将画布图像保存在 IE 中,当前解决方案已经在 chrome 中工作的方式
  • 打开一个新窗口,以便出现画布图像并且可以右键单击并保存

我已经尝试过上述两种方法,但没有运气。

它也必须在javascript中

    <html><head>
    <script type="text/javascript">
    var canvas, ctx, flag = false,
        prevX = 0,
        currX = 0,
        prevY = 0,
        currY = 0,
        dot_flag = false;

    var x = "black",
        y = 2;

    function init() {
        canvas = document.getElementById('can');
        ctx = canvas.getContext("2d");
        w = canvas.width;
        h = canvas.height;

        canvas.addEventListener("mousemove", function (e) {
            findxy('move', e)
        }, false);
        canvas.addEventListener("mousedown", function (e) {
            findxy('down', e)
        }, false);
        canvas.addEventListener("mouseup", function (e) {
            findxy('up', e)
        }, false);
        canvas.addEventListener("mouseout", function (e) {
            findxy('out', e)
        }, false);
    }

    function color(obj) {
        switch (obj.id) {
            case "black":
                x = "black";
                break;
            case "white":
                x = "white";
                break;
        }
        if (x == "white") y = 50;
        else y = 2;

    }

    function draw() {
        ctx.beginPath();
        ctx.moveTo(prevX, prevY);
        ctx.lineTo(currX, currY);
        ctx.strokeStyle = x;
        ctx.lineWidth = y;
        ctx.stroke();
        ctx.closePath();
    }

    function erase() {
        var m = confirm("Want to clear");
        if (m) {
            ctx.clearRect(0, 0, w, h);
            document.getElementById("canvasimg").style.display = "none";
        }
    }

    function findxy(res, e) {
        if (res == 'down') {
            prevX = currX;
            prevY = currY;
            currX = e.clientX - canvas.offsetLeft;
            currY = e.clientY - canvas.offsetTop;

            flag = true;
            dot_flag = true;
            if (dot_flag) {
                ctx.beginPath();
                ctx.fillStyle = x;
                ctx.fillRect(currX, currY, 2, 2);
                ctx.closePath();
                dot_flag = false;
            }
        }
        if (res == 'up' || res == "out") {
            flag = false;
        }
        if (res == 'move') {
            if (flag) {
                prevX = currX;
                prevY = currY;
                currX = e.clientX - canvas.offsetLeft;
                currY = e.clientY - canvas.offsetTop;
                draw();
            }
        }
        }

            function downloadCanvas(link, canvasId, filename) {
var test = document.getElementById(canvasId).toDataURL();
            link.href = document.getElementById(canvasId).toDataURL();
            link.download = filename;
window.open(test);
        }

        /** 
         * The event handler for the link's onclick event. We give THIS as a
         * parameter (=the link element), ID of the canvas and a filename.
        */
        document.getElementById('download').addEventListener('click', function() {
            downloadCanvas(this, 'can', 'signature.png');
        }, false);
</script>
<meta><meta><meta charset="utf-8"></head>
<body onload="init()">
    <canvas width="400" height="200" id="can" style="border: 2px solid currentColor; left: 10px; top: 10px; position: absolute; background-color: rgb(255, 255, 255);"></canvas>
    <div style="left: 450px; top: 10px; position: absolute;">Choose Color</div>
    <div id="black" style="background: black; left: 550px; top: 15px; width: 19px; height: 19px; position: absolute;" onclick="color(this)"></div>
    <div style="left: 450px; top: 40px; position: absolute;">Eraser</div>
    <div id="white" style="background: white; border: 2px solid currentColor; left: 550px; top: 45px; width: 15px; height: 15px; position: absolute;" onclick="erase()"></div>
    <img id="canvasimg" style="left: 52%; top: 10%; position: absolute;">
    <a class="button" id="download" style="left: 10px; top: 220px; position: absolute;" onclick="downloadCanvas(this, 'can', 'test.png')" href="#">Download</a>


</body></html>
4

1 回答 1

2

[最终答案/编辑]

打开一个新窗口,并将正文的innerHTML属性设置为:

<img src="[DATA URI GOES HERE]"/>

根据 MSDN,toDataURL() 方法适用于 IE >= 9。

canvasElement.toDataURL(imageMimeType,质量)

imageMimeType 可以是“image/png”、“image/jpg”、“image/webp”,我相信“image/gif”也是如此。

为了实现您想要做的事情,您可以使用画布数据 URI 的 href 设置锚点,甚至可以使用 window.open 在新选项卡中打开数据 URI。

将图像保存在同一选项卡中另一个 StackOverflow 问题

数据 URI 资源http ://en.wikipedia.org/wiki/Data_URI_scheme https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs

于 2014-12-29T12:22:12.333 回答