Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYuion committed Feb 25, 2022
1 parent 8898a35 commit 6b1e37d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.3.18-dev",
"version": "0.3.20-dev",
"license": "MIT",
"name": "@placeos/svg-viewer",
"author": "Alex Sorafumo <[email protected]>",
Expand Down
141 changes: 134 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ export function generateCoordinateListForTree(element: HTMLElement): HashMap<Rec
const box = el?.getBoundingClientRect() || {};
mapping[el.id] = {
x:
Math.floor(
((box.left + box.width / 2 - p_box.left) / (p_box.width)) * 100000
) / 100000,
Math.floor(((box.left + box.width / 2 - p_box.left) / p_box.width) * 100000) /
100000,
y:
Math.floor(
((box.top + box.height / 2 - p_box.top) / (p_box.height)) * 100000
) / 100000,
Math.floor(((box.top + box.height / 2 - p_box.top) / p_box.height) * 100000) /
100000,
w: Math.floor((box.width / p_box.width) * 100000) / 100000,
h: Math.floor((box.height / p_box.height) * 100000) / 100000,
};
Expand Down Expand Up @@ -167,7 +165,12 @@ export function middleOf(points: Point[]) {
};
}

export function calculateCenterFromZoomOffset(delta: number, towards: Point, from: Point, scaling = { x: 1, y: 1 }) {
export function calculateCenterFromZoomOffset(
delta: number,
towards: Point,
from: Point,
scaling = { x: 1, y: 1 }
) {
return {
x: +(from.x + (towards.x - from.x) * delta * scaling.x).toFixed(5),
y: +(from.y + (towards.y - from.y) * delta * scaling.y).toFixed(5),
Expand Down Expand Up @@ -195,3 +198,127 @@ export function simplifyDataObject(obj: any) {
}
return data;
}

/* Base64 string to array encoding */

export function uint6ToB64(nUint6: number) {
return nUint6 < 26
? nUint6 + 65
: nUint6 < 52
? nUint6 + 71
: nUint6 < 62
? nUint6 - 4
: nUint6 === 62
? 43
: nUint6 === 63
? 47
: 65;
}

export function base64EncArr(aBytes: Uint8Array) {
var nMod3 = 2,
sB64Enc = '';

for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
if (nIdx > 0 && ((nIdx * 4) / 3) % 76 === 0) {
sB64Enc += '\r\n';
}
nUint24 |= aBytes[nIdx] << ((16 >>> nMod3) & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCodePoint(
uint6ToB64((nUint24 >>> 18) & 63),
uint6ToB64((nUint24 >>> 12) & 63),
uint6ToB64((nUint24 >>> 6) & 63),
uint6ToB64(nUint24 & 63)
);
nUint24 = 0;
}
}

return (
sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) +
(nMod3 === 2 ? '' : nMod3 === 1 ? '=' : '==')
);
}

export function strToUTF8Arr(sDOMStr: string) {
var aBytes,
nChr,
nStrLen = sDOMStr.length,
nArrLen = 0;

/* mapping... */

for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
nChr = sDOMStr.codePointAt(nMapIdx) ?? 0;

if (nChr > 65536) {
nMapIdx++;
}

nArrLen +=
nChr < 0x80
? 1
: nChr < 0x800
? 2
: nChr < 0x10000
? 3
: nChr < 0x200000
? 4
: nChr < 0x4000000
? 5
: 6;
}

aBytes = new Uint8Array(nArrLen);

/* transcription... */

for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
nChr = sDOMStr.codePointAt(nChrIdx) ?? 0;
if (nChr < 128) {
/* one byte */
aBytes[nIdx++] = nChr;
} else if (nChr < 0x800) {
/* two bytes */
aBytes[nIdx++] = 192 + (nChr >>> 6);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x10000) {
/* three bytes */
aBytes[nIdx++] = 224 + (nChr >>> 12);
aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x200000) {
/* four bytes */
aBytes[nIdx++] = 240 + (nChr >>> 18);
aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63);
aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
} else if (nChr < 0x4000000) {
/* five bytes */
aBytes[nIdx++] = 248 + (nChr >>> 24);
aBytes[nIdx++] = 128 + ((nChr >>> 18) & 63);
aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63);
aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
} /* if (nChr <= 0x7fffffff) */ else {
/* six bytes */
aBytes[nIdx++] = 252 + (nChr >>> 30);
aBytes[nIdx++] = 128 + ((nChr >>> 24) & 63);
aBytes[nIdx++] = 128 + ((nChr >>> 18) & 63);
aBytes[nIdx++] = 128 + ((nChr >>> 12) & 63);
aBytes[nIdx++] = 128 + ((nChr >>> 6) & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
}
}

return aBytes;
}

export function stringToBase64(str: string) {
return base64EncArr(strToUTF8Arr(str));
}
5 changes: 3 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
generateCoordinateListForTree,
log,
simplifyDataObject,
stringToBase64,
} from './helpers';
import { focusOnFeature, listenForResize, listenForViewActions } from './input';
import { listViewers, on_resize, update } from './store';
Expand Down Expand Up @@ -183,7 +184,7 @@ export async function renderToIFrame(viewer: Viewer) {
window.attachEvent("onmessage", updateStyles);
}
</script>`;
const svg64 = btoa(
const svg64 = stringToBase64(
`<html><head><style>*{overflow:hidden;}html,body{padding:0;margin:0;}</style><style id="style"></style>${domain_js}</head><body>${svg_string}</body></html>`
);
const b64Start = 'data:text/html;base64,';
Expand Down Expand Up @@ -392,11 +393,11 @@ export function renderFeatures(viewer: Viewer) {
if (size.w || size.h) {
feature_container_el.style.width = `${size.w * 100}%`;
feature_container_el.style.height = `${size.h * 100}%`;
feature_container_el.style.transform = `translate(-50%, -50%)`;
} else {
feature_container_el.style.width = `1%`;
feature_container_el.style.height = `${1 / viewer.ratio}%`;
}
feature_container_el.style.transform = `translate(-50%, -50%)`;
if (feature.content instanceof Node) {
feature_container_el.appendChild(feature.content);
}
Expand Down

0 comments on commit 6b1e37d

Please sign in to comment.