@bytedance/mona-client-web
Version:
web for mona
48 lines • 2.23 kB
JavaScript
import CanvasContext from './CanvasContext';
function queryCanvasByCanvasId(canvasId) {
// query name to Compatible with max
return (document.querySelector("canvas[data-canvas-id='".concat(canvasId, "']")) ||
document.querySelector("canvas[name='".concat(canvasId, "']")));
}
function createCanvasContext(canvasId) {
var $canvas = queryCanvasByCanvasId(canvasId);
return new CanvasContext($canvas);
}
function canvasToTempFilePath(options) {
var canvasId = options.canvasId, width = options.width, height = options.height, destWidth = options.destWidth, destHeight = options.destHeight, _a = options.x, x = _a === void 0 ? 0 : _a, _b = options.y, y = _b === void 0 ? 0 : _b, _c = options.success, success = _c === void 0 ? function () { } : _c, _d = options.fail, fail = _d === void 0 ? function () { } : _d, _e = options.complete, complete = _e === void 0 ? function () { } : _e;
var res = { errMsg: 'canvasToTempFilePath:ok' };
try {
var $canvas = queryCanvasByCanvasId(canvasId);
if ($canvas) {
var $offscreenCanvas = document.createElement('canvas');
var realWidth = width || $canvas.offsetWidth;
var realHeight = height || $canvas.offsetHeight;
$offscreenCanvas.width = destWidth || realWidth;
$offscreenCanvas.height = destHeight || realHeight;
var context = $offscreenCanvas.getContext('2d');
context === null || context === void 0 ? void 0 : context.drawImage($canvas, x, y, realWidth, realHeight);
var tempFilePath = $offscreenCanvas.toDataURL();
res = { errMsg: 'canvasToTempFilePath:ok', tempFilePath: tempFilePath };
success(res);
}
}
catch (err) {
res = { errMsg: 'canvasToTempFilePath:fail ' + err.message };
fail(res);
}
finally {
complete(res);
}
}
function createOffscreenCanvas(width, height) {
var $canvas = document.createElement('canvas');
if (width) {
$canvas.width = width;
}
if (height) {
$canvas.height = height;
}
return $canvas;
}
export { createCanvasContext, canvasToTempFilePath, createOffscreenCanvas };
//# sourceMappingURL=v1.js.map