-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15771a0
commit d502c7f
Showing
6 changed files
with
96 additions
and
56 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function visualize(polylines, width, height, scale, stroke) { | ||
let svg = `<svg version="1.1" xmlns="http:https://www.w3.org/2000/svg" width="${width * scale}" height="${height * scale}">` | ||
for (let i = 0; i < polylines.length; i++) { | ||
svg += `<path fill="none" stroke-width="${stroke}" stroke="rgb(${Math.floor(Math.random() * 200)},${Math.floor( | ||
Math.random() * 200 | ||
)},${Math.floor(Math.random() * 200)})" d="M${polylines[i] | ||
.map((x) => x[0] * scale + "," + x[1] * scale) | ||
.join(" L")}"/>` | ||
} | ||
svg += "</svg>" | ||
return svg | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
export async function pathToImageData(imagePath) { | ||
return new Promise((res, rej) => { | ||
const canvas = document.createElement("canvas") | ||
try { | ||
const ctx = canvas.getContext("2d", { willReadFrequently: true }) | ||
document.createElement("canvas").getContext("2d") | ||
const img = new Image() | ||
img.onload = () => { | ||
canvas.width = img.width | ||
canvas.height = img.height | ||
ctx.drawImage(img, 0, 0) | ||
const imageData = ctx.getImageData(0, 0, img.width, img.height) | ||
res({ img, imageData }) | ||
} | ||
img.src = imagePath | ||
} catch (e) { | ||
rej(JSON.stringify(e)) | ||
} finally { | ||
canvas.remove() | ||
} | ||
}) | ||
} | ||
|
||
export function i18n(param) { | ||
if (param && typeof param === "object") { | ||
const current = document.documentElement.lang | ||
const exist = Object.keys(param) | ||
return exist.includes(current) ? param[current] : exist.includes("en") ? param["en"] : param[exist[0]] | ||
} else return "" | ||
} | ||
const html = String.raw | ||
|
||
export function nodeFabric(actor) { | ||
const template = document.createElement("template") | ||
template.innerHTML = html` | ||
<h1>${i18n(actor.name)}</h1> | ||
<form> | ||
${Object.entries(actor.input) | ||
.map(([key, socket]) => { | ||
switch (socket.type) { | ||
case "Boolean": | ||
return html` | ||
<div> | ||
<input type="checkbox" id="${key}" name="${key}" ${socket.value ? "checked" : ""} /> | ||
<label for="${key}">${i18n(socket.name)}</label> | ||
</div> | ||
` | ||
case "String": | ||
return html` | ||
<div> | ||
<label for="${key}">${i18n(socket.name)}</label> | ||
<textarea cols="80" rows="4" id="${key}" name="${key}"> ${socket.value} </textarea> | ||
</div> | ||
` | ||
default: | ||
return "" | ||
} | ||
}) | ||
.join("")} | ||
</form> | ||
` | ||
template.content.querySelector("form").addEventListener("change", (e) => { | ||
e.preventDefault() | ||
const formData = new FormData(e.currentTarget) | ||
const props = {} | ||
for (const key in actor.input) { | ||
const value = formData.get(key) | ||
if (actor.input[key].type === "Boolean") { | ||
props[key] = value === "on" | ||
} else props[key] = value | ||
} | ||
actor.send(props) | ||
}) | ||
template.content.appendChild(actor) | ||
return template.content | ||
} |