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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:44:16+00:00 2026-06-15T02:44:16+00:00

I’ve modified a page where I can drag and drop images onto a canvas.

  • 0

I’ve modified a page where I can drag and drop images onto a canvas. It does everything I want except for one. I’ve tried multiple methods (including scripts, e.g. Kinetic and Raphael, which I still think may be the route to go) but have dead ended:

Once the image is dropped, I can’t drag it on the canvas to a new position.

function drag(e)
{
    //store the position of the mouse relativly to the image position
    e.dataTransfer.setData("mouse_position_x",e.clientX - e.target.offsetLeft );
    e.dataTransfer.setData("mouse_position_y",e.clientY - e.target.offsetTop  );

    e.dataTransfer.setData("image_id",e.target.id);
}

function drop(e)
{
    e.preventDefault();
    var image = document.getElementById( e.dataTransfer.getData("image_id") );

    var mouse_position_x = e.dataTransfer.getData("mouse_position_x");
    var mouse_position_y = e.dataTransfer.getData("mouse_position_y");

    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');

    // the image is drawn on the canvas at the position of the mouse when we lifted the mouse button
    ctx.drawImage( image , e.clientX - canvas.offsetLeft - mouse_position_x , e.clientY - canvas.offsetTop - mouse_position_y );
}

function convertCanvasToImage() {
    var canvas = document.getElementById('canvas');

    var image_src = canvas.toDataURL("image/png");
    window.open(image_src);

}

Here is a JSFiddle that I used as my initial start point – http://fiddle.jshell.net/gael/GF96n/4/ (drag the JSFiddle logo onto the canvas and then try to move it). I’ve since added CSS, tabs, content, etc. to my almost working page. The function I don’t want to lose is the ability to drag the single image multiple times (clone) onto the canvas.

Any ideas/examples/pointers on how to create this functionality?

  • 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-15T02:44:17+00:00Added an answer on June 15, 2026 at 2:44 am

    You need to do a couple of changes to your code, instead of drawing the image immediately to the canvas, you need to keep track of all images dropped. imagesOnCanvas will be filled with all images dropped.

    var imagesOnCanvas = [];
    
    function drop(e)
    {
        e.preventDefault();
        var image = document.getElementById( e.dataTransfer.getData("image_id") );
    
        var mouse_position_x = e.dataTransfer.getData("mouse_position_x");
        var mouse_position_y = e.dataTransfer.getData("mouse_position_y");
    
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
    
        imagesOnCanvas.push({
          context: ctx,  
          image: image,  
          x:e.clientX - canvas.offsetLeft - mouse_position_x,
          y:e.clientY - canvas.offsetTop - mouse_position_y,
          width: image.offsetWidth,
          height: image.offsetHeight
        });
    
    }
    

    You also need an animation loop, which will go through all images in imagesOnCanvas and draw them sequentially. requestAnimationFrame is used to achieve this.

    function renderScene() {
        requestAnimationFrame(renderScene);
    
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        ctx.clearRect(0,0,
            canvas.width,
            canvas.height
        );
    
    
        for(var x = 0,len = imagesOnCanvas.length; x < len; x++) {
            var obj = imagesOnCanvas[x];
            obj.context.drawImage(obj.image,obj.x,obj.y);
    
        }
    }
    
    requestAnimationFrame(renderScene);
    

    Next you will have to monitor mousedown events on canvas, and if the event occurs on an image the startMove action can be called

    canvas.onmousedown = function(e) {
        var downX = e.offsetX,downY = e.offsetY;
    
        // scan images on canvas to determine if event hit an object
        for(var x = 0,len = imagesOnCanvas.length; x < len; x++) {
            var obj = imagesOnCanvas[x];
            if(!isPointInRange(downX,downY,obj)) {
                continue;
            }
    
            startMove(obj,downX,downY);
            break;
        }
    
    }
    

    The isPointInRange function returns true if the mouse event occurred on an image object

    function isPointInRange(x,y,obj) {
        return !(x < obj.x ||
            x > obj.x + obj.width ||
            y < obj.y ||
            y > obj.y + obj.height);
    }
    

    Once ‘move mode’ is active, the x/y coordinates of the object are changed to reflect the new mouse position

    function startMove(obj,downX,downY) {
        var canvas = document.getElementById('canvas');
    
        var origX = obj.x, origY = obj.y;
        canvas.onmousemove = function(e) {
            var moveX = e.offsetX, moveY = e.offsetY;
            var diffX = moveX-downX, diffY = moveY-downY;
    
    
            obj.x = origX+diffX;
            obj.y = origY+diffY;
        }
    
        canvas.onmouseup = function() {
            // stop moving
            canvas.onmousemove = function(){};
        }
    }
    

    Working example here:

    http://jsfiddle.net/XU2a3/41/

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.