0

My requirement is to return data URL. But, when I run the application, there is an run-time error:

JavaScript runtime error: Unspecified error.

Here is the code that I have used. Temp path is the path where is location of image is.

var canvas = document.createElement("canvas");   
var context = canvas.getContext("2d");         
var img = new Image();         
img.src = "@tempPath";                 
context.drawImage(img, 40, 40);         
var dataURL = canvas.toDataURL("image/jpeg");         
alert(dataURL);'
3
  • 4
    Is there really a single quote after alert(dataURL);? Commented May 6, 2015 at 5:16
  • Which IE version are you using? Is it HTML5 supported ? Commented May 6, 2015 at 5:49
  • no thats a mistake while copying the code. Commented May 6, 2015 at 6:06

1 Answer 1

2

Try following code, It worked for me:

<body>
    <canvas id="myCanvas"></canvas>
    <img id="profileImg" alt=""/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            drawImg();
        });
        function drawImg() {
            try {
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
                var img = new Image();
                img.src = $('#profileImg');
                context.drawImage(img, 40, 40);
                var dataURL = canvas.toDataURL("image/jpeg");
                alert(dataURL);
            } catch (e) {
                if (e.name == "NS_ERROR_NOT_AVAILABLE") {
                    // This is a bug in Firefox. The easiest fix is to simply keep trying until the error goes away, 
                    //since no event fires at the correct time.
                    // Wait before trying again; you can change the length of this delay.
                    setTimeout(drawImg, 100);
                } else {
                    throw e;
                }
            }
        }
    </script>
</body> 

Works well with IE as well. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.