A
A
Alexey Lebedev2014-12-03 22:25:12
JavaScript
Alexey Lebedev, 2014-12-03 22:25:12

How to programmatically enlarge SVG on click?

I am using SVGP. Everything is perfectly scaled by mouse scroll.
However, it turned out that not everyone has a mouse wheel.
I wanted to make 2 buttons for zooming in and out, but there is no documentation. Didn't find the answer on google.
Maybe someone has dealt with something similar.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uaSaint, 2014-12-05
@uaSaint

I'm not familiar with the library, but as far as I can see from the demo ( www.cyberz.org/projects/SVGPan/tiger.svg ) the following matters:
1. onmouseup="handleMouseUp(evt)"
2. onmousedown="handleMouseDown(evt)"
3 handleMouseMove(evt)
It looks like it's enough to call them when the button is clicked, or use them as an example to extend the code.
Required code in this place:

/**
 * Handle mouse move event.
 */
function handleMouseMove(evt) {
  if(evt.preventDefault)
    evt.preventDefault();

  evt.returnValue = false;

  var svgDoc = evt.target.ownerDocument;

  var g = getRoot(svgDoc);

  if(state == 'pan' && enablePan) {
    // Pan mode
    var p = getEventPoint(evt).matrixTransform(stateTf);

    setCTM(g, stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
  } else if(state == 'drag' && enableDrag) {
    // Drag mode
    var p = getEventPoint(evt).matrixTransform(g.getCTM().inverse());

    setCTM(stateTarget, root.createSVGMatrix().translate(p.x - stateOrigin.x, p.y - stateOrigin.y).multiply(g.getCTM().inverse()).multiply(stateTarget.getCTM()));

    stateOrigin = p;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question