remotion
Version:
Make videos programmatically
133 lines (132 loc) • 5.09 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Canvas = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const calcArgs = (fit, frameSize, canvasSize) => {
switch (fit) {
case 'fill': {
return [
0,
0,
frameSize.width,
frameSize.height,
0,
0,
canvasSize.width,
canvasSize.height,
];
}
case 'contain': {
const ratio = Math.min(canvasSize.width / frameSize.width, canvasSize.height / frameSize.height);
const centerX = (canvasSize.width - frameSize.width * ratio) / 2;
const centerY = (canvasSize.height - frameSize.height * ratio) / 2;
return [
0,
0,
frameSize.width,
frameSize.height,
centerX,
centerY,
frameSize.width * ratio,
frameSize.height * ratio,
];
}
case 'cover': {
const ratio = Math.max(canvasSize.width / frameSize.width, canvasSize.height / frameSize.height);
const centerX = (canvasSize.width - frameSize.width * ratio) / 2;
const centerY = (canvasSize.height - frameSize.height * ratio) / 2;
return [
0,
0,
frameSize.width,
frameSize.height,
centerX,
centerY,
frameSize.width * ratio,
frameSize.height * ratio,
];
}
default:
throw new Error('Unknown fit: ' + fit);
}
};
const CanvasRefForwardingFunction = ({ width, height, fit, className, style }, ref) => {
const canvasRef = (0, react_1.useRef)(null);
const draw = (0, react_1.useCallback)((imageData) => {
var _a;
const canvas = canvasRef.current;
const canvasWidth = width !== null && width !== void 0 ? width : imageData.displayWidth;
const canvasHeight = height !== null && height !== void 0 ? height : imageData.displayHeight;
if (!canvas) {
throw new Error('Canvas ref is not set');
}
const ctx = (_a = canvasRef.current) === null || _a === void 0 ? void 0 : _a.getContext('2d');
if (!ctx) {
throw new Error('Could not get 2d context');
}
canvas.width = canvasWidth;
canvas.height = canvasHeight;
ctx.drawImage(imageData, ...calcArgs(fit, {
height: imageData.displayHeight,
width: imageData.displayWidth,
}, {
width: canvasWidth,
height: canvasHeight,
}));
}, [fit, height, width]);
(0, react_1.useImperativeHandle)(ref, () => {
return {
draw,
getCanvas: () => {
if (!canvasRef.current) {
throw new Error('Canvas ref is not set');
}
return canvasRef.current;
},
clear: () => {
var _a;
const ctx = (_a = canvasRef.current) === null || _a === void 0 ? void 0 : _a.getContext('2d');
if (!ctx) {
throw new Error('Could not get 2d context');
}
ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
},
};
}, [draw]);
return (0, jsx_runtime_1.jsx)("canvas", { ref: canvasRef, className: className, style: style });
};
exports.Canvas = react_1.default.forwardRef(CanvasRefForwardingFunction);