Canvas not displaying image in JavaScript

I have a bird object with a draw function defined, but for some reason, the image is not showing up when called in the render function. Can someone please help me figure out what I'm doing wrong? Thanks in advance.

EDIT: Upon further investigation, I've discovered that the bird image doesn't always appear because there are other sprites on the screen. When I turn them off, the bird shows up without fail. Is there a solution to this issue?

bird = {
            draw:function() {
                var birdimg = new Image();
                birdimg.onload = function() {
                    ctx.drawImage(birdimg, -20, height - 200);
                }
                birdimg.src = "//i.sstatic.net/nsZLM.png"; //"assets/bird.png";
            }
        };

        function main() {
            canvas = document.createElement("canvas");
            //Set the width and height to the sizes of the browser
            width = window.innerWidth;
            height = window.innerHeight;

            //Frames the game if the width is larger than 500
            if(width >=500){
                width = 320;
                height = 480;
                canvas.style.border = "1px solid #000";
                evt = "mousedown";
            }

            //Sets the canvas sizes to the width and height variables
            canvas.width = width;
            canvas.height = height;
            ctx = canvas.getContext("2d");

            //Set the default state
            currentstate = states.Splash;

            document.body.appendChild(canvas);

            render();
        }

        function render(){
            //Adding bird
            bird.draw();

            //Loads sky.png
            bgimg = new Image();
            bgimg.onload = function() {

                ctx.drawImage(bgimg,-20,height-200);
                ctx.drawImage(bgimg,256,height-200);

            }

            bgimg.src = "assets/sky.png";

            //Loads land img
            landimg = new Image();
            landimg.onload = function() {

                ctx.drawImage(landimg,0,height-100);

            }

            landimg.src = "assets/land.png";

            //Creates rectangle that colors background. THIS IS THE BOTTOM LAYER. WRITE CODE ABOVE

            ctx.fillStyle="#4ec0ca";
            ctx.fillRect(0,0,canvas.width,canvas.height);

            ctx.stroke();
        }

Answer №1

Implementing the suggestions made by @K3N, I have put together a functional example. Many thanks to @K3N for the valuable advice.

After incorporating these modifications, your code will appear as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <script>
    // ensure variables are accessible outside of main()
    var canvas,
        ctx,
        width,
        height;

    var bird = {
        draw: function() {
            var birdimg = new Image();
            birdimg.src ="http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg";

            birdimg.onload = function() {
                ctx.drawImage(birdimg, -20, height - 200);
            };
        }
    };

     function main(){
       canvas = document.createElement("canvas");

       //Adjust dimensions to fit browser window
       width = window.innerWidth;
       height = window.innerHeight;

       //Set game frame if width is over 500
       if(width >=500){
         width = 320;
         height = 480;
         canvas.style.border = "1px solid #000";

         var evt = "mousedown";
       }

       canvas.width = width;
       canvas.height = height;
       ctx = canvas.getContext("2d");

       document.body.appendChild(canvas);

       render();
    }

    main();

    function render(){
      //Include bird in rendering
      bird.draw();
    }
  </script>
</body>
</html>

Live Demo

I trust this explanation proves beneficial, Nick Leoutsakos

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

My toggleclass function seems to be malfunctioning

I am encountering a strange issue with my jQuery script. It seems to work initially when I toggle between classes, but then requires an extra click every time I want to repeat the process. The classes switch as expected at first, but subsequent toggles req ...

Injecting a controller into an AngularJS module within an anonymous function

As a newcomer to the world of javascript and just beginning to work with angular.js, I have a question. I'm wondering if there is a method for injecting a controller into a module that is declared within an anonymous function. This is how my code cu ...

Why does TypeScript keep throwing the "No inputs were found in the config file" error at me?

Why am I receiving the No inputs were found in config file error from TypeScript? I have set up my tsconfig.json in VS Code, but the error occurs when I try to build it. The terminal displays: error TS18003: No inputs were found in config file '/Use ...

What is a way to retain the value of a variable after a request, while starting off with a different value on the initial load?

In my Flask application, users have the option to choose a specific time period with a start date and an end date. When the page initially loads, I want the start date to default to the first day of the current month and the end date to be the current day. ...

Utilizing a checkbox within a select dropdown component in Vue

Has anyone come across a checkbox dropdown feature in Vue? I have been searching online but couldn't find any examples or resources related to this. If someone has a working skeleton for it, please share! ...

Combining the elements within an array of integers

Can anyone provide insight on how to sum the contents of an integer array using a for loop? I seem to be stuck with my current logic. Below is the code I've been working on: <p id='para'></p> var someArray = [1,2,3,4,5]; funct ...

Using jQuery and PHP to send a dynamic form through AJAX

I'm currently working on a pet registration form where users can add new pets. When a user clicks the "add pet" button, I use jQuery to clone the pet section of the form and give each cloned section an id like #pet-2, #pet-3, and so on. Although my ...

Tips for designing a multi-level dropdown navbar

I am currently facing an issue with designing a navbar. I am aiming for Multi-Level Dropdowns, but whenever I click on More Services, it automatically closes the main dropdown menu. I have experimented with various approaches, but none of them seem to be ...

Invoke a Python function from JavaScript

As I ask this question, I acknowledge that it may have been asked many times before. If I missed the answers due to my ignorance, I apologize. I have a hosting plan that restricts me from installing Django, which provided a convenient way to set up a REST ...

How to read a text file in JavaScript line by line

Coding Challenge: <script src="../scripts/jquery-3.1.0.min.js"></script> <script> $(document).ready(function(){ $('#test').click(function(){ var txtFile = '../data/mail.txt'; var file ...

Performing AJAX in Rails4 to update boolean values remotely

Suppose I have a model named Post which includes a boolean attribute called active. How can I efficiently modify this attribute to either true or false directly from the list of posts in index.html.erb using link_to or button_to helper along with remote: ...

``Are you looking to create multiple canvases in a loop?

I've managed to get this command working exactly as I wanted: http://jsfiddle.net/m1erickson/64BHx/ However, I didn't anticipate how challenging it would be to turn the drawing into reality here: What I attempted to do is illustrated in this li ...

Guide on altering the background color of a table row depending on the data in its cells with the help of AngularJS

I am looking to dynamically change the background color of a row based on specific cell data. If the first four characters in a table cell match a certain value, I want the entire row to change its color to red. Currently, my code changes the row color ba ...

Show a table with rows that display an array from a JSON object using JavaScript

My current code includes JSON data that I need to display in table rows, but I'm struggling to understand how to do so effectively. The output I am currently seeing is all the rows from the array stacked in one row instead of five separate rows as in ...

D3 not distinguishing between bars with identical data even when a key function is implemented

When attempting to create a Bar chart with mouseover and mouseout events on the bars using scaleBand(), I encountered an issue. After reviewing the solution here, which explains how ordinal scale treats repeated values as the same, I added a key to the dat ...

Using Primeng to implement pagination and lazy loading in a dataView

Looking for a way to search through product data with filters and display it using primeng dataview? Consider implementing pagination in your API that pulls products from the database page by page. Set the products array as the value in dataview so that wh ...

Modifying the input's value dynamically alters the selection choices in Vuetify

Choose the First option Fpo, when you select the first item, the first object in the list is assigned to the variable test. I have used test.name as a model for that input field. Strangely, when I try to modify the input field, the select option also chang ...

State loss occurs when moving to a different page using next/link

Currently, I am working on a library application with Next.js. Within this project, there are two main pages: BooksPage, which displays a list of all books, and BookPage, where detailed information about a specific book is shown. The components involved in ...

The useRoutes function is returning null despite the correct pathname being provided

Check out my React code snippet! I've got the Component nestled within a micro-frontend application, which is then brought into the main app using module federation. // embedded in my microfrontend. const path = '/another-component-path'; f ...

Encountering a ReferenceError stating that the window object is not defined while building a React Leaflet application

In my upcoming nextjs project, I've incorporated react-leaflet. It behaves flawlessly in dev mode. However, upon attempting to build the project, it throws a ReferenceError: window is not defined. Despite setting ssr to false in dynamic, the error per ...