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

  • Home
  • SEARCH
  • 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 9180937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:10:13+00:00 2026-06-17T18:10:13+00:00

this code draws an image of the user’s local computer on a canvas then

  • 0

this code draws an image of the user’s local computer on a canvas then slide it on some other canvases for slide puzzle game

function drawImage(event) {

        if (event.target.files.length <= 0) return;
        var files = event.target.files;
        var URL = window.URL || window.webkitURL;
        var img = $("#image")[0];

        img.src = URL.createObjectURL(files[0]);

        getCanvas("karajan").drawImage(img, 0, 0, img.width, img.height, 0, 0, 450, 450);

       ...........

    }

    function getCanvas(id) {
        var canvas = $("#" + id)[0];
        if (canvas.getContext) {
            return canvas.getContext("2d");
        }
        return undefined;
    }

and a tag that inputs image file from local computer

<input type="file" accept="image/*" id="image_input" onchange="drawImage(event)"/>
<img id="image"/>

and when user want to change the image, the canvas will draw with previous image. I don’t know why. in the first lines of drawImage function image changes with newer image and draws on canvas

  • 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-17T18:10:15+00:00Added an answer on June 17, 2026 at 6:10 pm

    I assume the image is not actually “loaded” until the current script terminates. That means you should listen to the load event of the image and only then draw it to the canvas.

    I did some restructuring of your code as well. If you already have jQuery available, use it:

    $(function() {
        var URL = window.URL || window.webkitURL;
        var $image = $('#image').on('load', function() {
            // clear canvas here
            getCanvas("karajan")
                .drawImage(this, 0, 0, this.width, this.height, 0, 0, 450, 450);
            // ...
        });
    
        $('#image_input').on('change', function() {
            if (this.files.length === 0) return;
    
            $image.prop('src', URL.createObjectURL(this.files[0]));
        });
    });
    

    You should still clear the canvas though, as Kolink suggested. Otherwise, if a new, smaller image is loaded, parts of the previous, larger image will still be visible.

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

Sidebar

Related Questions

This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
I want to draw image in place where I click. i have this code:
I have class where I draw image. This is code: public class CanvasdrawActivity extends
This code draws a single color (with alpha) shape using a bitmap's alpha channel.
I have this code which draws a Window and NSView, and have set up
I try to draw some lines with different colors. This code tries to draw
In the code below I have two Canvas layers one text and one image,
This program will have an infinite canvas (ie as long as the user scrolls,
Why won't this code draw parabola? It is as simple as it can be
I used this code to draw a line in a panel. private bool isMoving

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.