-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
326 lines (278 loc) · 9.14 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
---
title: transform-origin
slug: Web/SVG/Attribute/transform-origin
page-type: svg-attribute
browser-compat: svg.global_attributes.transform-origin
---
{{SVGRef()}}
The **`transform-origin`** SVG attribute sets the origin for an item's transformations.
You can use this attribute with any SVG element.
> [!NOTE]
> As a presentation attribute in SVG, `transform-origin` corresponds in syntax and behavior to the `transform-origin` property in CSS, and can be used as CSS property to style SVG. See the [CSS transform-origin](/en-US/docs/Web/CSS/transform-origin) property for more information.
## Usage notes
<table class="properties">
<tbody>
<tr>
<td><strong>Values</strong></td>
<td>See {{cssxref("transform-origin", "", "#formal_syntax")}}</td>
</tr>
<tr>
<td><strong>Default value</strong></td>
<td><code>0, 0</code></td>
</tr>
<tr>
<td><strong>Animatable</strong></td>
<td>Yes</td>
</tr>
</tbody>
</table>
> [!NOTE]
> The default value of `transform-origin` is `0 0` for all SVG elements except for root `<svg>` elements and `<svg>` elements that are a direct child of a [foreignObject](/en-US/docs/Web/SVG/Element/foreignObject), and whose transform-origin is `50% 50%`, like other CSS elements.
The `transform-origin` property may be specified using one, two, or three values, where each value represents an offset. Offsets that are not explicitly defined are reset to their corresponding [initial values](/en-US/docs/Web/CSS/initial_value).
If a single {{cssxref("<length>")}} or {{cssxref("<percentage>")}} value is defined, it represents the horizontal offset.
If two or more values are defined and either no value is a keyword, or the only used keyword is `center`, then the first value represents the horizontal offset and the second represents the vertical offset.
- One-value syntax:
- The value must be a {{cssxref("length")}}, or one of the keywords `left`, `center`, `right`, `top`, and `bottom`.
- Two-value syntax:
- One value must be a {{cssxref("length")}}, a {{cssxref("percentage")}}, or one of the keywords `left`, `center`, and `right`.
- The other value must be a {{cssxref("length")}}, a {{cssxref("percentage")}}, or one of the keywords `top`, `center`, and `bottom`.
- Three-value syntax:
- The first two values are the same as for the two-value syntax.
- The third value must be a {{cssxref("length")}}. It always represents the Z offset.
## Example
This example shows the code for one PNG image and three SVG images:
1. A PNG reference image.
2. An SVG reference image that uses no transformation.
3. An SVG image that uses `transform-origin` to do a transformation, with the expected result being an image identical to the reference image.
4. An SVG image that does not use `transform-origin` but does the same transformation using just [`transform`](/en-US/docs/Web/SVG/Attribute/transform), with the expected result being an image identical to the reference image.
The fourth image shows how to do the transformation in browsers that don't support `transform-origin` — because the code for the fourth image does the same transformation as the third image's `transform-origin`-based code, but by only using `transform`, without `transform-origin`.
> [!NOTE]
> These examples use a modified version of a code snippet in a Stack Overflow [question](https://stackoverflow.com/questions/67057190/safari-doesnt-respect-transform-origin-svg-attribute) from [Maxim Kulikov](https://stackoverflow.com/users/1033939/maxim-kulikov), as well as a modified version of a code snippet in an [answer](https://stackoverflow.com/questions/67057190/safari-doesnt-respect-transform-origin-svg-attribute/67057754) from [Michael Mullany](https://stackoverflow.com/users/271353/michael-mullany) that accompanies the question. Both code snippets are used under the terms of the [CC BY-SA](https://creativecommons.org/licenses/by-sa/4.0/) license.)
### HTML
```html
<h4>Reference image</h4>
<div>
<figure>
<img src="reference.png" alt="PNG reference image" />
<figcaption>
Figure 1. PNG reference image. The images following this should look
exactly the same as this.
</figcaption>
</figure>
</div>
<div>
<figure>
<svg
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
viewBox="0 0 200 200">
<circle cx="100" cy="100" r="100" stroke="none" fill="black" />
<line
x1="100"
y1="0"
x2="100"
y2="200"
stroke="rebeccapurple"
stroke-width="2" />
<line
x1="0"
y1="100"
x2="200"
y2="100"
stroke="rebeccapurple"
stroke-width="2" />
<circle cx="100" cy="100" r="75" stroke="none" fill="blue" />
<line
x1="100"
y1="25"
x2="100"
y2="175"
stroke="rebeccapurple"
stroke-width="1.5" />
<line
x1="25"
y1="100"
x2="175"
y2="100"
stroke="rebeccapurple"
stroke-width="1.5" />
<circle cx="100" cy="100" r="50" stroke="none" fill="red" />
<line
x1="100"
y1="50"
x2="100"
y2="150"
stroke="rebeccapurple"
stroke-width="1" />
<line
x1="50"
y1="100"
x2="150"
y2="100"
stroke="rebeccapurple"
stroke-width="1" />
<circle cx="100" cy="100" r="25" stroke="none" fill="yellow" />
<line
x1="100"
y1="75"
x2="100"
y2="125"
stroke="rebeccapurple"
stroke-width="0.5" />
<line
x1="75"
y1="100"
x2="125"
y2="100"
stroke="rebeccapurple"
stroke-width="0.5" />
</svg>
<figcaption>
Figure 2. SVG reference image. The images following this should look
exactly the same as this.
</figcaption>
</figure>
</div>
<h4>Transformation with transform-origin</h4>
<div>
<figure>
<svg
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
viewBox="0 0 200 200">
<defs>
<g id="target-g-1">
<circle cx="100" cy="100" r="100" stroke="none" />
<line
x1="100"
y1="0"
x2="100"
y2="200"
stroke="rebeccapurple"
stroke-width="2" />
<line
x1="0"
y1="100"
x2="200"
y2="100"
stroke="rebeccapurple"
stroke-width="2" />
</g>
</defs>
<use href="#target-g-1" fill="black" />
<use
href="#target-g-1"
fill="blue"
transform="scale(0.75 0.75)"
transform-origin="100 100" />
<svg
xmlns="http://www.w3.org/2000/svg"
x="0"
y="0"
width="200"
height="200"
viewBox="0 0 200 200">
<use
href="#target-g-1"
fill="red"
transform="scale(0.5 0.5)"
transform-origin="100 100" />
<use
href="#target-g-1"
fill="yellow"
transform="scale(0.25 0.25)"
transform-origin="100 100" />
</svg>
</svg>
<figcaption>
Figure 3. transform-origin used. This image should look exactly the same
as the reference image in Figure 2.
</figcaption>
</figure>
</div>
<h4>Transformation without transform-origin</h4>
<div>
<figure>
<svg
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
viewBox="0 0 200 200">
<defs>
<g id="target-g-1">
<circle cx="100" cy="100" r="100" stroke="none" />
<line
x1="100"
y1="0"
x2="100"
y2="200"
stroke="rebeccapurple"
stroke-width="2" />
<line
x1="0"
y1="100"
x2="200"
y2="100"
stroke="rebeccapurple"
stroke-width="2" />
</g>
</defs>
<use href="#target-g-1" fill="black" />
<use
href="#target-g-1"
fill="blue"
transform="translate(100 100) scale(0.75 0.75) translate(-100 -100)" />
<svg
xmlns="http://www.w3.org/2000/svg"
x="0"
y="0"
width="200"
height="200"
viewBox="0 0 200 200">
<use
href="#target-g-1"
fill="red"
transform="translate(100 100) scale(0.5 0.5) translate(-100 -100)" />
<use
href="#target-g-1"
fill="yellow"
transform="translate(100 100) scale(0.25 0.25) translate(-100 -100)" />
</svg>
</svg>
<figcaption>
Figure 4. transform-origin not used. This image should look exactly the
same as the reference image in Figure 2.
</figcaption>
</figure>
</div>
```
### CSS
```css
h4 {
font-family: sans-serif;
}
figure {
border: thin #c0c0c0 solid;
display: inline-flex;
flex-flow: column;
padding: 5px;
max-width: 200px;
margin: auto;
}
figcaption {
margin-top: 5px;
background-color: #222;
color: #fff;
font: smaller sans-serif;
padding: 3px;
text-align: center;
}
```
### Result
{{ EmbedLiveSample('Example', 700, 1350) }}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}