Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript drawImage on a canvas in the position where an element has been dropped into it?

yumedoll

Active Coder
I'm making a game about baking a decorating a cake using Interact.js.

Everything is done so far but I'm trying to figure out how to make it so the image that is dragged onto the cake (which has a canvas in it's div) will be printed on the canvas at the exact position it was dropped.

The idea is:
- There's a menu full of img elements which are all decorations
  • The user clicks and drags it onto the cake.
  • Once dropped, the img element returns back to its original position but it leaves behind an image of itself drawn on the cakes canvas element where it was dropped.

Things to note:
- The canvas itself is not the dropzone but rather its parent div.
- draggableElement refers to the events related target, the draggable element.
- This is based off of the Interactjs library but what I need doesn't seem to be provided by the library.
- splotch is a sound effect.
- This is part of a switch statement that fires a different line of code for each element dropped into any element with the class "dropzone" depending on the dropped element's class, this is the last case of the switch statement.
- draggableElement.removeAttribute("data-x"); draggableElement.removeAttribute("data-y"); is required for the img element to return back to it's original position.
- I set the x and y coordinates of the drawImage to 100 each just to see if the image prints or not and it does just fine.

JavaScript:
    case cls.contains('decopart'):
      splotch.play();
      var decoposX = parseFloat(draggableElement.getAttribute("data-x"))*100;
      decoctx.drawImage(draggableElement, 100, 100);
      draggableElement.removeAttribute("data-x");
      draggableElement.removeAttribute("data-y");
      draggableElement.style.transform = 'translate(0px, 0px)';
      draggableElement.style.webkitTransform = 'translate(0px, 0px)';

What I've tried:
- This method here just freezes the draggable element in place after it is dropped and doesn't print the image.
-This little doohickey:
JavaScript:
 case cls.contains('decopart'):
      splotch.play();
      var decoposX = parseFloat(draggableElement.getAttribute("data-x"))*100;
      var decoposY = parseFloat(draggableElement.getAttribute("data-y"))*100;
      decoctx.drawImage(draggableElement, decoposX, decoposY);
      draggableElement.removeAttribute("data-x");
      draggableElement.removeAttribute("data-y");
      draggableElement.style.transform = 'translate(0px, 0px)';
      draggableElement.style.webkitTransform = 'translate(0px, 0px)';
but it doesn't work either

You can see the full script involving this here and you can actually try the game here (You'll need to get through the cake baking and mixing process first before you reach the part I'm talking about though, be careful not to burn the cake!! ⁽⁠⁽⁠◝⁠(⁠ ⁠•⁠௰⁠•⁠ ⁠)⁠◜⁠⁾⁠⁾)
 
I decided to give Fabric.js a try instead, but if anyone has any suggestions on this method, go ahead and let me know about it as I might be able to use it in the future for another game.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom