Skip to content

Commit 3e88d04

Browse files
feat: Turtle.PathAnimation XML ( Fixes #374 )
1 parent 5ca259f commit 3e88d04

File tree

4 files changed

+58
-28
lines changed

4 files changed

+58
-28
lines changed

Types/Turtle/Morph.ps1

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@
1212
.EXAMPLE
1313
$sierpinskiTriangle = turtle SierpinskiTriangle 42 4
1414
$SierpinskiTriangleFlipped = turtle rotate 180 SierpinskiTriangle 42 4
15-
turtle SierpinskiTriangle 42 4 morph (
15+
$sf = turtle SierpinskiTriangle 42 4 morph (
1616
$SierpinskiTriangle,
1717
$SierpinskiTriangleFlipped,
1818
$sierpinskiTriangle
19-
) save ./SierpinskiTriangleFlip.svg
19+
)
20+
$sf | Save-Turtle ./SierpinskiTriangleFlip.svg
21+
.EXAMPLE
22+
$sierpinskiTriangle = turtle SierpinskiTriangle 42 4
23+
$SierpinskiTriangle90 = turtle rotate 90 SierpinskiTriangle 42 4
24+
$SierpinskiTriangle180 = turtle rotate 180 SierpinskiTriangle 42 4
25+
$SierpinskiTriangle270 = turtle rotate 270 SierpinskiTriangle 42 4
26+
$sf = turtle SierpinskiTriangle 42 4 morph @(
27+
$SierpinskiTriangle,
28+
$SierpinskiTriangle90
29+
) morph @(
30+
$SierpinskiTriangle180
31+
$SierpinskiTriangle270
32+
$sierpinskiTriangle
33+
)
34+
$sf | save-turtle ./SierpinskiRoll.svg
2035
.EXAMPLE
2136
$sideCount = (3..24 | Get-Random )
2237
$stepCount = 36
@@ -53,12 +68,16 @@ $Arguments
5368
$durationArgument = $null
5469
$hasPoints = $false
5570
$segmentCount = 0
71+
$reflect = $false
5672
$newPaths = @(foreach ($arg in $Arguments) {
5773
if ($arg -is [string]) {
5874
if ($arg -match '^\s{0,}m') {
5975
$arg
6076
$hasPoints = $true
6177
}
78+
elseif ($arg -match 'reflect') {
79+
$reflect = $true
80+
}
6281
} elseif ($arg.PathData) {
6382
$arg.PathData
6483
$hasPoints = $true
@@ -73,7 +92,7 @@ $newPaths = @(foreach ($arg in $Arguments) {
7392
$segmentCount = [Math]::Abs($arg)
7493
} else {
7594
$durationArgument = [TimeSpan]::FromSeconds($arg)
76-
}
95+
}
7796
}
7897
})
7998

@@ -97,19 +116,28 @@ if (-not $newPaths) {
97116
}
98117
}
99118

100-
if ($this.PathAnimation) {
119+
# Check to see if we have existing animations on the path
120+
$pathAnimation = $this.PathAnimation
121+
$updatedAnimations = @()
122+
# If we do
123+
if ($pathAnimation) {
124+
# see if any of them are updated
101125
$updatedAnimations =
102-
@(foreach ($animationXML in $this.PathAnimation -split '(?<=/>)') {
103-
$animationXML = $animationXML -as [xml]
126+
@(foreach ($animationXML in $pathAnimation) {
104127
if (-not $animationXML) { continue }
105-
if ($animationXML.animate.attributeName -eq 'd') {
106-
$animationXML.animate.values = "$($newPaths -join ';')"
128+
# we only want to change morphs
129+
# (which are animations of the path data)
130+
if ($animationXML.animate.attributeName -eq 'd') {
131+
$animationXML.animate.values = "$(
132+
@($animationXML.animate.values; $newPaths) -join ';'
133+
)"
107134
}
108-
$animationXML.OuterXml
109-
})
110-
$this.PathAnimation = $updatedAnimations
111-
} else {
112-
$this.PathAnimation += [Ordered]@{
135+
$animationXML
136+
})
137+
}
138+
139+
if (-not $updatedAnimations) {
140+
$this.PathAnimation = [Ordered]@{
113141
attributeName = 'd' ; values = "$($newPaths -join ';')" ; repeatCount = 'indefinite'; dur = $(
114142
if ($durationArgument) {
115143
"$($durationArgument.TotalSeconds)s"
@@ -118,9 +146,8 @@ if ($this.PathAnimation) {
118146
} else {
119147
"4.2s"
120148
}
121-
122149
)
123-
}
150+
}
124151
}
125152

126153
return $this

Types/Turtle/get_PathElement.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ foreach ($collection in $this.SVGAttribute, $this.Attribute) {
4646
}
4747
)>"
4848
if ($this.Title) { "<title>$([Security.SecurityElement]::Escape($this.Title))</title>" }
49-
if ($this.PathAnimation) {$this.PathAnimation}
49+
if ($this.PathAnimation) {$this.PathAnimation.OuterXml}
5050
"</path>"
5151
)

Types/Turtle/set_Duration.ps1

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ foreach ($v in $value) {
1919
}
2020
}
2121

22-
if (($this.'.Duration' -is [TimeSpan]) -and $this.PathAnimation) {
23-
$updatedAnimations =
24-
@(foreach ($animationXML in $this.PathAnimation -split '(?<=/>)') {
25-
$animationXML = $animationXML -as [xml]
26-
if (-not $animationXML) { continue }
27-
if ($animationXML.animate.attributeName -eq 'd') {
28-
$animationXML.animate.dur = "$(($this.'.Duration').TotalSeconds)s"
29-
}
30-
$animationXML.OuterXml
31-
})
32-
$this.PathAnimation = $updatedAnimations
22+
if (($this.'.Duration' -is [TimeSpan]) -and $this.PathAnimation) {
23+
foreach ($animationXML in $this.PathAnimation) {
24+
$animationXML = $animationXML -as [xml]
25+
if (-not $animationXML) { continue }
26+
if ($animationXML.animate.attributeName -eq 'd') {
27+
$animationXML.animate.dur = "$(($this.'.Duration').TotalSeconds)s"
28+
}
29+
}
3330
}

Types/Turtle/set_PathAnimation.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ $newAnimation = @(foreach ($animation in $PathAnimation) {
5656
}
5757
})
5858

59+
$animationXml = $newAnimation -as [xml]
60+
61+
if (-not $animationXml) { return }
62+
5963
$pathAnimation = $this.PathAnimation
6064
if ($pathAnimation) {
61-
$newAnimation = @($pathAnimation) + $newAnimation
65+
$newAnimation = @($pathAnimation) + $animationXml
66+
} else {
67+
$newAnimation = @($animationXml)
6268
}
6369
$this | Add-Member -MemberType NoteProperty -Force -Name '.PathAnimation' -Value $newAnimation
6470

0 commit comments

Comments
 (0)