add ludus-extraction function from svg
This commit is contained in:
parent
7285e599c5
commit
e6b2ffe4c9
|
@ -2,25 +2,29 @@ import init from "./out.mjs"
|
|||
|
||||
const mod = await init()
|
||||
|
||||
let result = null
|
||||
let res = null
|
||||
|
||||
let code = null
|
||||
|
||||
export function run (source) {
|
||||
code = source
|
||||
const output = mod.ludus(source).value
|
||||
result = JSON.parse(output)
|
||||
return result
|
||||
res = JSON.parse(output)
|
||||
return res
|
||||
}
|
||||
|
||||
export function stdout () {
|
||||
if (!result) return ""
|
||||
return result.io.console.data
|
||||
if (!res) return ""
|
||||
return res.io.console.data
|
||||
}
|
||||
|
||||
export function turtle_commands () {
|
||||
if (!result) return []
|
||||
return result.io.turtle.data
|
||||
if (!res) return []
|
||||
return res.io.turtle.data
|
||||
}
|
||||
|
||||
export function result () {
|
||||
return res
|
||||
}
|
||||
|
||||
const turtle_init = {
|
||||
|
@ -55,6 +59,7 @@ function resolve_color (color) {
|
|||
if (typeof color === 'string') return colors[color]
|
||||
if (typeof color === 'number') return [color, color, color, 255]
|
||||
if (Array.isArray(color)) return color
|
||||
return [0, 0, 0, 255] // default to black?
|
||||
}
|
||||
|
||||
let background_color = "black"
|
||||
|
@ -204,7 +209,7 @@ function turn_to_rad (heading) {
|
|||
}
|
||||
|
||||
function turn_to_deg (heading) {
|
||||
return ((heading % 1) * 360)
|
||||
return (heading % 1) * 360
|
||||
}
|
||||
|
||||
function svg_render_line (prev, curr) {
|
||||
|
@ -227,6 +232,16 @@ function escape_svg (svg) {
|
|||
.replace(/'/g, "'")
|
||||
}
|
||||
|
||||
export function extract_ludus (svg) {
|
||||
const code = svg.split("<ludus>")[1]?.split("</ludus>")[0] ?? ""
|
||||
return code
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, `"`)
|
||||
.replace(/'/g, `'`)
|
||||
}
|
||||
|
||||
function svg_render_path (states) {
|
||||
const path = []
|
||||
for (let i = 1; i < states.length; ++i) {
|
||||
|
@ -277,8 +292,9 @@ export function svg (commands) {
|
|||
const view_height = (maxY - minY) * 1.2
|
||||
const margin = Math.max(view_width, view_height) * 0.1
|
||||
const x1 = minX - margin
|
||||
const y1 = minY - margin
|
||||
const x2 = maxX + margin
|
||||
// don't actually need these:
|
||||
// const y1 = minY - margin
|
||||
// const x2 = maxX + margin
|
||||
const y2 = maxY + margin
|
||||
const path = svg_render_path(states)
|
||||
const turtle = svg_render_turtle(states[states.length - 1])
|
||||
|
|
Loading…
Reference in New Issue
Block a user