Skip to content

Instantly share code, notes, and snippets.

@shvgn
Created April 10, 2018 19:31
Show Gist options
  • Save shvgn/657e3d30c1cf850a00c5ff5f0723235e to your computer and use it in GitHub Desktop.
Save shvgn/657e3d30c1cf850a00c5ff5f0723235e to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zesudem
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/*** PORTED FROM PLAYER ***/
"use strict";
var MIN_TEXTURE_SIZE = 128;
var MAX_TEXTURE_SIZE = 1024;
function calcTextureSize(width, height) {
var minSize = Math.min(width, height);
var count = Math.max(1, Math.round(minSize / MAX_TEXTURE_SIZE));
if (minSize <= MIN_TEXTURE_SIZE) return MIN_TEXTURE_SIZE;
while (width % count !== 0 && height % count !== 0) {
++count;
if (minSize / count <= MIN_TEXTURE_SIZE) {
return MIN_TEXTURE_SIZE;
}
}
return Math.max(MIN_TEXTURE_SIZE, minSize / count);
}
/*** CUSTOM CODE ***/
function textureCount(width, height) {
var tileSize = calcTextureSize(width, height);
if (width === tileSize) return Math.ceil(height / tileSize);
if (height === tileSize) return Math.ceil(width / tileSize);
var fullTilesCountX = Math.floor(width / tileSize);
var fullTilesCountY = Math.floor(height / tileSize);
var fullTilesCommonWidth = fullTilesCountX * tileSize;
var fullTilesCommonHeight = fullTilesCountY * tileSize;
var tileArea = tileSize * tileSize;
var imageArea = width * height;
if (fullTilesCommonWidth === width && fullTilesCommonHeight === height) {
return imageArea / tileArea;
}
var getPartialTilesCount = function getPartialTilesCount(rawDistanceA, fullTilesCountB) {
return +!!(rawDistanceA % tileSize) * fullTilesCountB;
};
var partialTilesCountX = getPartialTilesCount(width, fullTilesCountY);
var partialTilesCountY = getPartialTilesCount(height, fullTilesCountX);
var extraTileInCorner = partialTilesCountX > 0 && partialTilesCountY > 0;
var partialCount = partialTilesCountX + partialTilesCountY + extraTileInCorner;
return fullTilesCountX * fullTilesCountY + partialCount;
}
/*** MANUAL TEST ***/
var w = 1366;
var h = 768;
console.log("\ncalcTextureSize(" + w + ", " + h + ") = " + calcTextureSize(w, h) + "\ntextureCount(" + w + ", " + h + ") = " + textureCount(w, h) + "\n");
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*** PORTED FROM PLAYER ***/
const MIN_TEXTURE_SIZE = 128
const MAX_TEXTURE_SIZE = 1024
function calcTextureSize(width, height) {
const minSize = Math.min(width, height)
let count = Math.max(1, Math.round(minSize / MAX_TEXTURE_SIZE))
if (minSize <= MIN_TEXTURE_SIZE) return MIN_TEXTURE_SIZE
while (width % count !== 0 && height % count !== 0) {
++count
if (minSize / count <= MIN_TEXTURE_SIZE) {
return MIN_TEXTURE_SIZE
}
}
return Math.max(MIN_TEXTURE_SIZE, minSize / count)
}
/*** CUSTOM CODE ***/
function textureCount (width, height) {
const tileSize = calcTextureSize(width, height)
if (width === tileSize) return Math.ceil( height / tileSize )
if (height === tileSize) return Math.ceil( width / tileSize )
const fullTilesCountX = Math.floor(width / tileSize)
const fullTilesCountY = Math.floor(height / tileSize)
const fullTilesCommonWidth = fullTilesCountX * tileSize
const fullTilesCommonHeight = fullTilesCountY * tileSize
const tileArea = tileSize * tileSize
const imageArea = width * height
if (fullTilesCommonWidth === width &&
fullTilesCommonHeight === height) {
return imageArea / tileArea
}
const getPartialTilesCount = (rawDistanceA, fullTilesCountB) => +(!!(rawDistanceA % tileSize)) * fullTilesCountB
const partialTilesCountX = getPartialTilesCount(width, fullTilesCountY)
const partialTilesCountY = getPartialTilesCount(height, fullTilesCountX)
const extraTileInCorner = (partialTilesCountX > 0 && partialTilesCountY > 0)
const partialCount = partialTilesCountX + partialTilesCountY + extraTileInCorner
return fullTilesCountX * fullTilesCountY + partialCount
}
/*** MANUAL TEST ***/
const w = 1366
const h = 768
console.log( `
calcTextureSize(${w}, ${h}) = ${calcTextureSize(w, h)}
textureCount(${w}, ${h}) = ${textureCount(w, h)}
`)
</script></body>
</html>
/*** PORTED FROM PLAYER ***/
"use strict";
var MIN_TEXTURE_SIZE = 128;
var MAX_TEXTURE_SIZE = 1024;
function calcTextureSize(width, height) {
var minSize = Math.min(width, height);
var count = Math.max(1, Math.round(minSize / MAX_TEXTURE_SIZE));
if (minSize <= MIN_TEXTURE_SIZE) return MIN_TEXTURE_SIZE;
while (width % count !== 0 && height % count !== 0) {
++count;
if (minSize / count <= MIN_TEXTURE_SIZE) {
return MIN_TEXTURE_SIZE;
}
}
return Math.max(MIN_TEXTURE_SIZE, minSize / count);
}
/*** CUSTOM CODE ***/
function textureCount(width, height) {
var tileSize = calcTextureSize(width, height);
if (width === tileSize) return Math.ceil(height / tileSize);
if (height === tileSize) return Math.ceil(width / tileSize);
var fullTilesCountX = Math.floor(width / tileSize);
var fullTilesCountY = Math.floor(height / tileSize);
var fullTilesCommonWidth = fullTilesCountX * tileSize;
var fullTilesCommonHeight = fullTilesCountY * tileSize;
var tileArea = tileSize * tileSize;
var imageArea = width * height;
if (fullTilesCommonWidth === width && fullTilesCommonHeight === height) {
return imageArea / tileArea;
}
var getPartialTilesCount = function getPartialTilesCount(rawDistanceA, fullTilesCountB) {
return +!!(rawDistanceA % tileSize) * fullTilesCountB;
};
var partialTilesCountX = getPartialTilesCount(width, fullTilesCountY);
var partialTilesCountY = getPartialTilesCount(height, fullTilesCountX);
var extraTileInCorner = partialTilesCountX > 0 && partialTilesCountY > 0;
var partialCount = partialTilesCountX + partialTilesCountY + extraTileInCorner;
return fullTilesCountX * fullTilesCountY + partialCount;
}
/*** MANUAL TEST ***/
var w = 1366;
var h = 768;
console.log("\ncalcTextureSize(" + w + ", " + h + ") = " + calcTextureSize(w, h) + "\ntextureCount(" + w + ", " + h + ") = " + textureCount(w, h) + "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment