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 7521129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:11:02+00:00 2026-05-30T02:11:02+00:00

I am having some difficulty copying a canvas from my buffer canvas to the

  • 0

I am having some difficulty copying a canvas from my buffer canvas to the canvas on my page. Thus far I have built a Render object, a Level object, and I have my main game loop (currently just a launch function).

I am able to write to the buffer canvas in the Render object just fine (if I add a document.body.append() statement the canvas successfully appends to the document with the necessary content) but I cannot copy from the buffer canvas to my main canvas. See below for a snippet of my code:

function Renderer(bufferWidth, bufferHeight) {
    var c=document.createElement('canvas');
    var ctx=c.getContext('2d');
    c.width=bufferWidth;
    c.height=bufferHeight;

    this.getBufferContext = function() { return ctx; };
    this.getBufferElement = function() { return c; };

    this.drawToCanvas = function(canvasCtx) { 
        canvasCtx.drawImage(c,0,0);
    };
}


var c = document.getElementById('mycanvas');
var ctx = c.getContext('2d');

var render = new Renderer(c.width, c.height);   
var level1 = new Level('images/imagequality.png');

level1.Draw(render.getBufferContext());
render.drawToCanvas(ctx);

Note that Renderer is in a separate file and is loaded using the script tags in my HTML page.

As mentioned earlier, the drawToCanvas() function doesn’t appear to successfully copy data from one canvas to another. Appending my source canvas confirms that it contains the expected data.

edit: I have listed my level code below.

function Level(mapname) {
    var map=new Image();
    map.src=mapname;

    this.Draw = function(renderer) {
        map.onload = function() { renderer.drawImage(map,0,0); };
    };
}
  • 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-05-30T02:11:02+00:00Added an answer on May 30, 2026 at 2:11 am

    I have good news, and I have bad news.

    The good news is the code you show here works 100%
    here is the demo: http://jsbin.com/upatij/edit#javascript,html,live

    bad news: that means that something inside your Level code is broken as my stub Level code works perfectly in your framework… 🙁

    stub Level:

    function Level() {
      this.Draw = function(xxctx) {
        for (var i = 0; i < 30; i++) {
          xxctx.moveTo(10 + (i * 40 % 300), 10 + (parseInt(i / 6, 10) * 40));
          xxctx.lineTo(40 + (i * 40 % 300), 40 + (parseInt(i / 6, 10) * 40));
          xxctx.moveTo(40 + (i * 40 % 300), 10 + (parseInt(i / 6, 10) * 40));
          xxctx.lineTo(10 + (i * 40 % 300), 40 + (parseInt(i / 6, 10) * 40));
        }
        xxctx.stroke();
      };
    }
    

    good luck! -ck

    AFTER YOUR SEEING YOUR LEVEL CODE:

    The problem is one of synchronicity, your use of classes here are hiding the problem from you, via deceptive naming, as your Level.Draw, is not a draw function at all… let me unwrap it for you:

    var c = document.getElementById('mycanvas');
    var ctx = c.getContext('2d');
    
    // var render = new Renderer(c.width, c.height);
    var Renderer_c = document.createElement('canvas');
    var Renderer_ctx = Renderer_c.getContext('2d');
    document.body.appendChild(Renderer_c); //added to show
    Renderer_c.width = c.width;
    Renderer_c.height = c.height;
    
    // var level1 = new Level('images/imagequality.png');
    var map = new Image();
    document.body.appendChild(map); //add to show
    map.src = 'http://th06.deviantart.net/fs71/150/i/2011/255/9/5/omnom_upside_down_by_earnurm-d49pjnl.png';
    
    console.log('at ' + 1);
    // level1.Draw(render.getBufferContext());
    map.onload = function() { 
      console.log('at ' + 3);
      //this happens async:
      alert('drawing now!');
      Renderer_ctx.drawImage(map,0,0); 
    };
    
    console.log('at ' + 2);
    // render.drawToCanvas(ctx);
    ctx.drawImage(Renderer_c, 0, 0);
    

    If you run that code you will see that at the moment at which onload is called everything else has already executed, you’ll notice how the console will read:

    at 1 
    at 2
    at 3
    

    and as such, at the moment when alert('drawing now!'); is executed…

    // render.drawToCanvas(ctx);
    ctx.drawImage(Renderer_c, 0, 0);
    

    will have already run… Basically your Draw() function is actually an asynchronous “Load”. Unfortunately, you current conceptualization does not work. Your Draw() function needs to be an async one like this:

    function Level(mapname) {
        var map=new Image();
        document.body.appendChild(map); //add to show
        map.src=mapname;
    
        this.asyncDraw = function(renderer, onComplete) {
            map.onload = function() { 
              renderer.drawImage(map,0,0); 
              onComplete();
            };
        };
    }
    

    and the function should then be called like this in your example:

    level1.asyncDraw(render.getBufferContext(), function() {
      render.drawToCanvas(ctx);
    });
    

    I should probably go on to say that this type of asynchronisity makes HTML5 game programming a little tricky as you really have to throw up the “Loading…” spinner and refrain from going into your rendering loop until all “resources” have loaded. In all practicality you need the concept of “ready” eg. Load(fOnReady) AND Draw(ctx) instead of just asyncDraw(ctx, fOnReady)…

    the updated jsbin is here: http://jsbin.com/upatij/2/edit

    hope this helps -ck

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

Sidebar

Related Questions

im having some difficulty trying to pull out a specific value from a cookie.
I am having some difficulty using a UIWebView to render PDF files on the
I'm having some difficulty getting my cross domain policy to work. I have an
I have had a bit of a look around and am having some difficulty
I have implemented drag and drop in my application, but am having some difficulty
I'm having some difficulty with a CSS only menu in Ie7. So far the
I'm having some difficulty with this, but basically load a page into a QWebView
I'm having some difficulty embedding an image from the Properties.Resources to a MailMessage, currently
I have the following web config file. I am having some difficulty in retrieving
I am having some difficulty with importing data from a .csv file. I am

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.