UNPKG

remotion

Version:

Make videos programmatically

107 lines (106 loc) 4.53 kB
"use strict"; 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 calculate_image_fit_js_1 = require("../calculate-image-fit.js"); const run_effect_chain_js_1 = require("../effects/run-effect-chain.js"); const use_effect_chain_state_js_1 = require("../effects/use-effect-chain-state.js"); const CanvasRefForwardingFunction = ({ width, height, fit, className, style, effects }, ref) => { const canvasRef = (0, react_1.useRef)(null); const chainState = (0, use_effect_chain_state_js_1.useEffectChainState)(); const sourceCanvas = (0, react_1.useMemo)(() => { if (typeof document === 'undefined') { return null; } return document.createElement('canvas'); }, []); const draw = (0, react_1.useCallback)((imageData) => { 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'); } if (!sourceCanvas) { throw new Error('Source canvas is not available'); } sourceCanvas.width = canvasWidth; sourceCanvas.height = canvasHeight; const sourceCtx = sourceCanvas.getContext('2d'); if (!sourceCtx) { throw new Error('Could not get 2d context for source canvas'); } sourceCtx.drawImage(imageData, ...(0, calculate_image_fit_js_1.calculateImageFit)(fit, { height: imageData.displayHeight, width: imageData.displayWidth, }, { width: canvasWidth, height: canvasHeight, })); canvas.width = canvasWidth; canvas.height = canvasHeight; return (0, run_effect_chain_js_1.runEffectChain)({ state: chainState.get(canvasWidth, canvasHeight), source: sourceCanvas, effects, output: canvas, width: canvasWidth, height: canvasHeight, }); }, [chainState, effects, fit, height, sourceCanvas, 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);