Skip to content

Commit

Permalink
1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
headlesshorse committed Nov 7, 2024
1 parent 6ba9df0 commit b2b87d4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dist/pruner.min.js

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,6 +1,6 @@
{
"name": "prunerjs",
"version": "1.1.2",
"version": "1.1.3",
"description": "Responsive image Javascript utility using viewport-based rendering.",
"main": "dist/pruner.min.js",
"scripts": {
Expand Down
20 changes: 7 additions & 13 deletions src/pruner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ function pruner() {
const elems = [...document.querySelectorAll('[data-pruner]')], cache = {};
let prevW = 0, prevH = 0;

const debounce = (fn, delay) => { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), delay); }; };

const loadImg = src => cache[src] || (cache[src] = new Promise((res, rej) => { const img = new Image(); img.crossOrigin = 'anonymous'; img.onload = () => res(img); img.onerror = () => rej(`Error loading: ${src}`); img.src = src; }));
const debounce = (fn, d) => { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), d); }; };
const loadImg = src => cache[src] || (cache[src] = new Promise((r, rej) => { const img = new Image(); img.crossOrigin = 'anonymous'; img.onload = () => r(img); img.onerror = () => rej(src); img.src = src; }));

const processImgs = async (el, vw, vh) => {
const { name, tile, scale = "1 0", path = '', roi, imageExtension = 'webp' } = JSON.parse(el.dataset.pruner);
const { name, tile, scale = "1 0", path = '', roi, imageExtension = 'webp' } = JSON.parse(el.dataset.pruner);
if (!tile || !name) return;
const [cols, rows] = tile.split(" ").map(Number), [sf, bp] = scale.split(" "), isMobile = vw <= +bp;

const [cols, rows] = tile.split(" "), [sf, bp] = scale.split(" "), isMobile = vw <= +bp;
try {
const { width: w, height: h } = await loadImg(`${path}${name}-1.${imageExtension}`);
const sw = Math.round(w * (isMobile ? +sf : 1)), sh = Math.round(h * (isMobile ? +sf : 1));
const numCols = Math.min(Math.ceil(vw / sw), cols), numRows = Math.min(Math.ceil(vh / sh), rows);
const c = document.createElement('canvas');
c.width = numCols * sw;
c.height = numRows * sh;
c.width = numCols * sw; c.height = numRows * sh;
const ctx = c.getContext('2d');
ctx.imageSmoothingEnabled = false;
el.src = '';
Expand All @@ -27,18 +24,15 @@ function pruner() {
const imgs = await Promise.all(
Array.from({ length: numRows * numCols }, (_, i) => {
const r = clampedStartR + Math.floor(i / numCols), c = clampedStartC + (i % numCols);
return r < rows && c < cols ? loadImg(`${path}${name}-${r * cols + c + 1}.${imageExtension}`) : Promise.resolve(null);
return r < rows && c < cols ? loadImg(`${path}${name}-${r * cols + c + 1}.${imageExtension}`) : null;
})
);
imgs.forEach((img, i) => img && ctx.drawImage(img, (i % numCols) * sw, Math.floor(i / numCols) * sh, sw, sh));
el.src = c.toDataURL('image/webp');
} catch (e) {}
};

const handleResize = debounce(() => {
const w = innerWidth, h = innerHeight;
if (w !== prevW || h !== prevH) { prevW = w; prevH = h; elems.forEach(el => processImgs(el, w, h)); }
}, 200);
const handleResize = debounce(() => { const w = innerWidth, h = innerHeight; if (w !== prevW || h !== prevH) { prevW = w; prevH = h; elems.forEach(el => processImgs(el, w, h)); } }, 200);

const obs = new IntersectionObserver(entries => entries.forEach(entry => { if (entry.isIntersecting) { handleResize(); obs.unobserve(entry.target); } }));
window.addEventListener('load', handleResize);
Expand Down

0 comments on commit b2b87d4

Please sign in to comment.