mirror of
https://github.com/arfct/itty-bitty.git
synced 2026-03-11 08:54:33 +00:00
150 lines
No EOL
6.6 KiB
JavaScript
150 lines
No EOL
6.6 KiB
JavaScript
let script = document.currentScript?.src
|
|
|
|
var cssURL = "/render/axiom.css";
|
|
Promise.all([loadSyle(cssURL)]).then(render);
|
|
|
|
let colors = ["#192F30", "#2A4546", "#2E4A4B", "#264A4B", "#274E50", "#225A5D", "#1B666A", "#207479", "#27898F", "#2E9EA1", "#37A7AA", "#3FB6BA", "#39C9CE", "#3CD0D5", "#40D7DC", "#46DCE1", "#5CE6EB", "#ACF6F4"]
|
|
let chars = `AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz.,:;!?&#/\\∞'"0123456789+-*()[]^\`█▟▙▜▛▀▄▐▌▝▘▗▖─═║╔╗╚╝╠╣╦╩╬><▲▼☺☻⚉ ™ % ♦♣♠♥`
|
|
var img = new Image;
|
|
img.src = "data:image/gif;base64,R0lGODlhegEtAIAAAP///////yH5BAUAAAEALAAAAAB6AS0AAAL/jI+py+0Po5y02ouz3rz7D4biSJbmiabqyrbuC8fyzAD2/dw2std6rgP4DMGeo5j4HYJAntC4gE6kSeHyaVVQiVmu89jdVsVOJkQJNqdxa7QUSg7Au9psT2yM5+leOb8ZxbcH9qX3Z1h1peg3FDjG9XdlF7kYx7jYcEfZx+mISZj49VknqjZ4GRp42tlHxjY66qo0h+pZgYiKF/n2s4PDNovlNyk6XMzoG6YsqVkqzMosSGe6fEobao1F7NlcmQztdU167Nz5/QpbO475zb69Hh7+JI+s3bpc/5n8XNvdff9syypovAptK0JFkxt/hxoahARJ3B5erwQeRGhMnUVQ/+4Wmts1rdq8fMP4/SN5CWMuK7SyUVMFbFPCkA/vOQtWM6VDeO10wqwIMhGTbDkLwju6SqFRpCLl0INIVFHUVCd7/hvYSA2spSirmmTZ1ObRjyvB6Wqk7ytZrBvTUg0rLtXakT27gi16dx1Dp1N9AiI3U67VvE/9yiMc9WremG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78Mz2BUObOvrWvsiUrj4WQ5F9isO/1z4vn3syXB4CyDxbifCApKQcB884HpU6LzVJXF7DqTd3YQXPe1h7kY83JV1iggUR6LFo1QxHKABTxe5CoKRO0bskPIyV0UGMm95O8ta71IkOaOxL3RpXBuu1gac+nkuhGwLHhAEt0BynfCDZ7wfvwrJNaQ98W7lY+H88mbFKyavhs+7YRfDN7nvNHKFGytj8pbov+apkY2KjEYPw1PHA/ERYYHsoRiJaMIEmhGUqUTjKG0pSuidkmz0WeUrqVa1ViJwjJ+8pSGDRsuvwc90vJRBAQAAOw=="
|
|
|
|
function render() {
|
|
window.canvas = el("canvas", { width: 500, height: 300 });
|
|
window.ctx = canvas.getContext("2d");
|
|
window.w = window.canvas.width
|
|
window.h = window.canvas.height
|
|
|
|
document.body.appendChild(el("div.window",
|
|
el("div#titlebar", {}, "Axiom QuickServe", el("span#title")),
|
|
window.canvas
|
|
)
|
|
);
|
|
document.body.appendChild(el("script", {}, params.body))
|
|
|
|
setTimeout(() => {
|
|
document.title = getName();
|
|
|
|
var a = new FileReader();
|
|
a.onload = function(e) {
|
|
console.log("e.target.result", e.target.result)
|
|
document.getElementById("titlebar").appendChild(el("a", {href:e.target.result, download:document.title}, "Download QuickServe Script"))
|
|
}
|
|
a.readAsDataURL(new Blob([params.body], {encoding:"UTF-8",type:"text/javascript;charset=UTF-8"}));
|
|
|
|
|
|
document.getElementById("title").innerText = " - " + document.title;
|
|
parent.postMessage({ title: document.title }, "*");
|
|
onConnect()
|
|
setInterval(() => { onUpdate() }, 1000 / 30)
|
|
}, 100)
|
|
document.onkeydown = (e) => {
|
|
console.log(e)
|
|
if (e.metaKey || e.ctrlKey) return;
|
|
let code = e.key.length <= 1 ? e.key.charCodeAt(0) : e.keyCode;
|
|
if (e.key == "Enter") code = 10;
|
|
// if (code >= 65 && code <= 90 && (!e.shiftKey)) code += 32;
|
|
onInput(code);
|
|
}
|
|
}
|
|
|
|
function drawChar(cx, cy, c) {
|
|
|
|
let x = cx * xs;
|
|
let y = cy * ys;
|
|
let w = xs;
|
|
let h = ys;
|
|
|
|
let i = chars.indexOf(c);
|
|
ctx.clearRect(x, y, w, h);
|
|
|
|
if (c == "™" || c == "%") { w *= 2; x -= 7; i -= 4 / 9; }; // special case wide characters
|
|
|
|
if (c == " ") i = 121;
|
|
|
|
ctx.drawImage(img,
|
|
(i % 42) * xs, Math.floor(i / 42) * ys, w, h,
|
|
x, y, w, h);
|
|
ctx.globalCompositeOperation = 'source-atop';
|
|
ctx.fillRect(x, y, w, h)
|
|
ctx.globalCompositeOperation = 'source-over';
|
|
}
|
|
|
|
let ys = 15;
|
|
let xs = 9
|
|
|
|
// Clear the screen.
|
|
window.clearScreen = () => {
|
|
ctx.fillStyle = colors[0];
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
}
|
|
|
|
// Draw the specified text. The color parameter is a number between 0 (darkest) and 17 (lightest).
|
|
window.drawText = (text, color, x, y) => {
|
|
ctx.fillStyle = colors[color];
|
|
text = text.toString();
|
|
for (var i = 0; i < text.length; i++) {
|
|
drawChar(x + i, y, text[i]);
|
|
}
|
|
// ctx.fillText(text, x * xs, y * ys);
|
|
}
|
|
|
|
// Draw the specified text, wrapping it so that it is no more than width characters wide.
|
|
window.drawTextWrapped = (text, color, x, y, width) => {
|
|
ctx.fillStyle = colors[color];
|
|
text = text.toString();
|
|
var row = 0;
|
|
var col = 0;
|
|
let words = text.split(" ");
|
|
words.forEach(word => {
|
|
if (col + word.length > width) {
|
|
row += 1;
|
|
col = 0;
|
|
}
|
|
drawText(word, color, x + col, y + row);
|
|
col += word.length + 1;
|
|
})
|
|
}
|
|
|
|
// Draw a box using the built-in box drawing characters.
|
|
window.drawBox = (color, x, y, width, height) => {
|
|
ctx.fillStyle = colors[color];
|
|
for (var i = 1; i < width - 1; i++) {
|
|
drawChar(x + i, y, "═");
|
|
drawChar(x + i, y + height - 1, "═");
|
|
}
|
|
for (var j = 1; j < height - 1; j++) {
|
|
drawChar(x, y + j, "║");
|
|
drawChar(x + width - 1, y + j, "║");
|
|
}
|
|
drawChar(x, y, "╔");
|
|
drawChar(x + width - 1, y, "╗");
|
|
drawChar(x, y + height - 1, "╚");
|
|
drawChar(x + width - 1, y + height - 1, "╝");
|
|
}
|
|
|
|
// Fill an area using the specified symbol.
|
|
window.fillArea = (symbol, color, x, y, width, height) => {
|
|
ctx.fillStyle = colors[color];
|
|
for (var i = 0; i < width; i++) {
|
|
for (var j = 0; j < height; j++) {
|
|
drawChar(x + i, y + j, symbol);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Write this server's persisted data string. You can convert a JavaScript object to a JSON string using JSON.stringify().
|
|
window.saveData = (data) => {
|
|
try {
|
|
localStorage.setItem(document.title, data);
|
|
} catch (e) {}
|
|
}
|
|
|
|
// Read this server's persisted data string. You can convert a JSON string to a JavaScript object using JSON.parse().
|
|
window.loadData = () => {
|
|
try {
|
|
return localStorage.getItem(document.title) || "";
|
|
} catch (e) {
|
|
console.log("cannot load data", e)
|
|
return ""
|
|
}
|
|
}; |