Skip to content

Commit 92cb540

Browse files
authored
text mode, fix saved svg viewbox (#130)
1 parent f2152c2 commit 92cb540

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

docs/static/hexmaps.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ html, body {
6161
height: 100vh;
6262
}
6363

64+
#textFontSize {
65+
width: 75px;
66+
height: 50px;
67+
paadding: 0;
68+
margin: 0;
69+
font-size: 20px;
70+
text-align: right;
71+
}
72+
6473
/* Composible properties */
6574
.hidden {
6675
display: none !important;

docs/static/hexmaps.js

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Controls = {
2727
COLOR: "COLOR",
2828
OBJECT: "OBJECT",
2929
PATHTIPSYMBOL: "PATHTIPSYMBOL",
30+
TEXT: "TEXT",
3031
}
3132

3233
const LAYER_TOOL_COMPATIBILITY = {
@@ -42,7 +43,7 @@ const LAYER_CONTROL_COMPATIBILITY = {
4243
[Layers.OBJECT]: [Controls.OBJECT],
4344
[Layers.PATH]: [Controls.COLOR, Controls.PATHTIPSYMBOL],
4445
[Layers.BOUNDARY]: [Controls.COLOR],
45-
[Layers.TEXT]: [Controls.COLOR],
46+
[Layers.TEXT]: [Controls.COLOR, Controls.TEXT],
4647
};
4748

4849
/****************
@@ -97,6 +98,9 @@ const GLOBAL_STATE = {
9798
TEXT: {
9899
primaryColor: "#b8895f",
99100
secondaryColor: "#7eaaad",
101+
bold: false,
102+
italics: false,
103+
underline: false,
100104
}
101105
},
102106
}
@@ -113,9 +117,17 @@ const OBJECT_BUTTONS = document.querySelectorAll(".object-btn");
113117
const PATH_TIP_SYMBOL_BUTTONS = document.querySelectorAll(".path-tip-symbol-btn");
114118
const CHOSEN_PRIMARY_COLOR_DIV = document.getElementById("chosenPrimaryColor");
115119
const CHOSEN_SECONDARY_COLOR_DIV = document.getElementById("chosenSecondaryColor");
120+
const TEXT_INPUT_DIV = document.getElementById("textInput");
121+
const TEXT_FONT_SIZE_DIV = document.getElementById("textFontSize");
122+
const TEXT_BOLD_DIV = document.getElementById("textBold");
123+
const TEXT_ITALICS_DIV = document.getElementById("textItalics");
124+
const TEXT_UNDERLINE_DIV = document.getElementById("textUnderline");
116125

117126
// keyboard shortcuts
118127
document.addEventListener("keydown", e => {
128+
if (document.activeElement.tagName == "INPUT") {
129+
return;
130+
}
119131
switch(e.code) {
120132
case "Digit1":
121133
switchToLayer(Layers.BASE);
@@ -224,6 +236,22 @@ PATH_TIP_SYMBOL_BUTTONS.forEach(btn => {
224236
});
225237
});
226238

239+
TEXT_BOLD_DIV.addEventListener("click", (e) => {
240+
TEXT_BOLD_DIV.classList.toggle("selected");
241+
GLOBAL_STATE.layers.TEXT.bold = !GLOBAL_STATE.layers.TEXT.bold;
242+
});
243+
244+
TEXT_ITALICS_DIV.addEventListener("click", (e) => {
245+
TEXT_ITALICS_DIV.classList.toggle("selected");
246+
GLOBAL_STATE.layers.TEXT.italics = !GLOBAL_STATE.layers.TEXT.italics;
247+
});
248+
249+
TEXT_UNDERLINE_DIV.addEventListener("click", (e) => {
250+
TEXT_UNDERLINE_DIV.classList.toggle("selected");
251+
GLOBAL_STATE.layers.TEXT.underline = !GLOBAL_STATE.layers.TEXT.underline;
252+
});
253+
254+
227255
document.getElementById("rotateAxesBtn").addEventListener("click", (e) => {
228256
GLOBAL_STATE.useVerticalAxes = !GLOBAL_STATE.useVerticalAxes;
229257
svgInit();
@@ -598,6 +626,32 @@ function drawPath(hexEntry) {
598626
SVG.appendChild(pathTip);
599627
}
600628

629+
function placeTextAtPoint(pt) {
630+
const textbox = document.createElementNS("http://www.w3.org/2000/svg", "text");
631+
textbox.setAttribute("font-size", TEXT_FONT_SIZE_DIV.value);
632+
if (GLOBAL_STATE.layers.TEXT.bold) {
633+
textbox.setAttribute("stroke-width", "0.5");
634+
}
635+
if (GLOBAL_STATE.layers.TEXT.italics) {
636+
textbox.setAttribute("font-style", "italic");
637+
}
638+
if (GLOBAL_STATE.layers.TEXT.underline) {
639+
textbox.setAttribute("text-decoration", "underline");
640+
}
641+
642+
textbox.setAttribute("x", pt.x);
643+
textbox.setAttribute("y", pt.y);
644+
textbox.setAttribute("fill", GLOBAL_STATE.layers.TEXT.primaryColor);
645+
textbox.textContent = TEXT_INPUT_DIV.value;
646+
textbox.classList.add("no-pointer-events");
647+
textbox.classList.add("in-image-text");
648+
textbox.addEventListener("click", e => {
649+
if (GLOBAL_STATE.currentTool == Tools.ERASER) SVG.removeChild(textbox);
650+
});
651+
SVG.appendChild(textbox);
652+
makeEraseable(textbox);
653+
}
654+
601655
function handleHexInteraction(c, r, mouseX, mouseY, isClick) {
602656
const hexEntry = GLOBAL_STATE.hexes[`${c},${r}`];
603657
const {hex, x, y} = hexEntry;
@@ -636,19 +690,8 @@ function handleHexInteraction(c, r, mouseX, mouseY, isClick) {
636690
}
637691
} else if (GLOBAL_STATE.currentLayer == Layers.TEXT) {
638692
if (GLOBAL_STATE.currentTool == Tools.BRUSH) {
639-
const textbox = document.createElementNS("http://www.w3.org/2000/svg", "text");
640693
const pt = new DOMPoint(mouseX, mouseY).matrixTransform(SVG.getScreenCTM().inverse());
641-
textbox.setAttribute("x", pt.x);
642-
textbox.setAttribute("y", pt.y);
643-
textbox.setAttribute("fill", GLOBAL_STATE.layers.TEXT.primaryColor);
644-
textbox.textContent = "Here be dragons";
645-
textbox.classList.add("no-pointer-events");
646-
textbox.classList.add("in-image-text");
647-
textbox.addEventListener("click", e => {
648-
if (GLOBAL_STATE.currentTool == Tools.ERASER) SVG.removeChild(textbox);
649-
});
650-
SVG.appendChild(textbox);
651-
makeEraseable(textbox);
694+
placeTextAtPoint(pt);
652695
}
653696
} else if (GLOBAL_STATE.currentLayer == Layers.PATH) {
654697
if (GLOBAL_STATE.currentTool == Tools.BRUSH) {
@@ -743,7 +786,10 @@ function svgInit() {
743786
* EXTRA FUNCTIONALITIES *
744787
*************************/
745788
function saveSvg() {
746-
const svgData = SVG.outerHTML;
789+
const clonedSvg = SVG.cloneNode(true);
790+
const bbox = SVG.getBBox();
791+
clonedSvg.setAttribute("viewBox", `${bbox.x} ${bbox.y} ${bbox.width} ${bbox.height}`);
792+
const svgData = clonedSvg.outerHTML;
747793
const preface = '<?xml version="1.0" standalone="no"?>\r\n';
748794
const svgBlob = new Blob([preface, svgData], {type:"image/svg+xml;charset=utf-8"});
749795
const svgUrl = URL.createObjectURL(svgBlob);

templates/template_hexmaps.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@
8989
<div class="path-tip-symbol-btn btn border-box" data-text="🚩">🚩</div>
9090
<div class="path-tip-symbol-btn btn border-box" data-text="💣">💣</div>
9191
</div>
92+
<div class="layer-control control-panel flex-column gappy overlaid" data-control="TEXT">
93+
<div class="flex-row gappy">
94+
<input id="textFontSize" type="number" dir="rtl" class="border-box" value="45"></input>
95+
<div id="textBold" class="btn border-box">B️</div>
96+
<div id="textItalics" class="btn border-box">I</div>
97+
<div id="textUnderline" class="btn border-box">U</div>
98+
</div>
99+
<input id="textInput" placeholder="Text to insert"></input>
100+
</div>
92101
<div class="layer-control control-panel flex-column gappy overlaid" data-control="COLOR">
93102
<svg xmlns='http://www.w3.org/2000/svg' width='45' height='45' viewport='0 0 45 45'>
94103
<rect id="chosenSecondaryColor" x="0" y="0" width="30" height="30" fill="#7eaaad" stroke="grey"/>

0 commit comments

Comments
 (0)