I am glad that someone else is looking for solutions to these problems. SVG doesn't seem to be paying enough attention yet.
I have been watching and working on solutions for several months. First, you have three options for moving SVGs.
- Using viewBox, as you said. I think this is the best solution if you want to consider the image as a separate element.
- You can use css transformations of an SVG element. the disadvantage is that it causes pixelation, but means that you can use the solutions that exist for other types of elements.
- You can use svg conversions for elements or groups of elements in SVG.
To answer the question about the code, a centered pinch can be described as follows. First, you need to translate the pinch event center point from the screen coordinate to the SVG coordinates using the CTT screen matrix (coordinate transformation matrix). if the point has coordinates (x, y), then to start your viewing you need a transformation equivalent to
- translation (-x, -y)
- scalefactor
- translation (x, y)
I did this work βmainlyβ in a library that I called hammerhead , which runs on top of hammerjs. Here is an example of this in action. I will not give you code to exactly solve your problem, because there is too much code to go where you put the '...' I ended up writing a viewBox to manipulate. For reference, the code I use to manage this is used here.
scale: function(scale, center){ var boxScale = 1.0/scale; center = center || this.center(); var newMinimal = this.getMinimal().subtract(center).multiply(boxScale).add(center); var newMaximal = this.getMaximal().subtract(center).multiply(boxScale).add(center); var newViewBox = viewBox(newMinimal, newMaximal, this.getValidator()); return newViewBox.valid()? newViewBox : null; }
This method is not without problems. Changing the viewBox is very computationally intensive. I will not make a pleasant scroll on the phone, except for the image with a very small number of elements. This will work well if you change the view once in response to an action. for example, from left to right. I explored some of the other options mentioned above in these repositories.
As I said, I tried it for quite some time. All of them use one of the approaches individually. To get both smooth scrolling and the lack of pixelation, I think a combination of both is required. Why am I working on another library.
source share