You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, bottomStretch = 3, topStretch = 1, leftStretch = 1, rightStretch = 1, limbRatio = 0.35, smoothing = 'true') {
// Parse parameters to ensure numbers and apply constraints
const parseNum = (val, def) => {
const p = parseFloat(val);
return isNaN(p) ? def : p;
};
// Stretch scale factors (0x to 20x). 1 is original scale. Default applies extreme purely to the bottom
const bS = Math.max(0, Math.min(20, parseNum(bottomStretch, 3)));
const tS = Math.max(0, Math.min(20, parseNum(topStretch, 1)));
const lS = Math.max(0, Math.min(20, parseNum(leftStretch, 1)));
const rS = Math.max(0, Math.min(20, parseNum(rightStretch, 1)));
// Percentage of the edges considered the "limbs" (1% to 49%)
const lR = Math.max(0.01, Math.min(0.49, parseNum(limbRatio, 0.35)));
const isSmooth = String(smoothing).toLowerCase() !== "false" && String(smoothing) !== "0";
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const W = originalImg.width;
const H = originalImg.height;
// Use 9-slice grid to keep the center of the image identical while stretching the specified rim sections
const x_src = [0, Math.round(W * lR), Math.round(W * (1 - lR)), W];
const y_src = [0, Math.round(H * lR), Math.round(H * (1 - lR)), H];
const w_dest = [
Math.round((x_src[1] - x_src[0]) * lS),
x_src[2] - x_src[1], // Unchanged center piece width
Math.round((x_src[3] - x_src[2]) * rS)
];
const h_dest = [
Math.round((y_src[1] - y_src[0]) * tS),
y_src[2] - y_src[1], // Unchanged center piece height
Math.round((y_src[3] - y_src[2]) * bS)
];
const x_dest = [0, w_dest[0], w_dest[0] + w_dest[1], w_dest[0] + w_dest[1] + w_dest[2]];
const y_dest = [0, h_dest[0], h_dest[0] + h_dest[1], h_dest[0] + h_dest[1] + h_dest[2]];
canvas.width = x_dest[3];
canvas.height = y_dest[3];
ctx.imageSmoothingEnabled = isSmooth;
// Draw the 3x3 stretched grid
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
const sx = x_src[col];
const sy = y_src[row];
const sw = x_src[col + 1] - sx;
const sh = y_src[row + 1] - sy;
const dx = x_dest[col];
const dy = y_dest[row];
const dw = w_dest[col];
const dh = h_dest[row];
// Prevent blank renders if scaled to 0
if (sw > 0 && sh > 0 && dw > 0 && dh > 0) {
ctx.drawImage(originalImg, sx, sy, sw, sh, dx, dy, dw, dh);
}
}
}
return canvas;
}
Apply Changes