P.cleanOutput = function () {
let self = this;
return new Promise((resolve, reject) => {
const halfPi = Math.PI / 2;
self.dirtyOutput = false;
let {sourceDimension, sourceImageData, columns, rows, struts, boundingBox} = self;
sourceDimension = Math.ceil(sourceDimension);
if (sourceImageData && rows - 1 > 0) {
let [startX, startY, outputWidth, outputHeight] = boundingBox;
outputWidth += startX;
outputHeight += startY;
const inputCell = requestCell(),
inputEngine = inputCell.engine,
inputCanvas = inputCell.element;
inputCanvas.width = sourceDimension;
inputCanvas.height = sourceDimension;
inputEngine.setTransform(1, 0, 0, 1, 0, 0);
inputEngine.putImageData(sourceImageData, 0, 0);
const outputCell = requestCell(),
outputEngine = outputCell.engine,
outputCanvas = outputCell.element;
outputCanvas.width = outputWidth;
outputCanvas.height = outputHeight;
outputEngine.globalAlpha = self.state.globalAlpha;
outputEngine.setTransform(1, 0, 0, 1, 0, 0);
const inputStrutHeight = parseFloat((sourceDimension / (rows - 1)).toFixed(4)),
inputStrutWidth = parseFloat((sourceDimension / (columns - 1)).toFixed(4));
let topStruts, baseStruts,
maxLen, tStep, bStep, iStep, xtStep, ytStep, xbStep, ybStep, tx, ty, bx, by, sx, sy,
xLen, yLen, stripLength, stripAngle,
c, cz, r, rz, i, iz;
for (r = 0, rz = rows - 1; r < rz; r++) {
topStruts = struts[r];
baseStruts = struts[r + 1];
for (c = 0, cz = columns - 1; c < cz; c++) {
let [ltx, lty, rtx, rty, tLen] = topStruts[c];
let [lbx, lby, rbx, rby, bLen] = baseStruts[c];
tLen *= sourceDimension;
bLen *= sourceDimension;
maxLen = Math.max(tLen, bLen, inputStrutWidth);
tStep = tLen / maxLen;
bStep = bLen / maxLen;
iStep = inputStrutWidth / maxLen;
xtStep = (rtx - ltx) / maxLen;
ytStep = (rty - lty) / maxLen;
xbStep = (rbx - lbx) / maxLen;
ybStep = (rby - lby) / maxLen;
for (i = 0; i < maxLen; i++) {
tx = ltx + (xtStep * i);
ty = lty + (ytStep * i);
bx = lbx + (xbStep * i);
by = lby + (ybStep * i);
sy = r * inputStrutHeight;
sx = (c * inputStrutWidth) + (iStep * i);
xLen = tx - bx;
yLen = ty - by;
stripLength = Math.sqrt((xLen * xLen) + (yLen * yLen));
stripAngle = Math.atan2(yLen, xLen) + halfPi;
outputEngine.setTransform(1, 0, 0, 1, tx, ty);
outputEngine.rotate(stripAngle);