27 lines
668 B
JavaScript
27 lines
668 B
JavaScript
function loadTemplateAsync(path) {
|
|
const result = new Promise(resolve => {
|
|
const xhttp = new XMLHttpRequest()
|
|
|
|
xhttp.onreadystatechange = function () {
|
|
if (this.readyState == 4) {
|
|
if (this.status == 200) resolve(this.responseText)
|
|
|
|
if (this.status == 404) resolve(`<div>Page not found: ${path}</div>`)
|
|
}
|
|
}
|
|
|
|
xhttp.open('GET', path, true)
|
|
xhttp.send()
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
function imgSizeFit(img, maxWidth = 1024, maxHeight = 768) {
|
|
let ratio = Math.min(
|
|
1,
|
|
maxWidth / img.naturalWidth,
|
|
maxHeight / img.naturalHeight
|
|
)
|
|
return {width: img.naturalWidth * ratio, height: img.naturalHeight * ratio}
|
|
}
|