Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatalin Mititiuc <webdevcat@proton.me>2024-05-30 10:44:22 -0700
committerCatalin Mititiuc <webdevcat@proton.me>2024-05-30 10:44:22 -0700
commit51974436fff22bcd2c37471351165179bb5daa23 (patch)
treec7582e4604da66cc0e936084d7afd6b3c3e7a671
parent1bfc83dfc6a543f0721cfc6efc04f4910ecaad05 (diff)
WIP: dynamically add references to scenario svg
-rw-r--r--esbuild-server.mjs2
-rw-r--r--public/assets/images/scenario-dragon_hunting.svg165
-rw-r--r--public/assets/images/scenario-side_show.svg44
-rw-r--r--src/import_external_resources.js25
4 files changed, 168 insertions, 68 deletions
diff --git a/esbuild-server.mjs b/esbuild-server.mjs
index 01629d1..10816d5 100644
--- a/esbuild-server.mjs
+++ b/esbuild-server.mjs
@@ -129,7 +129,7 @@ const ctx = await esbuild.context({
outdir: 'build',
plugins: [resolveImportedSvg],
loader: {
- '.svg': 'file'
+ '.svg': 'text'
},
assetNames: 'assets/images/[name]-[hash]',
});
diff --git a/public/assets/images/scenario-dragon_hunting.svg b/public/assets/images/scenario-dragon_hunting.svg
index 5f16d7a..9a41451 100644
--- a/public/assets/images/scenario-dragon_hunting.svg
+++ b/public/assets/images/scenario-dragon_hunting.svg
@@ -1,6 +1,5 @@
<?xml version="1.0" standalone="no"?>
<svg viewBox="-15.639999389648438 -129.2300262451172 2320.879638671875 4871.5654296875" xmlns="http://www.w3.org/2000/svg">
- <!-- <link xmlns="http://www.w3.org/1999/xhtml" rel="stylesheet" href="http://localhost:8080/assets/css/map.css" type="text/css" /> -->
<link xmlns="http://www.w3.org/1999/xhtml" rel="stylesheet" href="../css/map.css" type="text/css" />
<style>
g[data-edge="north"] { --i: -2; }
@@ -26,7 +25,6 @@
</style>
<defs>
<polygon id="hex" points="0,10 8.66,5 8.66,-5 0,-10 -8.66,-5 -8.66,5"/>
- <circle id="counter-base" cx="0" cy="0" r="5"/>
<line id="ast-line" x1="-1.2" y1="0" x2="1.2" y2="0"/>
@@ -46,13 +44,68 @@
<use x="-8.66" style="transform-origin: -8.66px 0;" class="ccw-60-deg" href="#ast-line"/>
</pattern>
- <g id="t-1" class="troop-counter-template"><use href="#counter-base"/><text>1</text></g>
- <g id="t-2" class="troop-counter-template"><use href="#counter-base"/><text>2</text></g>
- <g id="t-3" class="troop-counter-template"><use href="#counter-base"/><text>3</text></g>
- <g id="t-4" class="troop-counter-template"><use href="#counter-base"/><text>4</text></g>
- <g id="t-5" class="troop-counter-template"><use href="#counter-base"/><text>5</text></g>
- <g id="t-6" class="troop-counter-template"><use href="#counter-base"/><text>6</text></g>
- <g id="t-7" class="troop-counter-template"><use href="#counter-base"/><text>7</text></g>
+ <circle id="counter-base" style="r: inherit; stroke: none;" cx="0" cy="0" r="5"/>
+
+ <g style="r: inherit;" id="number-1">
+ <use style="r: inherit;" href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">1</text>
+ </g>
+ <g id="number-2">
+ <use href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">2</text>
+ </g>
+ <g id="number-3">
+ <use href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">3</text>
+ </g>
+ <g id="number-4">
+ <use href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">4</text>
+ </g>
+ <g id="number-5">
+ <use href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">5</text>
+ </g>
+ <g id="number-6">
+ <use href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">6</text>
+ </g>
+ <g id="number-7">
+ <use href="#counter-base"/>
+ <text style="fill: var(--text-fill); transform: translateY(var(--translateY))">7</text>
+ </g>
+
+ <g id="semi-auto">
+ <line x1="-2" y1="1" x2="2" y2="1"/>
+ <line x1="-2" y1="2" x2="2" y2="2"/>
+ </g>
+
+ <g id="auto">
+ <line x1="-2" y1="0" x2="2" y2="0"/>
+ <line x1="-2" y1="1" x2="2" y2="1"/>
+ <line x1="-2" y1="2" x2="2" y2="2"/>
+ </g>
+
+ <g id="rifle" style="r: inherit;" class="weapon-symbol">
+ <use style="r: inherit;" href="#counter-base"/>
+ <use href="#semi-auto"/>
+ <line x1="0" y1="-5" x2="0" y2="5"/>
+ <polyline style="fill: none;" points="-2,-3.5 0,-5 2,-3.5"/>
+ </g>
+
+ <g id="smg" style="r: inherit;" class="weapon-symbol">
+ <use style="r: inherit;" href="#counter-base"/>
+ <use href="#auto"/>
+ <line x1="0" y1="-5" x2="0" y2="4.5"/>
+ <line x1="-2" y1="4.5" x2="2" y2="4.5"/>
+ </g>
+
+ <g id="blazer" style="r: inherit;" class="weapon-symbol">
+ <use style="r: inherit;" href="#counter-base"/>
+ <use href="#auto"/>
+ <polyline points="0,-5 0,-3 -3,-2.5 3,-1.5 0,-1 0,2.5 -3,3, 3,4 0,4.5 0,5"/>
+ <polyline points="-2,-3.5 0,-5 2,-3.5"/>
+ </g>
<image id="counter-prone" href="counter_prone.jpg" width="10"/>
<image id="counter-grenade" href="counter_grenade.jpg" width="10"/>
@@ -61,9 +114,9 @@
<rect id="background"/>
<g class="gameboard">
- <use data-href="mapsheets.svg#map2"/>
- <use data-href="mapsheets.svg#map1" style="transform: translate(0, 390.25px);"/>
- <use data-href="mapsheets.svg#map3" style="transform: translate(0, 780.5px);"/>
+ <use href="mapsheets.svg#map2"/>
+ <use href="mapsheets.svg#map1" style="transform: translate(0, 390.25px);"/>
+ <use href="mapsheets.svg#map3" style="transform: translate(0, 780.5px);"/>
<rect id="dots" fill="url(#asterisk)"/>
@@ -72,57 +125,113 @@
<g id="lines"/>
</g>
- <g class="start-locations">
+ <g class="start-locations" data-attacker-name="liao" data-defender-name="davion">
<g data-edge="north">
<g data-x="13">
- <g class="counter" data-allegiance="attacker" data-number="1"><use href="#t-1"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="1">
+ <use class="primary-weapon" href="#blazer"/>
+ <use class="troop-number" href="#number-1"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="14">
- <g class="counter" data-allegiance="attacker" data-number="2"><use href="#t-2"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="2">
+ <use class="primary-weapon" href="#rifle"/>
+ <use class="troop-number" href="#number-2"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="15">
- <g class="counter" data-allegiance="attacker" data-number="3"><use href="#t-3"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="3">
+ <use class="primary-weapon" href="#rifle"/>
+ <use class="troop-number" href="#number-3"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="16">
- <g class="counter" data-allegiance="attacker" data-number="4"><use href="#t-4"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="4">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-4"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="17">
- <g class="counter" data-allegiance="attacker" data-number="5"><use href="#t-5"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="5">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-5"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="18">
- <g class="counter" data-allegiance="attacker" data-number="6"><use href="#t-6"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="6">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-6"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="19">
- <g class="counter" data-allegiance="attacker" data-number="7"><use href="#t-7"/></g>
+ <g class="counter" data-allegiance="attacker" data-number="7">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-7"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
</g>
<g data-edge="south">
<g data-x="13">
- <g class="counter" data-allegiance="defender" data-number="1"><use href="#t-1"/></g>
+ <g class="counter" data-allegiance="defender" data-number="1">
+ <use class="primary-weapon" href="#blazer"/>
+ <use class="troop-number" href="#number-1"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="14">
- <g class="counter" data-allegiance="defender" data-number="2"><use href="#t-2"/></g>
+ <g class="counter" data-allegiance="defender" data-number="2">
+ <use class="primary-weapon" href="#rifle"/>
+ <use class="troop-number" href="#number-2"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="15">
- <g class="counter" data-allegiance="defender" data-number="3"><use href="#t-3"/></g>
+ <g class="counter" data-allegiance="defender" data-number="3">
+ <use class="primary-weapon" href="#rifle"/>
+ <use class="troop-number" href="#number-3"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="16">
- <g class="counter" data-allegiance="defender" data-number="4"><use href="#t-4"/></g>
+ <g class="counter" data-allegiance="defender" data-number="4">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-4"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="17">
- <g class="counter" data-allegiance="defender" data-number="5"><use href="#t-5"/></g>
+ <g class="counter" data-allegiance="defender" data-number="5">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-5"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="18">
- <g class="counter" data-allegiance="defender" data-number="6"><use href="#t-6"/></g>
+ <g class="counter" data-allegiance="defender" data-number="6">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-6"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
<g data-x="19">
- <g class="counter" data-allegiance="defender" data-number="7"><use href="#t-7"/></g>
+ <g class="counter" data-allegiance="defender" data-number="7">
+ <use class="primary-weapon" href="#smg"/>
+ <use class="troop-number" href="#number-7"/>
+ <use class="squad-number" href="#number-1"/>
+ </g>
</g>
</g>
</g>
<g class="grid"/>
</g>
- <script href="../../import_mapsheets.js"/>
+ <script href="../../import_external_resources.js"/>
<script data-cols="33" data-rows="77" href="../../map.js"/>
</svg>
diff --git a/public/assets/images/scenario-side_show.svg b/public/assets/images/scenario-side_show.svg
index 543475e..c95b0f1 100644
--- a/public/assets/images/scenario-side_show.svg
+++ b/public/assets/images/scenario-side_show.svg
@@ -1,5 +1,5 @@
<?xml version="1.0" standalone="no"?>
-<svg viewBox="-15.639999389648438 -109 2320.879638671875 3280" xmlns="http://www.w3.org/2000/svg">
+<svg viewBox="-10 -10 200 300" xmlns="http://www.w3.org/2000/svg">
<link xmlns="http://www.w3.org/1999/xhtml" rel="stylesheet" href="../css/map.css" type="text/css" />
<style>
g[data-edge="north"] { --i: -2; }
@@ -44,34 +44,34 @@
<use x="-8.66" style="transform-origin: -8.66px 0;" class="ccw-60-deg" href="#ast-line"/>
</pattern>
- <circle id="counter-base" style="r: inherit;" cx="0" cy="0" r="5"/>
+ <circle id="counter-base" style="r: inherit; stroke: none;" cx="0" cy="0" r="5"/>
<g style="r: inherit;" id="number-1">
<use style="r: inherit;" href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">1</text>
</g>
- <g style="r: inherit;" id="number-2">
- <use style="r: inherit;" href="#counter-base"/>
+ <g id="number-2">
+ <use href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">2</text>
</g>
- <g style="r: inherit;" id="number-3">
- <use style="r: inherit;" href="#counter-base"/>
+ <g id="number-3">
+ <use href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">3</text>
</g>
- <g style="r: inherit;" id="number-4">
- <use style="r: inherit;" href="#counter-base"/>
+ <g id="number-4">
+ <use href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">4</text>
</g>
- <g style="r: inherit;" id="number-5">
- <use style="r: inherit;" href="#counter-base"/>
+ <g id="number-5">
+ <use href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">5</text>
</g>
- <g style="r: inherit;" id="number-6">
- <use style="r: inherit;" href="#counter-base"/>
+ <g id="number-6">
+ <use href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">6</text>
</g>
- <g style="r: inherit;" id="number-7">
- <use style="r: inherit;" href="#counter-base"/>
+ <g id="number-7">
+ <use href="#counter-base"/>
<text style="fill: var(--text-fill); transform: translateY(var(--translateY))">7</text>
</g>
@@ -109,25 +109,13 @@
<image id="counter-prone" href="counter_prone.jpg" width="10"/>
<image id="counter-grenade" href="counter_grenade.jpg" width="10"/>
- <image id="numbers" href="rendered_numbers.png" width="182" height="22"/>
-
- <symbol id="n1" viewBox="1 0 17 22" width="17" height="22"><use href="#numbers"/></symbol>
- <symbol id="n2" viewBox="19 0 16 22" width="16" height="22"><use href="#numbers"/></symbol>
- <symbol id="n3" viewBox="36 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n4" viewBox="54 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n5" viewBox="0 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n6" viewBox="0 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n7" viewBox="0 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n8" viewBox="0 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n9" viewBox="0 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
- <symbol id="n0" viewBox="0 0 18 22" width="18" height="22"><use href="#numbers"/></symbol>
</defs>
<rect id="background"/>
<g class="gameboard">
- <use data-href="mapsheets.svg#map2"/>
- <use data-href="mapsheets.svg#map3" style="transform: translate(0, 390.25px);"/>
+ <use href="mapsheets.svg#map2"/>
+ <use href="mapsheets.svg#map3" style="transform: translate(0, 390.25px);"/>
<rect id="dots" fill="url(#asterisk)"/>
diff --git a/src/import_external_resources.js b/src/import_external_resources.js
index aba29d2..cb1d7fb 100644
--- a/src/import_external_resources.js
+++ b/src/import_external_resources.js
@@ -1,15 +1,18 @@
import mapsheets from './assets/images/mapsheets.svg';
-import counters from './assets/images/counters.svg';
-const files = {
- 'mapsheets.svg': mapsheets,
- 'counters.svg': counters
-}
+const doc = new DOMParser().parseFromString(mapsheets, 'image/svg+xml');
+const refs = [];
-document.querySelectorAll('use[data-href*=".svg"').forEach(el => {
- const [filename] = el.dataset.href.match(/.+\.svg/g)
- , href = el.dataset.href.replace(filename, files[filename].split('/').pop())
- ;
-
- el.setAttributeNS(null, 'href', href);
+document.querySelectorAll('use[href*=".svg"').forEach(el => {
+ const fragId = el.getAttributeNS(null, 'href').split('.svg').pop();
+ const frag = doc.querySelector(fragId);
+ frag.querySelectorAll('use').forEach(el => refs.push(el.getAttributeNS(null, 'href')));
+ if (el.style.transform) frag.style.transform = el.style.transform;
+ el.replaceWith(frag);
});
+
+const refsQuery = [...new Set([...refs])].join(', ');
+const refNodes = doc.querySelectorAll(refsQuery);
+const defs = document.querySelector('defs');
+
+refNodes.forEach(n => defs.appendChild(n));