1+ <#
2+ . SYNOPSIS
3+ Gets a Turtle Pattern
4+ . DESCRIPTION
5+ Gets the current turtle as a pattern that stretches off to infinity.
6+ . EXAMPLE
7+ turtle star 42 4 | Save-Turtle "./GridPattern.svg"
8+ . EXAMPLE
9+ turtle star 42 6 | Save-Turtle "./StarPattern6.svg"
10+ . EXAMPLE
11+ turtle star 42 8 | Save-Turtle "./StarPattern8.svg"
12+ . EXAMPLE
13+ turtle star 42 5 | Save-Turtle "./StarPattern5.svg"
14+ . EXAMPLE
15+ turtle star 42 7 | Save-Turtle "./Star7Pattern.svg"
16+ . EXAMPLE
17+ turtle viewbox 100 start 25 25 square 50 | Save-Turtle "./WindowPattern.svg" Pattern
18+ . EXAMPLE
19+ turtle star 100 4 morph @(
20+ turtle star 100 4
21+ turtle rotate 90 star 100 4
22+ turtle rotate 180 star 100 4
23+ turtle rotate 270 star 100 4
24+ turtle star 100 4
25+ ) | Save-Turtle "./GridPatternMorph.svg"
26+ . EXAMPLE
27+ turtle star 100 3 morph @(
28+ turtle star 100 3
29+ turtle rotate 90 star 100 3
30+ turtle rotate 180 star 100 3
31+ turtle rotate 270 star 100 3
32+ turtle star 100 3
33+ ) | Save-Turtle "./TriPatternMorph.svg"
34+ . EXAMPLE
35+ turtle star 100 6 morph @(
36+ turtle star 100 6
37+ turtle rotate 90 star 100 6
38+ turtle rotate 180 star 100 6
39+ turtle rotate 270 star 100 6
40+ turtle star 100 6
41+ ) | Save-Turtle "./Star6PatternMorph.svg" | Show-Turtle
42+ . EXAMPLE
43+ # We can use a pattern transform to scale the pattern
44+ turtle sierpinskiTriangle PatternTransform @{
45+ scale = 0.25
46+ rotate = 120
47+ } |
48+ Save-Turtle "./SierpinskiTrianglePattern.svg" Pattern |
49+ show-Turtle
50+ . EXAMPLE
51+ # We can use pattern animations to change the pattern
52+ # Animations are relative to initial transforms
53+ turtle sierpinskiTriangle PatternTransform @{
54+ scale = 0.25
55+ rotate = 120
56+ } PatternAnimation ([Ordered]@{
57+ type = 'scale' ; values = 1.33,0.66, 1.33 ; repeatCount = 'indefinite' ;dur = "23s"; additive = 'sum'
58+ }) |
59+ Save-Turtle "./SierpinskiTrianglePattern.svg" Pattern |
60+ Show-Turtle
61+ #>
62+ [OutputType ([xml ])]
163param ()
2- $segments = @ (
64+
65+ # Get our viewbox
366$viewBox = $this.ViewBox
67+ # and get the width and height
468$null , $null , $viewX , $viewY = $viewBox
69+
70+ # Initialize our core attributes.
71+ # These may be overwritten by user request.
72+ $coreAttributes = [Ordered ]@ {
73+ ' id' = " $ ( $this.ID ) -pattern"
74+ ' patternUnits' = ' userSpaceOnUse'
75+ ' width' = $ViewX
76+ ' height' = $viewY
77+ ' transform-origin' = ' 50% 50%'
78+ }
79+
80+ # If we have specified any transforms
81+ if ($this.PatternTransform ) {
82+ $coreAttributes ." patternTransform" =
83+ # Then generate a transform expression
84+ @ (foreach ($key in $this.PatternTransform.Keys ) {
85+ # transforms are a name, followed by parameters in paranthesis
86+ " $key ($ ( $this.PatternTransform [$key ]) )"
87+ }) -join ' '
88+ }
89+
90+ # Pattern attributes can be defined within .SVGAttribute or .Attribute
91+ # provided they have the appropriate prefix
92+ $prefix = [Regex ]::new(' ^/?pattern/' , ' IgnoreCase' )
93+ # (slashes are invalid markup, and thus a fine way to target nested instances)
94+
95+ foreach ($collection in $this.SVGAttribute , $this.Attribute ) {
96+ # If the connection does not exist, continue.
97+ if (-not $collection ) { continue }
98+ # For each key that matches the prefix
99+ foreach ($key in $collection.Keys -match $prefix ) {
100+ # add it to the attributes after stripping the prefix.
101+ $coreAttributes [$attributeName -replace $prefix ] = $collection [$attributeName ]
102+ }
103+ }
104+
105+ $segments = @ (
5106" <svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%'>"
6107" <defs>"
7- " <pattern id='$ ( $this.ID ) -pattern' patternUnits='userSpaceOnUse' width='$viewX ' height='$viewY ' transform-origin='50% 50%'$ (
8- if ($this.PatternTransform ) {
9- " patternTransform='" + (
10- @ (foreach ($key in $this.PatternTransform.Keys ) {
11- " $key ($ ( $this.PatternTransform [$key ]) )"
12- }) -join ' '
13- ) + " '"
14- }
15- ) >"
108+ " <pattern$ (
109+ foreach ($attributeName in $coreAttributes.Keys ) {
110+ " $attributeName ='$ ( $coreAttributes [$attributeName ]) '"
111+ }
112+ ) >"
16113 $ (if ($this.PatternAnimation ) { $this.PatternAnimation })
17114 $ ($this.SVG.SVG.InnerXML )
18115 " </pattern>"
19116" </defs>"
20117" <rect width='10000%' height='10000%' x='-5000%' y='-5000%' fill='url(#$ ( $this.ID ) -pattern)' transform-origin='50% 50%' />"
21- " </svg>" )
118+ " </svg>"
119+ )
22120
23- $segments -join ' ' -as [xml ]
121+ [xml ]$segments
0 commit comments