mirror of
https://github.com/NaitLee/Cat-Printer.git
synced 2025-05-29 05:30:08 -07:00
+ Added font and text size to settings panel
This commit is contained in:
parent
949ab15857
commit
8f402eb80f
@ -91,6 +91,19 @@
|
|||||||
<input type="checkbox" name="dump" data-key />
|
<input type="checkbox" name="dump" data-key />
|
||||||
<span data-i18n="dump-traffic">Dump Traffic</span>
|
<span data-i18n="dump-traffic">Dump Traffic</span>
|
||||||
</label> -->
|
</label> -->
|
||||||
|
<label data-i18n="text-font">Text font</label>
|
||||||
|
<select id="text-font">
|
||||||
|
<option value="Serif">Serif</option>
|
||||||
|
<option value="Sans-serif" selected>Sans-serif</option>
|
||||||
|
<option value="monospace">Monospace</option>
|
||||||
|
<option value="cursive">Cursive</option>
|
||||||
|
</select>
|
||||||
|
<br>
|
||||||
|
<label>
|
||||||
|
<span data-i18n="text-size-">Text size:</span>
|
||||||
|
<input id="text-size" type="number" name="text-size" min="1" max="100" value="20" data-key />
|
||||||
|
<span>px</span>
|
||||||
|
</label>
|
||||||
<button id="set-accessibility" data-key>
|
<button id="set-accessibility" data-key>
|
||||||
<span>🌎</span>
|
<span>🌎</span>
|
||||||
<span data-i18n="accessibility">Accessibility</span>
|
<span data-i18n="accessibility">Accessibility</span>
|
||||||
|
33
www/main.js
33
www/main.js
@ -439,31 +439,46 @@ class CanvasController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
insertText() {
|
insertText() {
|
||||||
let text = prompt("Enter text");
|
|
||||||
let lines = [];
|
const textSize = parseInt(document.getElementById("text-size").value);
|
||||||
|
const textFont = document.getElementById("text-font").value;
|
||||||
|
const yStep = textSize;
|
||||||
|
|
||||||
|
const ctx = this.canvas.getContext("2d");
|
||||||
|
ctx.font = textSize + "px " + textFont;
|
||||||
|
// const textWidth = ctx.measureText(textSize);
|
||||||
|
const maxWidth = this.canvas.width - 10;
|
||||||
|
|
||||||
|
|
||||||
|
let text = prompt("Enter text");
|
||||||
|
if (text == null) { return; }
|
||||||
|
|
||||||
// Use Word Wrap to split the text over multiple lines
|
// Use Word Wrap to split the text over multiple lines
|
||||||
|
let lines = [];
|
||||||
|
const getMaxCharsPerLine = (text, ctx) => {
|
||||||
|
let textWidth = ctx.measureText(text).width;
|
||||||
|
let textIndex = maxWidth / textWidth;
|
||||||
|
|
||||||
|
if (textIndex > 1) { return text.length}
|
||||||
|
return Math.floor(textIndex * text.length);
|
||||||
|
}
|
||||||
const wrapText = (text, maxLength) => {
|
const wrapText = (text, maxLength) => {
|
||||||
let splitPos = maxLength - 1;
|
let splitPos = maxLength - 1;
|
||||||
while (text[splitPos] != " " && splitPos > 0) {
|
while (text[splitPos] != " " && splitPos > 0) {
|
||||||
splitPos--;
|
splitPos--;
|
||||||
}
|
}
|
||||||
if (splitPos == 0) { splitPos = maxLength; }
|
if (splitPos == 0) { splitPos = maxLength; }
|
||||||
return [text.slice(0, splitPos), text.slice(splitPos, text.length)];
|
return [text.slice(0, splitPos).trim(), text.slice(splitPos, text.length).trim()];
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxLength = 33;
|
while (ctx.measureText(text).width > maxWidth) {
|
||||||
while (text.length > maxLength) {
|
|
||||||
let line;
|
let line;
|
||||||
[line, text] = wrapText(text, maxLength);
|
[line, text] = wrapText(text, getMaxCharsPerLine(text, ctx));
|
||||||
lines.push(line);
|
lines.push(line);
|
||||||
}
|
}
|
||||||
lines.push(text)
|
lines.push(text)
|
||||||
|
|
||||||
let ctx = this.canvas.getContext("2d");
|
|
||||||
ctx.font = "20px Sans-Serif";
|
|
||||||
|
|
||||||
const yStep = 20;
|
|
||||||
let yPos = yStep;
|
let yPos = yStep;
|
||||||
for (let line of lines) {
|
for (let line of lines) {
|
||||||
ctx.fillText(line, 0, yPos);
|
ctx.fillText(line, 0, yPos);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user