Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:56:55+00:00 2026-06-09T17:56:55+00:00

I am trying to create an object that will create an image and draw

  • 0

I am trying to create an object that will create an image and draw it on canvas. The reason I am not just the regular image object is that I want to know where the objects are place on the canvas for mouse actions.

Here is my constructor function:

function DisplayImg(src, x, y, w, h) {
            this.src = src;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
        }

I create a draw function to draw it to the canvas:

DisplayImg.prototype.draw = function(context) {
            img = new Image();
            img.src = this.src;
            img.onload = function() {
            context.drawImage(img,this.x,this.y,this.w,this.h); 
            }
        }

I then make an API call to instagram and grab some photo urls and put them in an array. Here I am trying to loop through that array and create an object for each one, and draw it to the canvas. I can console.log the data and see the image src’s so I know the data is there. This is the function I call once I get the data back:

function draw() {  
          for (var i = 0; i < srcArr.length; i++) { 
            var src = srcArr[i];
            x = Math.floor(Math.random()*8);
            y = Math.floor(Math.random()*8);
            var theImage = new DisplayImg(src,x,y,75,75); 
            theImage.draw(context);
          } 
        }

My logs return the correct data, but none of the images are drawn to the canvas. Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:56:56+00:00Added an answer on June 9, 2026 at 5:56 pm

    First thing you should do is fix your prototype function:

    DisplayImg.prototype.draw = function(context) {
        var display = this,
            img = new Image();
        img.onload = function() {
            context.drawImage(img,display.x,display.y,display.w,display.h); 
        }
        img.src = this.src;
    }
    

    You’re passing garbage values to the context.drawImage function because “this” no longer applies to your DisplayImage instance inside of that onload function, this should remedy that.

    Secondly, make sure you are actually passing a correct context through, not much I can help you there with. I made a fiddle that examples your code, but I had to change your “draw” function to make it sane: http://jsfiddle.net/yuaHF/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a method that will sum two timeO objects and return
I'm trying to create a custom object that behaves properly in set operations. I've
I am trying to create a date object that is 90 days from a
In an iPhone app for iOS5 I'm trying to create a controller object that
I'm trying to create a UnitTest to verify that an object has been deleted.
I'm trying to create a loop that creates a series of objects that contains
I'm trying to create an NSDictionary that stores objects with keys based on IDs.
I am basically trying to create a display object transformation manager which will allow
Here is the problem I want to use a constructor that will simplify image
I am trying to create a game, where the user will see an image,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.