Basic Transformations - SVG: Scalable Vector Graphics | MDN
Có thể bạn quan tâm
- References
- SVG
- SVG Tutorial
- Basic transformations
- English (US)
- Français
- 日本語
- Русский
- 中文 (简体)
- Example
- Translation
- Rotation
- Multiple transformations
- Skewing
- Scaling
- Complex transformations with matrix()
- Effects on Coordinate Systems
- Embedding SVG in SVG
- SVG
- Tutorials
- Introducing SVG from scratch
- Introduction
- Getting started
- Positions
- Basic shapes
- Paths
- Fills and Strokes
- Gradients in SVG
- Patterns
- Texts
- Basic transformations
- Clipping and masking
- Other content in SVG
- Filter effects
- SVG fonts
- SVG image element
- Tools for SVG
- SVG and CSS
- Reference
- Elements
- <a>
- <animate>
- <animateMotion>
- <animateTransform>
- <circle>
- <clipPath>
- <cursor> Deprecated
- <defs>
- <desc>
- <ellipse>
- <feBlend>
- <feColorMatrix>
- <feComponentTransfer>
- <feComposite>
- <feConvolveMatrix>
- <feDiffuseLighting>
- <feDisplacementMap>
- <feDistantLight>
- <feDropShadow>
- <feFlood>
- <feFuncA>
- <feFuncB>
- <feFuncG>
- <feFuncR>
- <feGaussianBlur>
- <feImage>
- <feMerge>
- <feMergeNode>
- <feMorphology>
- <feOffset>
- <fePointLight>
- <feSpecularLighting>
- <feSpotLight>
- <feTile>
- <feTurbulence>
- <filter>
- <font-face-format> Deprecated
- <font-face-name> Deprecated
- <font-face-src> Deprecated
- <font-face-uri> Deprecated
- <font-face> Deprecated
- <font> Deprecated
- <foreignObject>
- <g>
- <glyph> Deprecated
- <glyphRef> Deprecated
- <hkern> Deprecated
- <image>
- <line>
- <linearGradient>
- <marker>
- <mask>
- <metadata>
- <missing-glyph> Deprecated
- <mpath>
- <path>
- <pattern>
- <polygon>
- <polyline>
- <radialGradient>
- <rect>
- <script>
- <set>
- <stop>
- <style>
- <svg>
- <switch>
- <symbol>
- <text>
- <textPath>
- <title> — the SVG accessible name element
- <tref> Deprecated
- <tspan>
- <use>
- <view>
- <vkern> Deprecated
- Attributes
- accent-height Deprecated
- accumulate
- additive
- alignment-baseline
- alphabetic Deprecated
- amplitude
- arabic-form Deprecated
- ascent Deprecated
- attributeName
- attributeType Deprecated
- azimuth
- baseFrequency
- baseline-shift
- baseProfile Deprecated
- bbox Deprecated
- begin
- bias
- by
- calcMode
- cap-height Deprecated
- class
- clip Deprecated
- clip-path
- clip-rule
- clipPathUnits
- color
- color-interpolation
- color-interpolation-filters
- cursor
- cx
- cy
- d
- data-*
- decoding
- descent Deprecated
- diffuseConstant
- direction
- display
- divisor
- dominant-baseline
- dur
- dx
- dy
- edgeMode
- elevation
- end
- exponent
- fill
- fill-opacity
- fill-rule
- filter
- filterUnits
- flood-color
- flood-opacity
- font-family
- font-size
- font-size-adjust
- font-stretch
- font-style
- font-variant
- font-weight
- fr
- from
- fx
- fy
- g1 Deprecated
- g2 Deprecated
- glyph-name Deprecated
- glyph-orientation-horizontal Deprecated
- glyph-orientation-vertical Deprecated
- gradientTransform
- gradientUnits
- hanging Deprecated
- height
- horiz-adv-x Deprecated
- horiz-origin-x Deprecated
- horiz-origin-y Deprecated
- href
- id
- ideographic Deprecated
- image-rendering
- in
- in2
- intercept
- k Deprecated
- k1
- k2
- k3
- k4
- kernelMatrix
- kernelUnitLength
- keyPoints
- keySplines
- keyTimes
- lang
- lengthAdjust
- letter-spacing
- lighting-color
- limitingConeAngle
- marker-end
- marker-mid
- marker-start
- markerHeight
- markerUnits
- markerWidth
- mask
- maskContentUnits
- maskUnits
- mathematical Deprecated
- max
- media
- method Experimental
- min
- mode
- name Deprecated
- numOctaves
- opacity
- operator
- order
- orient
- orientation Deprecated
- origin
- overflow
- overline-position
- overline-thickness
- paint-order
- panose-1 Deprecated
- path
- pathLength
- patternContentUnits
- patternTransform
- patternUnits
- pointer-events
- points
- pointsAtX
- pointsAtY
- pointsAtZ
- preserveAlpha
- preserveAspectRatio
- primitiveUnits
- r
- radius
- refX
- refY
- repeatCount
- repeatDur
- requiredFeatures Deprecated
- restart
- result
- rotate Experimental
- rx
- ry
- scale
- seed
- shape-rendering
- side Experimental
- slope Deprecated
- spacing
- specularConstant
- specularExponent
- spreadMethod
- startOffset
- stdDeviation
- stemh Deprecated
- stemv Deprecated
- stitchTiles
- stop-color
- stop-opacity
- strikethrough-position
- strikethrough-thickness
- string Deprecated
- stroke
- stroke-dasharray
- stroke-dashoffset
- stroke-linecap
- stroke-linejoin
- stroke-miterlimit
- stroke-opacity
- stroke-width
- style
- surfaceScale
- SVG attribute: crossorigin
- systemLanguage
- tabindex
- tableValues
- target
- targetX
- targetY
- text-anchor
- text-decoration
- text-rendering
- textLength
- to
- transform
- transform-origin
- type
- u1 Deprecated
- u2 Deprecated
- underline-position
- underline-thickness
- unicode Deprecated
- unicode-bidi
- unicode-range Deprecated
- units-per-em Deprecated
- v-alphabetic Deprecated
- v-hanging Deprecated
- v-ideographic Deprecated
- v-mathematical Deprecated
- values
- vector-effect
- version Deprecated
- vert-adv-y Deprecated
- vert-origin-x Deprecated
- vert-origin-y Deprecated
- viewBox
- visibility
- width
- widths Deprecated
- word-spacing
- writing-mode
- x
- x-height Deprecated
- x1
- x2
- xChannelSelector
- xlink:arcrole Deprecated
- xlink:href Deprecated
- xlink:show Deprecated
- xlink:title Deprecated
- xlink:type Deprecated
- xml:lang Deprecated
- xml:space Deprecated
- y
- y1
- y2
- yChannelSelector
- z
- zoomAndPan Deprecated
- Guides
-
- Applying SVG effects to HTML content
- Content type
- Namespaces crash course
- SVG animation with SMIL
- SVG as an Image
- Example
- Translation
- Rotation
- Multiple transformations
- Skewing
- Scaling
- Complex transformations with matrix()
- Effects on Coordinate Systems
- Embedding SVG in SVG
- « Previous
- Next »
Now we're ready to start distorting our beautiful images. But first, let's formally introduce the <g> element. With this helper, you can assign properties to a complete set of elements. Actually, that's its only purpose.
Example
html<svg width="30" height="10"> <g fill="red"> <rect x="0" y="0" width="10" height="10" /> <rect x="20" y="0" width="10" height="10" /> </g> </svg>All following transformations are summed up in an element's transform attribute. Transformations can be chained by concatenating them, separated by whitespace.
Translation
It may be necessary to move an element around, even though you can position it with the according attributes. For this purpose, the translate() transformation stands ready.
html<svg width="40" height="50" style="background-color:#bff;"> <rect x="0" y="0" width="10" height="10" transform="translate(30,40)" /> </svg>The example will render a rectangle, translated to the point (30,40) instead of (0,0).
If the second value is not given, it is assumed to be 0.
Rotation
Rotating an element is quite a common task. Use the rotate() transformation for this:
html<svg width="31" height="31"> <rect x="12" y="-10" width="20" height="20" transform="rotate(45)" /> </svg>This example shows a square that is rotated by 45 degrees. The value for rotate() is given in degrees.
Multiple transformations
Transformations can be concatenated easily just by separating them with spaces. For example, translate() and rotate() are common used transformations.
html<svg width="40" height="50" style="background-color:#bff;"> <rect x="0" y="0" width="10" height="10" transform="translate(30,40) rotate(45)" /> </svg>This example shows again the small square shown above that this time is also rotated by 45 degrees.
Skewing
To make a rhombus out of our rectangle, the skewX() and skewY() transformations are available. Each one takes an angle that determines how far the element will be skewed.
Scaling
scale() changes the size of an element. It takes two numbers, the first being the x scale factor and the second being the y scale factor. The factors are taken as the ratio of the transformed dimension to the original. For example, 0.5 shrinks by 50%. If the second number is omitted, it is assumed to be equal to the first.
Complex transformations with matrix()
All the above transformations can be expressed by a 2x3 transformation matrix. To combine several transformations, one can set the resulting matrix directly with the matrix(a, b, c, d, e, f) transformation which maps coordinates from a previous coordinate system into a new coordinate system by
{ x newCoordSys = a x prevCoordSys + c y prevCoordSys + e y newCoordSys = b x prevCoordSys + d y prevCoordSys + f \left{ \begin{matrix} x*{\mathrm{prevCoordSys}} = a x*{\mathrm{newCoordSys}} + c y*{\mathrm{newCoordSys}} + e \ y*{\mathrm{prevCoordSys}} = b x*{\mathrm{newCoordSys}} + d y*{\mathrm{newCoordSys}} + f \end{matrix} \right.
See a concrete example on the SVG transform documentation. Detailed information about this property can be found in the SVG Recommendation.
Effects on Coordinate Systems
When using transformations you establish a new coordinate system inside the element the transformations apply to. That means, the units you specify for the element and its children might not follow the 1:1 pixel mapping, but are also distorted, skewed, translated and scaled according to the transformation.
html<svg width="100" height="100"> <g transform="scale(2)"> <rect width="50" height="50" /> </g> </svg>The resulting rectangular in the above example will be 100x100px. The more intriguing effects arise, when you rely on attributes like userSpaceOnUse and the such.
Embedding SVG in SVG
In contrast to HTML, SVG allows you to embed other svg elements seamlessly. This way you can also create new coordinate systems by utilizing the viewBox, width and height of the inner svg element.
html<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100"> <svg width="100" height="100" viewBox="0 0 50 50"> <rect width="50" height="50" /> </svg> </svg>The example above has basically the same effect as the one above, namely that the rect will be twice as large as specified.
- « Previous
- Next »
Help improve MDN
Was this page helpful to you?YesNoLearn how to contribute.This page was last modified on Jul 2, 2024 by MDN contributors.
View this page on GitHub • Report a problem with this contentTừ khóa » Html Scale Svg Element
-
How To Scale SVG | CSS-Tricks
-
Resizing SVG In Html? - Stack Overflow
-
Scale - SVG: Scalable Vector Graphics - MDN Web Docs
-
Intrinsic Sizing - SVG
-
How To Make An Svg Scale With Its Parent Container ? - GeeksforGeeks
-
How To Resize SVG In HTML - W3docs
-
Html Scale Svg Code Example - Code Grepper
-
Scale SVG Graphics With Only Css - Gists · GitHub
-
Resizing An SVG When The Window Is Resized In D3.js - Chartio
-
SVG Viewport And ViewBox (For Complete Beginners) - Web Design
-
Scaling SVG Elements - CodePen
-
HTML Svg ViewBox Attribute - Dofactory
-
Html – Scale An SVG Element Using A Transform Origin - ITecNote
-
How To Resize A SVG Image - Medium