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