Make image drawn on canvas draggable with JavaScript

前端 未结 2 1888
醉梦人生
醉梦人生 2020-12-13 04:29

I was able to successfully draw an image on an HTML canvas. But I need to be able to drag the image about on the canvas.

I know this function can be implemented easi

2条回答
  •  再見小時候
    2020-12-13 05:14

    To do dragging you handle 3 mouse events:

    1. mousedown -- set a flag indicating that the drag has begun.

    2. mouseup -- clear that drag flag because the drag is over

    3. mousemove -- if the drag flag is set, clear the canvas and draw the image at the mouse position

    Here is some code:

    
    
    
     
    
    
    
    
    
    
    
    
    
        
    
    
    

提交回复
热议问题