0

These codes are prepared for save an image from a canvas. It only runs fun in IE/Chrome. In firefox I get only a blank image,grey or black when the canvas is loaded WebGL. Firefox can save images of a canvas properly ,while only being abnormal when a canvas loaded WebGL. How to solve this problem?

Actually I want the image to be sent back to the server.If firefox users get wrong images, whole construction will be destoryed.

<html>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<head>
    <script>
        window.onload = function() {
            draw();
            var saveButton = document.getElementById("saveImageBtn");
            bindButtonEvent(saveButton, "click", saveImageInfo);
            var dlButton = document.getElementById("downloadImageBtn");
            bindButtonEvent(dlButton, "click", saveAsLocalImage);
        };
        function draw(){
            var canvas = document.getElementById("thecanvas");
            var ctx = canvas.getContext("2d");
            ctx.fillStyle = "rgba(125, 46, 138, 0.5)";
            ctx.fillRect(25,25,100,100);
            ctx.fillStyle = "rgba( 0, 146, 38, 0.5)";
            ctx.fillRect(58, 74, 125, 100);
            ctx.fillStyle = "rgba( 0, 0, 0, 1)"; // black color
            ctx.fillText("Gloomyfish - Demo", 50, 50);
        }

        function bindButtonEvent(element, type, handler)
        {
            if(element.addEventListener) {
                element.addEventListener(type, handler, false);
            } else {
                element.attachEvent('on'+type, handler);
            }
        }

        function saveImageInfo ()
        {
            var mycanvas = document.getElementById("thecanvas");
            var image    = mycanvas.toDataURL("image/png");
            var w=window.open('about:blank','image from canvas');
            w.document.write("<img src='"+image+"' alt='from canvas'/>");
        }

        function saveAsLocalImage () {
            var myCanvas = document.getElementById("thecanvas");
            // here is the most important part because if you dont replace you will get a DOM 18 exception.
            // var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition: attachment;filename=foobar.png");
            var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
            window.location.href=image; // it will save locally
        }
    </script>
</head>
<body bgcolor="#E6E6FA">
<div>
    <canvas width=200 height=200 id="thecanvas"></canvas>
    <button id="saveImageBtn">Save Image</button>
    <button id="downloadImageBtn">Download Image</button>
</div>
</body>
</html>  
B.Law
  • 107
  • 1
  • 7
  • Do you initialize your webgl context with `preserveDrawingBuffer` set to true? – LJᛃ Nov 06 '15 at 14:02
  • 1
    Possible duplicate of [Canvas toDataURL() returns blank image only in Firefox](http://stackoverflow.com/questions/26783586/canvas-todataurl-returns-blank-image-only-in-firefox) – gman Nov 07 '15 at 01:03
  • Thx LJᛃ , the parameter you provided works really well ! – B.Law Nov 27 '15 at 06:28

0 Answers0