SVGPath

使用 SVGPath 来调节设计模式下画的 SVG 路径的 fillstrokeStartstrokeEnd 等属性,并给它添加动画。

Use SVGPath to modify and animate SVG paths targeted from Design. Set fill, strokeStart, strokeEnd and more.

在代码模式中不能创建 SVGPath ,我们一般使用 SVGLayer 并通过获取它的 elements 属性来获取 SVGPath 。

An SVGPath should not be created in code. The only way to get an SVGPath is through the elements property of SVGLayer

  1. svg = new SVGLayer
  2. svg: "<svg><path id='shape' name='Rectangle' d='M0 0 H 150 V 75 H 0 L 0 0' />"
  3. fill: "#0AF"
  4. path = svg.elements.shape

图层可以沿着一个 SVG 路径运动,下面的例子演示了一个图层沿着一个 SVG 路径的位置和角度运动。

Layers can also be animated along a path, in combination with the point and rotation property, for example.

  1. layer = new Layer
  2. size: 100
  3. layer.midPoint = path.start
  4. layer.animate
  5. point: path
  6. rotation: path
  7. # Shorthand for doing all of the above steps
  8. layer.animate
  9. path: path

path.length <number>

路径长度。(只读)

The length of the path. (Read-only)

  1. print path.length
  2. # Output: 450

path.width <number>

路径宽度。(只读)

The width of the path. (Read-only)

  1. print path.width
  2. # Output: 150

path.height <number>

路径高度。(只读) The height of the path. (Read-only)

  1. print path.height
  2. # Output: 75

path.start(relativeToLayer)

返回路径的起始位置坐标和角度。如果你传入了一个图层作为参数,那么返回的坐标就是以这个图层为参考系,否则的话以其父图层作为参考系。

Returns the location and rotation of the start of the path. Pass a layer as final argument to get the coordinates for that layer. If no layer is passed, the coordinates will be for the current context.

  1. print path.start()
  2. # {x:0, y:0, rotation:90}
  3. # Shorthand to set the location of a layer to the start of the path
  4. layer.point = path.start

path.end(relativeToLayer)

返回路径的结束位置坐标和角度。如果你传入了一个图层作为参数,那么返回的坐标就是以这个图层为参考系,否则的话以其父图层作为参考系。

Returns the location and rotation of the end of the path. Pass a layer as final argument to get the coordinates in that layer. If no layer is passed, the coordinates will be for the current context.

  1. print path.end()
  2. # {x:0, y:0, rotation:0}
  3. # Shorthand to set the location of a layer to the end of the path
  4. layer.point = path.end

path.pointAtFraction(fraction)

获取路径对应比例( 0 到 1 )长度处的坐标,该坐标是相对于这个路径的。

Get the location of a point along a fraction (value from 0 to 1) of the path, relative to the position of the path.

  1. point = path.pointAtFraction(0.5)
  2. print point.x, point.y
  3. # Output: 150, 75

path.rotationAtFraction(fraction, delta)

路径线条对应比例( 0 到 1 )处的角度,另一个可选参数是计算角度的偏差。

Get the angle of the line at a fraction (value from 0 to 1) of the path. The optional second argument is the offset used to calculate the angle.

  1. print path.rotationAtFraction(0.5)
  2. # Output: -90

path.fill <string>

设置路径填充色。

Sets the fill color of the path.

  1. path.fill = "#FFF"

path.stroke <string>

设置路径的描边颜色。

Sets the stroke color of the path.

  1. path.stroke = "#0AF"

path.strokeWidth <number>

设置路径的描边宽度。

Sets the stroke width of the path.

  1. path.strokeWidth = 10

path.strokeStart <number>

设置路径的描边起始点。

Set the start position of where to draw the stroke on the path.

  1. # Only stroke the last half of the path
  2. path.strokeStart = path.length / 2

path.strokeEnd <number>

设置路径的描边结束点。

Set the end position of where to stop drawing the stroke on the path.

  1. # Only stroke the first half of the path
  2. path.strokeEnd = path.length / 2

path.strokeLength <number>

设置路径的描边长度。

Set the length of the stroke of the path.

  1. path.strokeLength = 200

path.strokeFraction <number>

设置路径的描边长度比例,是 strokeLength 的一种简写。

Set the stroke length to a fraction of the total length of the path. Shorthand for strokeLength.

  1. # Set the stroke length to half of the total length
  2. # Same as path.strokeLength = path.length / 2
  3. path.strokeFraction = 0.5

path.strokeLinecap <string>

设置描边的两端样式。

Set the line-cap of the stroke.

你可以选择 buttroundsquare 中的一个。

You can choose between butt, round and square.

  1. # Multiple types of line caps
  2. shape.strokeLinecap = "butt"
  3. shape.strokeLinecap = "round"
  4. shape.strokeLinecap = "square"

path.strokeLinejoin <string>

设置描边的拐角样式。

Set the line-join of the stroke.

你可以选择 miterroundbevel 中的一个。

You can choose between miter, round and bevel.

  1. # Multiple types of line joins
  2. shape.strokeLinejoin = "miter"
  3. shape.strokeLinejoin = "round"
  4. shape.strokeLinejoin = "bevel"

path.strokeMiterlimit <number>

设置斜切限制,只在 strokeLinejoinmiter 时起作用。

Set the miter limit of the stroke. Only works when strokeLinejoin is set to miter.

  1. star.strokeMiterlimit = 1

path.strokeOpacity <number>

设置描边不透明度,它的值是从 0 到 1 ,0 表示不可见,1 表示完全可见。

Sets the stroke opacity. Opacity is defined with a number between 0 and 1, where 0 is invisible and 1 fully opaque.

  1. path.opacity = 0.5

path.strokeDasharray <array>

给描边设置更加精细的虚线图案,可以看MDN 的示例了解更多细节。

Sets the stroke to have a certain pattern. See MDN’s examples for more details.

  1. path.strokeDasharray = [5, 5, 10, 10]

path.strokeDashoffset <number>

设置虚线间隔宽度,当你想做虚线动画时很有用。

The offset of the dash-pattern set by strokeDasharray. Useful for animating a dashed pattern.

  1. path.stroke = "#000"
  2. path.strokeDasharray = [10, 10]
  3. path.animate
  4. strokeDashoffset: path.length