Skip to content

Commit 133dec2

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.get_PNG ( Fixes #59 )
1 parent 391b350 commit 133dec2

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Turtle.types.ps1xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ window.onload = async function() {
761761
var p = new Path2D("$($this.PathData)")
762762
ctx.stroke(p)
763763
ctx.fill(p)
764+
765+
/*Insert-Post-Processing-Here*/
764766
}
765767
"@
766768
"</script>"
@@ -1038,6 +1040,57 @@ $PatternTransform
10381040
$this | Add-Member -MemberType NoteProperty -Force -Name '.PatternTransform' -Value $PatternTransform
10391041
</SetScriptBlock>
10401042
</ScriptProperty>
1043+
<ScriptProperty>
1044+
<Name>PNG</Name>
1045+
<GetScriptBlock>
1046+
$chromiumNames = 'chromium','chrome','msedge'
1047+
foreach ($browserName in $chromiumNames) {
1048+
$chromiumCommand =
1049+
$ExecutionContext.SessionState.InvokeCommand.GetCommand($browserName,'Application')
1050+
if (-not $chromiumCommand) {
1051+
$chromiumCommand =
1052+
Get-Process -Name $browserName -ErrorAction Ignore |
1053+
Select-Object -First 1 -ExpandProperty Path
1054+
}
1055+
if ($chromiumCommand) { break }
1056+
}
1057+
if (-not $chromiumCommand) {
1058+
Write-Error "No Chromium-based browser found. Please install one of: $($chromiumNames -join ', ')"
1059+
return
1060+
}
1061+
1062+
$pngRasterizer = $this.Canvas -replace '/\*Insert-Post-Processing-Here\*/', @'
1063+
const dataUrl = await canvas.toDataURL('image/png')
1064+
console.log(dataUrl)
1065+
1066+
const newImage = document.createElement('img')
1067+
newImage.src = dataUrl
1068+
document.body.appendChild(newImage)
1069+
'@
1070+
1071+
1072+
$appDataRoot = [Environment]::GetFolderPath("ApplicationData")
1073+
$appDataPath = Join-Path $appDataRoot 'Turtle'
1074+
$filePath = Join-Path $appDataPath 'Turtle.raster.html'
1075+
$null = New-Item -ItemType File -Force -Path $filePath -Value (
1076+
$pngRasterizer -join [Environment]::NewLine
1077+
)
1078+
# $pngRasterizer &gt; $filePath
1079+
1080+
$headlessArguments = @(
1081+
'--headless', # run in headless mode
1082+
'--dump-dom', # dump the DOM to stdout
1083+
'--disable-gpu', # disable GPU acceleration
1084+
'--no-sandbox' # disable the sandbox if running in CI/CD
1085+
)
1086+
1087+
$chromeOutput = &amp; $chromiumCommand @headlessArguments "$filePath" | Out-String
1088+
if ($chromeOutput -match '&lt;img\ssrc="data:image/png;base64,(?&lt;b64&gt;[^"]+)') {
1089+
,[Convert]::FromBase64String($matches.b64)
1090+
}
1091+
1092+
</GetScriptBlock>
1093+
</ScriptProperty>
10411094
<ScriptProperty>
10421095
<Name>Points</Name>
10431096
<GetScriptBlock>

0 commit comments

Comments
 (0)