Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active August 22, 2019 02:42
Show Gist options
  • Save dotherightthing/c0807cbc75abc25ac56131b999d1cf4b to your computer and use it in GitHub Desktop.
Save dotherightthing/c0807cbc75abc25ac56131b999d1cf4b to your computer and use it in GitHub Desktop.
[Changing an SVG's natural dimensions] #icons #github #artboard

Changing an SVG's natural dimensions

Background

I want to insert several 3rd party 'technology badges' into my Github README, with the following constraints:

  • I want to use SVGs, so the images look crisp on all screens
  • I'm limited to inserting the SVGs as <img>s
  • Github Flavoured Markdown doesn't provide any controls to resize my <img>s
  • I have no control over the page CSS

In this scenario, the SVGs will display at their natural dimensions, i.e. the size of the artboard that they were authored on.

Source images

The first technology badge, x-mocha.svg is perfectly square, and nicely sized to 192px * 192px:

<svg xmlns="http://www.w3.org/2000/svg" width="192" height="192">

The second technology badge, x-nodejs.svg, is larger, and isn't perfectly square:

<svg width="256px" height="289px" viewBox="0 0 256 289" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">

I want to scale x-nodejs.svg down, so that it can sit next to x-mocha.svg.

Scale the .svg

In x-nodejs.svg, the height, 289px, is the larger of the two dimensions, so I'll use that to calculate the scale involved.

But first, let's inspect x-nodejs.svg, to see how it is positioned on its artboard:

[screenshot]

Ah, there's a 6px gutter around the outside. We need to take this into account when matching the image heights:

  • match the SVG heights
  • use the viewbox to scale x-nodejs.svg to match x-mocha.svg
  • use the viewbox to offset x-nodejs.svg to match x-mocha.svg

First, calculate the scale:

  • targetHeight (192) / nodejsHeight (289) = scale (0.66435986)

Then calculate the new image dimensions

  • nodejsNewWidth = nodeJsWidth (256) * scale (0.62283737) = 170.07612416
  • nodejsNewHeight = 192 (match x-mocha.svg)

Open x-nodejs.svg in a text editor and update the dimensions (you can leave out the px):

<svg width="170.07612416" height="192" viewBox="0 0 256 289" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">

The image heights now match, but x-nodejs.svg is displaying larger than x-mocha.svg.

Scale the viewbox dimensions

<svg width="170.07612416" height="192" viewBox="0 0 256 289" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">

The rule here, is that a larger width and height makes the image appear to be smaller / zoomed out:

First, get the 6px gutters (top/bottom and left/right) as a fraction of the artboard:

  • gutter (6 * 2) / 192 = 0.0625

Then scale up the viewbox by this amount:

  • nodejsViewportWidth (256) * ( 1 + 0.0625 ) = 272
  • nodejsViewportHeight (289) * ( 1 + 0.0625 ) = 307.0625

Update the x-nodejs.svg:

<svg width="170.07612416" height="192" viewBox="0 0 272 307.0625" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">

The SVGs are now the same height, but are not aligned vertically due to the 6px gutter.

Scale the viewbox position

Working backwards, I can see that I need to pan left and up by 9.5 to create the visual gutter:

<svg width="170.07612416" height="192" viewBox="-9.5 -9.5 272 307.0625" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">

How do I calculate this?


Links

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="256px" height="289px" viewBox="0 0 256 289" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
<g>
<path d="M127.999999,288.463771 C124.024844,288.463771 120.314699,287.403728 116.869564,285.548656 L81.6231884,264.612838 C76.32298,261.697724 78.9730854,260.637682 80.5631458,260.107661 C87.7184259,257.72257 89.0434775,257.192547 96.4637688,252.952381 C97.2587979,252.422361 98.3188405,252.687372 99.1138718,253.217392 L126.144927,269.383024 C127.20497,269.913045 128.530021,269.913045 129.325053,269.383024 L235.064182,208.165634 C236.124225,207.635611 236.654245,206.575571 236.654245,205.250519 L236.654245,83.0807467 C236.654245,81.7556929 236.124225,80.6956526 235.064182,80.1656324 L129.325053,19.2132506 C128.26501,18.6832305 126.939959,18.6832305 126.144927,19.2132506 L20.4057954,80.1656324 C19.3457551,80.6956526 18.8157349,82.0207041 18.8157349,83.0807467 L18.8157349,205.250519 C18.8157349,206.31056 19.3457551,207.635611 20.4057954,208.165634 L49.2919247,224.861286 C64.9275364,232.811595 74.7329196,223.536234 74.7329196,214.260871 L74.7329196,93.681159 C74.7329196,92.0910985 76.0579711,90.5010358 77.9130428,90.5010358 L91.4285716,90.5010358 C93.0186343,90.5010358 94.6086948,91.8260873 94.6086948,93.681159 L94.6086948,214.260871 C94.6086948,235.196689 83.2132512,247.387164 63.3374737,247.387164 C57.2422362,247.387164 52.4720502,247.387164 38.9565214,240.761906 L11.1304347,224.861286 C4.24016581,220.886129 5.68434189e-14,213.46584 5.68434189e-14,205.515528 L5.68434189e-14,83.3457557 C5.68434189e-14,75.3954465 4.24016581,67.9751552 11.1304347,64.0000006 L116.869564,2.78260752 C123.494824,-0.927535841 132.505176,-0.927535841 139.130436,2.78260752 L244.869565,64.0000006 C251.759834,67.9751552 256,75.3954465 256,83.3457557 L256,205.515528 C256,213.46584 251.759834,220.886129 244.869565,224.861286 L139.130436,286.078676 C135.685299,287.668739 131.710145,288.463771 127.999999,288.463771 L127.999999,288.463771 Z M160.596274,204.455488 C114.219461,204.455488 104.679089,183.254659 104.679089,165.233955 C104.679089,163.643893 106.004141,162.053832 107.859212,162.053832 L121.639752,162.053832 C123.229813,162.053832 124.554864,163.113872 124.554864,164.703935 C126.674947,178.749484 132.770187,185.639753 160.861283,185.639753 C183.122154,185.639753 192.662526,180.604556 192.662526,168.67909 C192.662526,161.788821 190.012423,156.753624 155.296065,153.308489 C126.409938,150.393375 108.389235,144.033126 108.389235,120.977226 C108.389235,99.5113875 126.409938,86.7908901 156.621119,86.7908901 C190.542443,86.7908901 207.238095,98.4513472 209.358178,123.89234 C209.358178,124.687371 209.093167,125.482403 208.563147,126.277434 C208.033127,126.807454 207.238095,127.337474 206.443064,127.337474 L192.662526,127.337474 C191.337475,127.337474 190.012423,126.277434 189.747412,124.952382 C186.567289,110.376813 178.351966,105.606625 156.621119,105.606625 C132.240165,105.606625 129.325053,114.086957 129.325053,120.447205 C129.325053,128.132506 132.770187,130.5176 165.631471,134.757766 C198.227744,138.997931 213.598344,145.093169 213.598344,167.884058 C213.333333,191.20497 194.252589,204.455488 160.596274,204.455488 L160.596274,204.455488 Z" fill="#539E43"></path>
</g>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment