I need to implement pinch to zoom and drag to pan over an SVG. This SVG gets shown at 480*480 pixels, but its real size is 1920*1920 pixels.
I went with HammerJS, and listened for pinch
and drag
:
$(document).hammer().on('pinch', $.throttle(30, setZoom)).
on('drag', $.throttle(30, moveTo));
Inside those methods (setZoom
, moveTo
) I'm already preventing the default action for the event, as suggested by Hammer creator
ev.gesture.preventDefault()
As you can see, I'm throttling the handlers using jQuery.throttle.
My zoom and pan implementation works by changing the viewbox
attribute of the SVG. So that to show the whole image I set said attribute to viewbox="0 0 1920 1920"
, and to show a particular I set it to viewbox="100 100 1720 1720"
.
(To manipulate the SVG I'm using svg.js)
Finally, the SVG has this structure:
- One image (1920*1920 pixels),
- 20 lines, inside a group, that represent a grid,
- More or less 50 groups, each containing an
ellipse
and atext > tspan
elements.
This is part of the code of the SVG:
<image id="SvgjsImage1027" xlink:href="/exams/still_picture/exam-4" width="1920" height="1920" x="0" y="0"></image>
<g id="SvgjsG1002" class="grid">
<line id="SvgjsLine1003" x1="0" y1="0" x2="1920" y2="0"></line>
…
</g>
<g id="SvgjsG1026" class="points">
<g id="s10621674-24" class="threshold">
<ellipse id="SvgjsEllipse1029" rx="7" ry="7" cx="1062" cy="1674" fill="#2b96d9"></ellipse>
<text id="SvgjsText1030" style="font-size:40;font-family:Helvetica, Arial, sans-serif;text-anchor:middle;textLength:1;" font-size="40" text-anchor="middle" x="1067.6568542494924" y="1690.5354797464468">
<tspan id="SvgjsTspan1031" dy="36.93333336" x="1067.6568542494924" style="font-size:40;font-family:Helvetica, Arial, sans-serif;text-anchor:middle;textLength:1;">24</tspan>
</text>
</g>
…
</g>
Question
Performance on Android is pretty bad. We've tried on Chrome 26 (Chrome 26.0.1410.58
) and Chrome Beta 27 (Chrome 27.0.1453.74
) on a Samsung Galaxy Tab 2, but it feels sluggish.
We've tried to open the same page on an iPad 2 (Safari on iOS 6), and it works just right, with performance that's comparable to a native app.
What can we try in order to improve performance on Android? We cannot switch to another browser (say, Firefox Mobile), so the solution must be in the HTML/Javascript land.
We've also tried to use some libraries to handle pinch to zoom and drag to pan (for example jQuery.panzoom) but we haven't had better results.