SVG坐标系统学习笔记

文章是阅读下面三篇关于SVG坐标和转化的文章的学习笔记, 重点学习了第一部分.canvas


part1: The viewport, viewBox, and preserveAspectRatio

SVG画布(svg canvas)

理论上讲,SVG画布在X轴,Y轴上能够无限的延伸,也就是说能够画出任意大小的SVG图形。可是在浏览器中只能渲染(render)到有限的视口(viewport)中,超过视口边界的部分会被减去(clipped off)从而不能在浏览器中看到。浏览器

viewport

viewport规定了SVG图形能够看到的区域,于此同时画布的大小(size of canvas)以及preserveAspectRatio决定了用户经过viewport看到的是整幅画布,仍是部分画布。app

经过设置SVG的widthheight属性能够设定viewport,也就是用户能够看到的画布的宽度和高度。当设定了宽度和高度后,浏览器也就创建了初始的视口坐标系统(initial viewport coordinate system)和用户坐标系统(initial user coordinate system)。ide

初始坐标系统

初始的坐标系统中包含两套坐标系统:svg

  • 初始的视口坐标系统: 创建在viewport基础上(a coordinate system established on the viewport),原点在左上方的(0,0)坐标。x值在x轴上越向右越大,y值在y轴上越向下越大。
  • 初始的用户坐标系统:创建在SVG画布基础上(the coordinate system established on the SVG canvas),初始的用户坐标系统和初始的视口坐标系统相同(This coordinate system is initially identical to the viewport coordinate system)。

能够经过使用viewBox属性来使得初始的用户坐标系统和初始的视口坐标系统不相同,使用viewBox设置的坐标系统被称做“当前坐标系统”或者“正在使用的用户空间”。此外,还可使用transform属性来建立新的“用户空间”或者是“坐标系统”。学习

viewBox

关于viewBox有如下几点须要注意:this

  • 能够将viewBox改变的坐标系统认为是“实际的坐标系统(real coordinate system)”,由于用户正是经过使用这套坐标系统将SVG图形绘制到SVG画布上的。
  • 若是没有指定viewBox属性的话,那么默认其值为“0 0 width height”,也就是说生成的初始的用户坐标系统和视口坐标系统相同。
  • 用户能够经过使用该属性来构建本身的用户坐标系统。若是用户构建的坐标系统和视口坐标系统的纵横比(aspect ratio)不同的话,能够经过使用preserveAspectRatio属性来指定是否将整个系统显示在视口中,或者是指定图形在视口的位置。
  • 使用viewBox须要指定四个值<min-x> <min-y> width height, <min-x><min-y>定义了用户自定义坐标系统的左上角(determine the upper left corner of the viewbox),widthheight分别指定了用户自定义坐标系统的宽度和高度。
  • 能够经过使用viewBox来缩放(scale)、移动(translate)SVG图案。甚至能够经过viewBox来剪切(crop)SVG图案。

下面是关于viewBox的实例:spa

  • 自定义viewBox,和viewport具备相同的纵横比
<svg width="800" height="600" viewbox="0 0 400 300">
    <!-- SVG content drawn onto the SVG canvas -->
</svg>

生成该SVG图形的过程是这样子的:翻译

  1. 截取从(0,0)到(400,300)的一块区域。
  2. 被截取的区域放大(scale up)来铺满整个视口
  3. 用户坐标系统为从(0,0)到(400,300),而视口坐标系统为从(0,0)到(800,600)。于是,用户坐标系统中的一个单元至关于视口坐标系统中两个单元。

值得说的是咱们这里<min-x><min-y>使用了0,0,若是非0,0的话至关于在0,0的基础之上进行了translate(-<min-x>, -<min-y>)处理,能够从下图中体会。 viewBox="100 100 200 150" 100,100,200,150 viewBox="-100 -100 400 300" -100,-100,400,3003d

上面两个例子都是viewBox的width和height都小于viewport的width和height并保持纵横比,来看一个width和height都大于viewport的width和height并保持纵横比的例子。 viewbox="0 0 1200 900" 0,0,1200,900

关于viewBox的总结

On one hand, unlike the transform attribute, the automatic transformation that is created due to a viewBox does not affect the x, y, width and height attributes on the element with the viewBox attribute. Thus, in the example above which shows an svg element which has attributes width, height and viewBox, the width and height attributes represent values in the coordinate system that exists before the viewBox transformation is applied. You can see this in the above examples as the initial (grey) viewport coordinate system remains unaffected even after using the viewBox attribute on the <svg>. 翻译---->: 一方面,viewBox效果不一样于transform的效果,这是因为viewBox引发的自动转换不会影响x,y,width,height。于是,在上面例子中widthheight属性表示在使用viewBox属性引发的转换效果以前的坐标系统。直接结果就是不管viewBox如何变化初始的视口坐标系统使用保持不变。

On the other hand, like the transform attribute, it does establish a new coordinate system for all other attributes and for descendant elements. You can also see that in the above examples as the user coordinate system established is a new one—it does not remain as the initial user coordinate system which was identical to the viewport coordinate system before the viewBox was used. And any descendants of the <svg> will be positioned and sized in the new user coordinate system, not the initial one. 翻译---->: 另外一方面,与transform属性同样,使用viewBox属性会为全部的其余属性以及SVG后代节点创建一套新的坐标系统。于是,上述各例中使用了viewBox后建立的用户坐标系统和初始的视口坐标系统不一样。SVG元素的任何后代节点会根据新建立的用户坐标系统进行位置的摆放和尺寸的展现。

preserveAspectRatio

The preserveAspectRatio attribute allows you to force uniform scaling of the viewbox, while maintaining the aspect ratio, and it allows you to specify how to position the viewbox inside the viewport if you don't want it to be centered by default. 翻译---->: preserveAspectRatio属性容许用户经过保持纵横比的前提下缩放viewBox的坐标系统到viewport,于此同时能够经过它指定将viewBox坐标系统映射到viewport坐标系统时的对齐方式,若是不想使用默认的居中对齐的话。

preserveAspectRatio的使用语法以下: preserveAspectRatio = defer? <align> <meetOrSlice>?

defer属性只在image元素上有效。

对齐方式align有10中,分别为x,y轴从表示左侧、居中和右侧对齐的三种规格Min,Mid,Max中各自选择一种规格。如xMinyMin, xMinyMid, xMinyMax。外加一个none选项,none选项表示不保持viewBox的纵横比,直接将viewBox缩放到viewport。

meetOrSlice选项规定了是否整个viewBox构造的用户空间系统都在viewport构造的视口坐标系统中可见。默认为meet,表示保持纵横比,而且全部的viewBox系统均可以在viewport系统中可见;

part2: The transform Attribute

transform functions

The transform attribute is used to specify one or more transformations on an element. The possible SVG transformations are: rotation, scaling, translation, and skewing. Transform value is defined as a list of transform definitions, which are applied in the order provided. The individual transform definitions are separated by whitespace and/or a comma.

全部的演示都是基于一下代码为最初始状态

<svg width="800" height="600" viewBox="0 0 800 600">
    <g id="parrot">
        <!-- shapes and paths forming the parrot -->
    </g>
    <g id="dog">
        <!-- shapes and paths forming the dog -->
    </g>
</svg>

Translation

translate(<tx> [<ty>])

tx represents the translation value along the x-axis; ty represents the translation value along the y-axis.

The ty value is optional; and, if omitted, it defaults to zero.

<svg width="800" height="800" viewBox="0 0 800 600">
    <g id="parrot" transform="translate(150 200)">
        <!-- shapes and paths forming the parrot -->
    </g>
    <!-- ... -->
</svg>

首先,来看结果图

其次,新的坐标系统图

最后,总结起来就是说,新创建的坐标系统只是原来的坐标系统的一个复制,鹦鹉每一个点在新坐标系统中的坐标同原来在最初始的坐标系统中的取值并未发生变化,而且坐标系统的范围一样未发生变化都是(800,600)。变化的是两个坐标系统的原点位置,新创建的坐标原点为初始坐标系统的(150, 200)。

It's important to notice here that the new current coordinate system established on the element is a replicate of the initial user space, with the position of the element preserved inside it. This means that it is not established on the element's bounding box, nor is the size of the new current coordinate system restricted to the size of the element.

Scaling

scale(<sx> [<sy>])

sx represents the scaling value along the x-axis, used to stretch or shrink the element horizontally; sy represents the scaling value along the y-axis, used to stretch or shrink the element vertically.

The sy value is optional; and, if omitted, it is assumed to be equal to sx.

It is important to note here that when an SVG element is scaled, its entire current coordinate system is scaled, resulting in the element also being repositioned inside the viewport.

<svg width="800" height="800" viewBox="0 0 800 600">
    <g id="parrot" transform="scale(2)">
        <!-- shapes and paths forming the parrot -->
    </g>
    <!-- ... -->
</svg>

首先,看最后效果图

其次,新的坐标系统图

最后,总结起来,鹦鹉每一个点在新坐标系统中的坐标同原来在最初始的坐标系统中的取值一样并未发生任何变化,而且两个坐标系统的原点在同一位置,变化的是如今的坐标系统范围是(400,300)而原来的是(800,600),新的坐标系统中单位长度在原始坐标系统中表示两个单位长度。

Skew

skewX(<skew-angle>)
skewY(<skew-angle>)

The skewX function specifies a skew transformation along the x-axis; and the skewY function specifies a skew transformation along the y-axis.

Rotation

rotate(<rotate-angle> [<cx> <cy>])

Nested and Chained Transformations

When transformations are chained, the most important thing to be aware of is that, just like with HTML element transformations, each transformation is applied to the coordinate system after that system is transformed by the previous transformations.

part3: Establishing New Viewports

未学习...

相关文章
相关标签/搜索