HTML Academy
Coordinate systems
Dimensions in SVG14/15
Back to the list of tasks
  • 1. SVG Width and Height
  • 2. The viewBox Attribute
  • 3. viewBox and Dimensions
  • 4. Challenge: Fish
  • 5. The preserveAspectRatio Attribute
  • 6. Flexible Background with preserveAspectRatio
  • 7. Challenge: Flexible Margins
  • 8. Alignment in preserveAspectRatio
  • 9. preserveAspectRatio and viewBox
  • 10. Filling the Space
  • 11. Challenge: Rainbow
  • 12. Units
  • 13. Coordinate systems
  • 14. Coordinate Systems and Transforms
  • 15. Challenge: Yellow Leaf
Challenge: Yellow Leaf
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Coordinate Systems and Transforms

Transforms also create their own coordinate system. To apply a transform to all content, wrap it in a group (<g> element):

<svg width="350" height="200">
  <g>
    …
  </g>
</svg>

And add a transform:

<svg width="350" height="200">
  <g transform="translate(50, 50)">
    …
  </g>
</svg>

All content has now shifted by 50 pixels vertically and horizontally together with the coordinate system, and if we now add another transform, it will be calculated using the new coordinate system:

<svg width="350" height="200">
<g transform="translate(50, 50) rotate(15)">
  …
</g>
</svg>

In SVG, the default pivot point is 0,0. Before the first transform, it was the upper left corner of the viewport, but after the transform, it has become the upper left corner of the transformable content. The second transform will again change the coordinate system for the group.

This assignment uses SVG transforms. Of course, you can learn a lot about CSS transforms in the “2D Transforms” section.

Comments

  • object.svg
  • style.css
SVG
<svg width="400" height="300"> <g class="bear"> <rect width="100%" height="100%" fill="none" stroke="yellowgreen" /> <g fill="orange" stroke="saddlebrown" stroke-width="15"> <circle r="23" cx="32" cy="31" /> <circle r="23" cx="168" cy="31" /> </g> <ellipse fill="saddlebrown" rx="84" ry="70" cx="100" cy="80" /> <circle fill="none" stroke="#000000" stroke-opacity="0.5" stroke-width="3" r="4" cx="100" cy="135" /> <g> <ellipse fill="orange" rx="24" ry="28" cx="77" cy="78" transform="rotate(-30,77,78)" /> <ellipse fill="white" rx="14" ry="16" cx="79" cy="83" transform="rotate(-30,79,83)" /> <circle fill="black" r="9" cx="82" cy="87" /> <circle fill="white" r="3" cx="84" cy="83" /> </g> <g> <ellipse fill="orange" rx="24" ry="28" cx="123" cy="78" transform="rotate(30,123,78)" /> <ellipse fill="white" rx="14" ry="16" cx="120" cy="83" transform="rotate(30,120,83)" /> <circle fill="black" r="9" cx="118" cy="87" /> <circle fill="white" r="3" cx="121" cy="83" /> </g> <g stroke="#000000" stroke-opacity="0.5" stroke-width="5" stroke-linecap="round"> <path d="M55,45 68,42" /> <path d="M145,45 135,42" /> </g> <g> <ellipse fill="black" rx="25" ry="15" cx="100" cy="107" /> <ellipse fill="white" fill-opacity="0.1" rx="17" ry="9" cx="102" cy="103" /> <ellipse fill="white" fill-opacity="0.1" rx="8" ry="4" cx="104" cy="100" /> </g> </g> </svg>
CSS
svg { border: 2px solid teal; }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

The code has changed, click “Refresh” or turn autorun on.

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Goalscompleted
0
    1. Assign the transform="rotate(45)" transform to the .bear group. The group content will rotate by 45 degrees, and the center of the transform will now be in the upper left corner of the viewport.
    2. Now, before the rotation transform, add an offset transform: translate (200). The content of the group will first be shifted by 200 pixels horizontally, thus creating a new coordinate system, and the figure will rotate 45 degrees relative to it.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.