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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:53:05+00:00 2026-06-10T11:53:05+00:00

I have two html img objects with different src urls. I’d like to combine

  • 0

I have two html img objects with different src urls. I’d like to combine these two images (using canvas), and create one merged img object. I want to give user to select a image from his location and second image will be logo and user can change the location of logo and when click on post button one image object is generated and able to save in database. I am getting one error while writting this javascript object type mismatch I do not under where is the issue…

 <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

    </head>
    <body>
    <form name="drawImage" id="drawImage">
    <input type="file" id="filename" name="filename" value="hello" />
    <canvas id="mycanvas" width="400" height="400"></canvas>
    <input type="button" id="mybtn" name="mybtn" value="hello" />
    </form>
    <script>
    $(function(){
        var canvas,ctx,img1,img2;
        var imagesLoaded = 0;
        function main() {
            imagesLoaded += 1;
            var canvas = document.getElementById("mycanvas");
            var ctx = canvas.getContext("2d");
            if(imagesLoaded == 2) {
                // composite now
                console.log(img1);
                ctx.drawImage(img1, 0, 0);
                //ctx.globalAlpha = 1;
                ctx.drawImage(img2, 0,100);
            }
        }
        function loadImage(src, onload) {
            // http://www.thefutureoftheweb.com/blog/image-onload-isnt-being-called
            var img = new Image();
            img.onload = onload;
            img.src = src;
            return img;
        }
        function drawImages(){
            var img1 = loadImage('http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png', main);
            var img2 = loadImage('http://blog.carazoo.com/wp-content/uploads/2010/11/skoda_logo-300x300.jpg', main);
        }

        $("#mybtn").click(function(){
            //var getImageInput=$("#filename").val();
            //var img1 = loadImage(getImageInput, main);
            //var img2 = loadImage('play-button.png', main);
            drawImages();   
        });
    });
    </script>
    </body>

    </html>
  • 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-10T11:53:07+00:00Added an answer on June 10, 2026 at 11:53 am

    The problem is that img1 and img2 are locally redeclared inside drawImages() and thus the img1 and img2 variables inside the main() scope point to the undefined img1 and img2 in your global scope. The same goes for canvas and ctx, although this doesn’t cause any issues since they’re only used inside main().

    In short, you can “fix” this by removing the var inside drawImages():

        function drawImages(){
            img1 = loadImage('http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png', main);
            img2 = loadImage('http://blog.carazoo.com/wp-content/uploads/2010/11/skoda_logo-300x300.jpg', main);
        }
    

    Additionally, you could remove the canvas and ctx from the global scope, since they’re only used inside main().

        var img1, img2;
        var imagesLoaded = 0;
    
        function main() {
            imagesLoaded += 1;
            var canvas = document.getElementById("mycanvas");
            var ctx = canvas.getContext("2d");
            // ...
        }
    

    However, you might want to do a more thorough rewrite of your code and properly scope your variables. You want to load the two images first and then render them on the canvas. Those are two distinct problems and it’s better to separate the code dealing with them.

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

Sidebar

Related Questions

I have two html img objects with different src urls. I'd like to combine
I am trying to change the HTML image src using Javascript. I have two
I have an HTML document where I have two different tables. One is class
I have two images in the background of my html doc. One positioned top
I have two text boxes t1 and t2 in an html page. I'd like
Say I have two <img> in a HTML page and both refer to the
I am working on a program using python, HTML and javascript. I have two
I am having trouble using JQuery with ASP.net. I have a HTML img ,
I have two regualar rexpressions I would like to combine into the one for
I have this HTML code: <span class=left> <img class=avatar src=image.jpg /> </span> <span class=right>

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.