# SVG and its boundaries

**URL:** <https://forum.juce.com/t/svg-and-its-boundaries/21922>\
**Category:** General JUCE discussion\
**Created:** [April 21, 2017, 10:54am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922 "2017-04-21T10:54:47Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 2, 2017, 2:14pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/10 "2017-05-02T14:14:51Z")

</div>

as a usecase, i want to resize SVG to match desired bounds while preserving ratio and respecting its viewbox/content area.

and this is how i achieve proper placement and scale, where the scale **“float s”** is sort of a magnify level

* * *

```
AffineTransform getTransformWith(int w, int h, float s, RectanglePlacement::Flags p = RectanglePlacement::fillDestination) {
	if(viewBox.getWidth() == 0 || viewBox.getHeight() == 0) return AffineTransform();
	float newX = 0.f;
	float newY = 0.f;
	float scaleX = w / viewBox.getWidth();
	float scaleY = (h > 0.f ? h : w) / viewBox.getHeight();
	scaleX *= s;
	scaleY *= s;

	if(p != RectanglePlacement::stretchToFit) {
		scaleX = (p == RectanglePlacement::Flags::centred) ? 1.f : jmax(jmin(scaleX, scaleY), 0.0f);
		scaleY = scaleX;
		newX += (w - viewBox.getWidth() * scaleX) / 2.0f; // centre
		newY += ((h > 0.f ? h : w) - viewBox.getHeight() * scaleX) / 2.0f; // centre
	}
	auto at = AffineTransform::scale(scaleX, scaleY).translated(newX, newY);
	return at.isSingularity() ? AffineTransform() : at;
}
```

---

_[View the full topic](https://forum.juce.com/t/svg-and-its-boundaries/21922)._
