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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:07:45+00:00 2026-06-06T00:07:45+00:00

i have a JavaScript project (using jquery), inside it i draw a canvas by

  • 0

i have a JavaScript project (using jquery), inside it i draw a canvas by code and then i insert array of images on it. it represents fine without any error.

problem: later on, i try to get these images inside from canvas to compare if they have the same src or the same id to don't replace it with the new image

my functions are:

var inter;
var screen;

function onClick(id){
    getScreen(id, function(data){
        screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);
        if (screen.document.getElementById("screen") != null ){    
            screen.document.getElementById("screen").innerHTML = "";
        }
        screen.document.write('<div id="screen"><canvas id="screenCanvas" width=' +
                              data.maxX + ' height=' + data.maxY + 
                              ' style="border:2px solid #000000;color:#000000;" ></canvas></div>');
        draw(data);
        inter = setInterval(function(){screen(id); }, 5000);
    });
}

function screen(id){
    getScreen(id, function(data){
        if (screen.closed == true){
            clearInterval(inter);
            return;
        }
        if((screen.document.getElementById('screenCanvas').width != data.maxX) ||
            (data.maxY != screen.document.getElementById('screenCanvas').height)){
            screen.close();
            screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);        
        screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);
        if (screen.document.getElementById("screen") != null ){    
            screen.document.getElementById("screen").innerHTML = "";
        }
        screen.document.write('<div id="screen"><canvas id="screenCanvas" width=' +
                              data.maxX + ' height=' + data.maxY + 
                              ' style="border:2px solid #000000;color:#000000;" ></canvas></div>');
        draw(data);
    });
}

function draw(data) {  
    var canvas = screen.document.getElementById('screenCanvas');
    var context = canvas.getContext('2d');  
    var tileY = 0;
    var tileX = 0;
    var counter = 0;
    var tileWidth = data.tileWidth;
    var tileHeight = data.tileHeight;
    for (var i=0;i<(data.maxX/data.tileWidth);i++){  
        for (var j=0;j<(data.maxY/data.tileHeight);j++){  
            var img = new Image();  
            img.onload = (function(img, tileX, tileY, tileWidth, tileHeight){
                return function() {
                    context.drawImage(img,tileX, tileY, tileWidth, tileHeight);
                }
            })(img, tileX, tileY, tileWidth, tileHeight);  
            img.src = "http://myserver:8080/images/screen/tile/" + 
                       data.tiles[counter].imageId; 
            tileX = tileX + parseInt(data.tileWidth); 
            counter ++; 
         } 
        tileY = tileY + parseInt(data.tileHeight); 
        tileX = 0;
    }  
}

</script>

this code gets an array (data) that contains list of (id, maxX, maxY, tileWidth, tileHeight, tiles[x, y, imageId]) and open new window, then write in this window the canvas code and draw images then calls a timer to work each 5 seconds. after each 5 seconds, screen method calls to re-get the data again and check if screen open then it remove all inner html to rewrite new canvas, and it tests if sizes changes.

this code works fine without any error, but i need to edit these code to get images inside canvas and test it they has the same imageId, don’t download it again.(i don’t save image id, to get images by, we can store it in img.id, or get images by src since, imageId is a part of image path that makes it unique).

Note:

id[unique](is the id of the user), 
maxX(maximum width of the screen), 
maxY(maximum height of the screen), 
tileWidth(width of each image), 
tileHeight(height of each image),
tiles(list of images)
    x(left position of the image)
    y(top postion of the image) 
    imageId[unique](id of each image)
example: data[id:1,maxX:1920,maxY:1080,tileWidth:480,tileHeight:270,tiles:(x:2,y:1,imageId:1a)]

any help?

  • 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-06T00:07:46+00:00Added an answer on June 6, 2026 at 12:07 am

    as Ravi Jain said you can’t get the image drawn to canvas object, and as robertc said you can use img element.

    try these functions that specify the usage of img element not canvas:

    function onClick(id){
        getScreen(id, function(data){
            var inter;
            var screen;
            var width = parseInt(data.maxX) + parseInt(data.tileWidth);
            var height = parseInt(data.maxY) + parseInt(data.tileHeight);
            screen=window.open('',id,'width=' + width + ',height=' + height , true);
            draw(screen, data);
            inter = setInterval(function(){screen(screen, inter, id); }, 5000);
        });
    }
    
    function screen(screen, inter, id){
        getScreen(id, function(data){
            if (screen.closed == true){
                clearInterval(inter);
                return;
            }
            if((screen.document.getElementById("screen").style.width != data.maxX + "px") ||
               (screen.document.getElementById("screen").style.height != data.maxY + "px")){
                screen.close();
                var width = parseInt(data.maxX) + parseInt(data.tileWidth);
                var height = parseInt(data.maxY) + parseInt(data.tileHeight);
                screen=window.open('',id,'width=' + width + ',height=' + height , true);
            }
            draw(screen, data);
        });
    }
    
    function draw(screen, data) {
        var screenDiv = screen.document.getElementById("screen");
        if (screenDiv == null) screen.document.write('<div id="screen" style="width:' +
                    data.maxX + '; height:' + data.maxY + '; " style="margin: 0 auto;" >');
        var tileY = 0; 
        var tileX = 0;
        var counter = 0;
        for (var i=0;i<(data.maxX/data.tileWidth);i++){ 
            for (var j=0;j<(data.maxY/data.tileHeight);j++){  
                var imageSource = screen.document.getElementById("id_" + counter);
                var path = "http://myserver:8080/images/screen/tile/" + 
                           data.tiles[counter].imageId; 
                if (imageSource == null){
                    screen.document.write('<img id="id_' + counter + '" src="' + path +
                    '" height="' + data.tileHeight + '" width="' + data.tileWidth + '" />');
                }else if(imageSource.src != path){
                    imageSource.src = path;
                }
                tileX = tileX + parseInt(data.tileWidth); 
                counter ++; 
            }
            tileY = tileY + parseInt(data.tileHeight); 
            tileX = 0;
        }
        if (screenDiv == null) screen.document.write('</div>');
    }
    
    </script>
    

    your code are working fine that’s true, but for one instance only, you declare var screen and var inter at the top of the script not in the function, this means that if you click on the first user to get it’s screen (as what i understood of your code, what your project did), the timer of the first user stops and of the second user starts. this code that i wrote solve this problem and your problem to replace the source of the image if it changes.

    hope this help you man,

    GOOD LUCK;

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

Sidebar

Related Questions

We are using jQuery in our project. We have numerous custom javascript files in
While using jquery or javascript code most of the time i have to face
I have a Maven project with JavaScript code. There is a special javascript compiler
I have implement the exact same concept for my project- http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/ My question is,
My latest project is using a javascript framework (jQuery), along with some plugins (validation,
Within a web project I am using jQuery and javascript to build a query
I have function in Javascript which works fine using prototype. The function is used
This is a Rails 3 project using jQuery 1.4.4. I have an index action
I have a little project I am working on and I am using jQuery.
I am fluent in CSS and JS. Using jQuery on this project. I have

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.