I
I
ivanovSP2017-10-17 18:53:01
JavaScript
ivanovSP, 2017-10-17 18:53:01

How to drag SVG with mouse?

There is a circle on the svg canvas, how can I move it with the mouse?
Please tell me some plugin without JQ

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-10-17
@Stalker_RED

ball.addEventListener('mousedown', function(evt) {
  svg.addEventListener('mousemove', drag)
})
ball.addEventListener('mouseup', function(evt) {
  svg.removeEventListener('mousemove', drag)
})
svg.addEventListener('mouseleave', function(evt) {
  svg.removeEventListener('mousemove', drag)
})

function drag(evt) {
  let x = evt.x - ball.cx.baseVal.value
  let y = evt.y - ball.cy.baseVal.value
  ball.setAttribute('transform', 'translate(' + x + ', ' + y + ')')
}

https://jsfiddle.net/Stalk/vts4dg8z/
From the libraries I can recommend d3.js and svg.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question