0

I try to upload and draw an image into a canvas. I have the html code for button and canvas and js code for onchange event. My code does not work and I do not have any errors to figure out what is going on.

8
  • What are you expecting as a result, i tried it and the image is being loaded inside the canvas. Can you specify more details 'on what is not working' ? Commented Jan 3, 2018 at 10:29
  • I have the same code but in separated files for js and html (same folder). I link them with <script type=“text/javascript” src=“myFileName.js”></script>, and nothig happens with my image loader Commented Jan 3, 2018 at 10:55
  • Are you adding the script tag in the <head> ? Commented Jan 3, 2018 at 11:32
  • you must add it at the end of the body, otherwise it won't work. can you open the browser console and check if there is any error in the logs ? Commented Jan 3, 2018 at 11:35
  • Add this <script src="myFileName.js"></script> before the body closing tag </body> Commented Jan 3, 2018 at 14:25

1 Answer 1

1

The fix is by simply including your scripts at the end of the body before the closing tag </body>.

Your html code will appear like this:

<html>
<body>
    <div id="buttonsDiv" >
        <input type="file" accept="image/*" title="Upload Image" class="normal-button" id="uploadBtn"/>            
    </div>
    <canvas id= "myImgCanvas" title="Drop an image here to upload" ></canvas>

    <script src="myFileName.js"></script>
</body>
</html>

Note that you must pay attention when you should/ should not include your scripts in the <head> tag vs including it at the end of the </body> tag. You can check this post for the best practice.

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.