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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:36:27+00:00 2026-06-07T17:36:27+00:00

I am dragging an image within a canvas. But during the drag image flickers.

  • 0

I am dragging an image within a canvas. But during the drag image flickers. I think there is some problem in function calling. I have implemented functions with onmousedown, onmouseup and onmousemove events. I am drawing the image onto canvas during drag.

Here is my code,

<html>
    <head>
    </head>
    <body>
    <div>
    <canvas id="canvas5" height="500" width="500" style = "position:relative;left:500px; border:2px  solid black;"> This text is displayed if your browser does not support HTML5 Canvas. </canvas>
    </div>
    <script type="text/javascript">
    var x2 = 100;
    var y2 = 100;
    var ctx; 
    var can;  
    var img=new Image();
    function drawcan() {
    can = document.getElementById("canvas5");
    ctx = can.getContext('2d');
    ctx.clearRect(0, 0, 500, 500);
    img.onload = function(){
    ctx.drawImage(img,x2 - img.width/2,y2 - img.height/2, img.width,img.height);
    };
    img.src="images/213.jpg";
    };

    function myMove(e){
    x1 = e.pageX - x2 - can.offsetLeft;
    x2 = x2 + x1;
    y1 = e.pageY - y2 - can.offsetTop;
    y2 = y2 + y1;
    drawcan();
    };

    function myDown(e) {
    if (e.pageX < x2 + img.width/2 + canvas5.offsetLeft)
    if (e.pageX > x2 - img.width/2 + canvas5.offsetLeft)
    if (e.pageY < y2 + img.height/2 + canvas5.offsetTop)
    if (e.pageY > y2 - img.height/2 + canvas5.offsetTop){ 
    can.onmousemove = myMove;
    };
    };  

    function myUp(e){
    can.onmousemove = null;   
    };

    drawcan();
    can.onmousedown = myDown;
    can.onmouseup = myUp;
    </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-07T17:36:29+00:00Added an answer on June 7, 2026 at 5:36 pm
    function drawcan() {
        can = document.getElementById("canvas5");
        ctx = can.getContext('2d');
        ctx.clearRect(0, 0, 500, 500);
        img.onload = function(){
            ctx.drawImage(img,x2 - img.width/2,y2 - img.height/2, img.width,img.height);
        };
        img.src="images/213.jpg";
    };
    

    See what this does? Every time you call drawcan, it loads the image before painting it somewhen later. Of course, the image is cached, but this process needs its time. Instead, wait for the image to load before doing anything, and then never load it again.

    var img = new Image(),
        load = false,
        can = document.getElementById("canvas5"),
        ctx = can.getContext('2d');
    img.onload = function() {
        load = true;
        drawcan(); // init
    };
    img.src="images/213.jpg";
    
    function drawcan() {
        if (!load) return;
        ctx.clearRect(0, 0, 500, 500);
        ctx.drawImage(img, x2 - img.width/2,y2 - img.height/2, img.width,img.height);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am changing the image matrix (zooming and dragging) and calling imageview.setImageMatrix(matrix) but the
I want to implement dragging of an image within a canvas. I want simplest
So, I'm using this approach to be able to drag an image: Dragging an
I'm trying to create a draggable image, but i'm trying to confine it's dragging
Is there any possibility using the drag&drop technology for dragging desktop images onto a
I am trying to make overlay over image while dragging but its also moving
I have a map image and I would like to be able to drag
In Interface Builder.app (and some other cocoa apps), image dragging has a very nice/sexy
I want to display a custom image while dragging an object in a drag&drop
I have a draggable image within a div. I can click on the image

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.