Absolutely! Here’s a single HTML file that:
- Shows the ASCII art in a
<pre> block.
- Has a button: “Convert to SVG”.
- When clicked, parses the ASCII art and draws a nice SVG version with lines and circles, matching the ASCII layout.
How it works:
- Each ASCII character is mapped to a shape (e.g.,
+, -, | → lines; /, \ → diagonals; (, ) → curves; o or O → circles).
- The code scans the ASCII art, and for each character, draws the corresponding SVG element at the right position.
- The SVG is shown below the ASCII art.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Edifier R1280 – ASCII to SVG</title>
<style>
html,body{
height:100%;
margin:0;
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
background:#111;
color:#eee;
font-family: "Courier New", Consolas, monospace;
}
pre{
line-height:1.05em;
font-size:14px;
margin-bottom: 1em;
}
#svg-container {
margin-top: 1em;
background: #222;
border-radius: 8px;
box-shadow: 0 2px 8px #0008;
padding: 1em;
}
button {
font-size: 1em;
padding: 0.5em 1.5em;
border-radius: 5px;
border: none;
background: #444;
color: #fff;
cursor: pointer;
margin-bottom: 1em;
}
button:hover {
background: #666;
}
</style>
</head>
<body>
<pre id="ascii-art">
+------------------+ +------------------+
| | | |
| ________ | | |
| / () \ | | |
| | ____ | | | |
| \ / \ / | | |
| \______/ | | |
| | | |
| __________ | | |
| / () ()\ | | |
| | ____ | | | |
| \ / \ / | | |
| \______/ | | |
| | | |
+------------------+ +------------------+
(Left) (Right)
</pre>
<button id="convert-btn">Convert to SVG</button>
<div id="svg-container"></div>
<script>
function asciiToSvg(ascii, options = {}) {
// Settings
const cellSize = options.cellSize || 16;
const stroke = options.stroke || "#e0e0e0";
const strokeWidth = options.strokeWidth || 2;
const circleRadius = options.circleRadius || 5;
const widthPadding = options.widthPadding || 2;
const heightPadding = options.heightPadding || 2;
// Parse ASCII into lines
const lines = ascii.replace(/\r/g, '').split('\n');
const nRows = lines.length;
const nCols = Math.max(...lines.map(l => l.length));
// SVG elements
let svgElements = [];
// Helper: get char at (r,c)
function getChar(r, c) {
if (r < 0 || r >= nRows) return ' ';
if (c < 0 || c >= lines[r].length) return ' ';
return lines[r][c];
}
// For each cell, check what to draw
for (let r = 0; r < nRows; ++r) {
for (let c = 0; c < nCols; ++c) {
const ch = getChar(r, c);
const x = c * cellSize + widthPadding * cellSize;
const y = r * cellSize + heightPadding * cellSize;
// Corners
if (ch === '+') {
// Draw lines if neighbors exist
if (getChar(r, c+1) === '-' || getChar(r, c+1) === '_')
svgElements.push(`<line x1="${x}" y1="${y}" x2="${x+cellSize}" y2="${y}" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
if (getChar(r+1, c) === '|')
svgElements.push(`<line x1="${x}" y1="${y}" x2="${x}" y2="${y+cellSize}" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Horizontal lines
else if (ch === '-' || ch === '_') {
svgElements.push(`<line x1="${x}" y1="${y}" x2="${x+cellSize}" y2="${y}" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Vertical lines
else if (ch === '|') {
svgElements.push(`<line x1="${x}" y1="${y}" x2="${x}" y2="${y+cellSize}" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Diagonals
else if (ch === '/') {
svgElements.push(`<line x1="${x+cellSize}" y1="${y}" x2="${x}" y2="${y+cellSize}" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
else if (ch === '\\') {
svgElements.push(`<line x1="${x}" y1="${y}" x2="${x+cellSize}" y2="${y+cellSize}" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Ovals (for speakers)
else if (ch === '(' || ch === ')') {
// Draw arc (approximate with ellipse)
svgElements.push(`<ellipse cx="${x+cellSize/2}" cy="${y+cellSize/2}" rx="${cellSize*0.45}" ry="${cellSize*0.7}" fill="none" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Small circles (for "()" or "o")
else if (ch === 'o' || ch === 'O') {
svgElements.push(`<circle cx="${x+cellSize/2}" cy="${y+cellSize/2}" r="${circleRadius}" fill="none" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Speaker "()" in ASCII
else if (ch === ')') {
svgElements.push(`<ellipse cx="${x+cellSize/2}" cy="${y+cellSize/2}" rx="${cellSize*0.45}" ry="${cellSize*0.7}" fill="none" stroke="${stroke}" stroke-width="${strokeWidth}" />`);
}
// Speaker "____" as thick line
else if (ch === '_') {
svgElements.push(`<line x1="${x}" y1="${y}" x2="${x+cellSize}" y2="${y}" stroke="${stroke}" stroke-width="${strokeWidth*1.5}" />`);
}
// You can add more mappings for other chars if desired
}
}
// SVG size
const svgWidth = (nCols + 2*widthPadding) * cellSize;
const svgHeight = (nRows + 2*heightPadding) * cellSize;
// Compose SVG
return `<svg width="${svgWidth}" height="${svgHeight}" viewBox="0 0 ${svgWidth} ${svgHeight}" style="background:#222; border-radius:8px;" xmlns="http://www.w3.org/2000/svg">
${svgElements.join('\n')}
</svg>`;
}
document.getElementById('convert-btn').onclick = function() {
const ascii = document.getElementById('ascii-art').innerText;
const svg = asciiToSvg(ascii, {
cellSize: 16,
stroke: "#e0e0e0",
strokeWidth: 2,
circleRadius: 5
});
document.getElementById('svg-container').innerHTML = svg;
};
</script>
</body>
</html>
How to use:
- Open in your browser.
- Click “Convert to SVG”.
- The SVG rendering of the ASCII art will appear below, with smooth lines and curves.
You can further enhance the mapping for more ASCII characters or tweak the cell size and colors as you wish!