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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:33:38+00:00 2026-06-16T07:33:38+00:00

canvasArray contains the split image, when opened in browser the <canvas> elements are supposed

  • 0

canvasArray contains the split image, when opened in browser the <canvas> elements are supposed to be displayed in a table e.g 3×3
First time opening page an emtpy table is rendered on screen (issue), when clicked again it displayed the <canvas> elements correctly, presumably because they are cached
How come they are not displaying first time? I tried using ctx.drawImage(img,xoffset,yoffset); twice in the code, inside the onload event and outside
Thinking being if image doesnt draw correctly outside the loop, once onload event fires it will try again, but it does not ..

Image URL<input type="text" id="image" value="images/hippo.jpg"/><br>


var canvasArray = Project.split( $("#image").val() , rowCount * rowCount); 
 // eg Project.split("https://image.com/funny.jpg, 3, 3");

var Project = {
  split: function(imgsrc, tiles) {

    var img = new Image(),
        canvasArray = new Array(),
        imgWidth,
        imgHeight,
        r,g,b = 0;
    img.onload = function(){
        xoffset = 0,
        offset = 0;

        for (var i = 0; i < tiles; i++){

            //create canvas element and set attributes and get the canvas context
            canvasArray[i] = document.createElement('canvas');
            canvasArray[i].setAttribute('width', tileW);
            canvasArray[i].setAttribute('height', tileH);    
            canvasArray[i].setAttribute('id', 'canvas'+i);
            var ctx = canvasArray[i].getContext('2d');

            ctx.drawImage(img,xoffset,yoffset);

            //if i is a multiple of the total number of tiles to a row,
            //move down a column and reset the row_col
            if((i + 1) % row_col == 0){
                yoffset -= tileH;
                xoffset =0;
            }else{
                //otherwise move across the image
                xoffset -= tileW; 
            }
        }
    };
    img.src = imgsrc;   

    //get the number of tiles in a row or column (row == column )
    var row_col = Math.sqrt(tiles),
        tileH = img.height / row_col,
        tileW = img.width / row_col,
        canvasArray = [tiles];

    var xoffset = 0,
        yoffset = 0;

    for (var i = 0; i < tiles; i++){

        //create canvas element and set attributes and get the canvas context
        canvasArray[i] = document.createElement('canvas');
        canvasArray[i].setAttribute('width', tileW);
        canvasArray[i].setAttribute('height', tileH);
        canvasArray[i].setAttribute('id', 'canvas'+i);
        var ctx = canvasArray[i].getContext('2d');

        ctx.drawImage(img,xoffset,yoffset);

        //if i is a multiple of the total number of tiles to a row,
        //move down a column and reset the row_col
        if((i + 1) % row_col == 0){
            yoffset -= tileH;
            xoffset =0;
        }else{
            //otherwise move across the image
            xoffset -= tileW; 
        }
    }

    return canvasArray;
}};
  • 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-16T07:33:39+00:00Added an answer on June 16, 2026 at 7:33 am

    You need to check complete of img, select image itself instead of val, calculate image dimensions inside onload handler, also as image loading is asynchronous operation, you need use callback, instead of return array:

    // eg Project.split("https://image.com/funny.jpg, 3*3,function(canvasArray)");
    var Project = {
        split: function(imgsrc, tiles, callback) {
    
            var canvasArray = new Array(),
                imgWidth, imgHeight, r, g, b = 0;
            var split = function() {
                console.log(1);
                var row_col = Math.sqrt(tiles),
                    tileH = Math.round(img.height / row_col),
                    tileW = img.width / row_col,
    
                    xoffset = 0,
                    yoffset = 0;
                for (var i = 0; i < tiles; i++) {
                    //create canvas element and set attributes and get the canvas context
                    canvasArray[i] = document.createElement('canvas');
                    canvasArray[i].setAttribute('width', tileW);
                    canvasArray[i].setAttribute('height', tileH);
                    canvasArray[i].setAttribute('id', 'canvas' + i);
                    var ctx = canvasArray[i].getContext('2d');
    
                    ctx.drawImage(img, xoffset, yoffset);
    
                    //if i is a multiple of the total number of tiles to a row,
                    //move down a column and reset the row_col
                    if ((i + 1) % row_col == 0) {
                        yoffset -= tileH;
                        xoffset = 0;
                    } else {
                        //otherwise move across the image
                        xoffset -= tileW;
                    }
                }
                callback(canvasArray);
            };
            var img = new Image();
            img.src = imgsrc;
            console.log(img.complete);
                if (img.complete) {split()} else  $(img).load(split);
        }
    };
    var tn = 3;
    $("button").bind('click', function() {
        Project.split($("#image").val(), tn * tn, function(canvasArray) {
            for (var i in canvasArray) {
                $('body').append(canvasArray[i]);
                if ((i * 1 + 1) % tn == 0) {
                    $('body').append($('<br>'))
                }
            }
            console.log(canvasArray);
        });
    
    
    });
    
    $("button").click();​
    

    DEMO

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

Sidebar

Related Questions

I have a 3x3 HTML table with each element storing a separate <canvas> element
how can i change the size of an html canvas through a function? i
In WPF C#, in code behind, I have to dynamically create an Array of
I try to show text box (#dimVal), when div(#CanvasArea) is clicked. And I want

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.