2023-04-06 02:48:32 +08:00
|
|
|
import React, { createContext, useRef, useContext, useCallback } from 'react';
|
2023-04-07 13:21:20 +08:00
|
|
|
import html2canvas from 'html2canvas';
|
2023-04-06 02:48:32 +08:00
|
|
|
|
|
|
|
|
const ScreenshotContext = createContext({});
|
|
|
|
|
|
|
|
|
|
export const useScreenshot = () => {
|
|
|
|
|
const { ref } = useContext(ScreenshotContext);
|
2023-04-07 13:21:20 +08:00
|
|
|
|
|
|
|
|
const takeScreenShot = node => {
|
|
|
|
|
if (!node) {
|
|
|
|
|
throw new Error('You should provide correct html node.');
|
|
|
|
|
}
|
|
|
|
|
return html2canvas(node).then(canvas => {
|
|
|
|
|
const croppedCanvas = document.createElement('canvas');
|
|
|
|
|
const croppedCanvasContext = croppedCanvas.getContext('2d');
|
|
|
|
|
// init data
|
|
|
|
|
const cropPositionTop = 0;
|
|
|
|
|
const cropPositionLeft = 0;
|
|
|
|
|
const cropWidth = canvas.width;
|
|
|
|
|
const cropHeight = canvas.height;
|
|
|
|
|
|
|
|
|
|
croppedCanvas.width = cropWidth;
|
|
|
|
|
croppedCanvas.height = cropHeight;
|
|
|
|
|
|
|
|
|
|
croppedCanvasContext.drawImage(canvas, cropPositionLeft, cropPositionTop);
|
|
|
|
|
|
|
|
|
|
const base64Image = croppedCanvas.toDataURL('image/png', 1);
|
|
|
|
|
|
|
|
|
|
return base64Image;
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-04-06 02:48:32 +08:00
|
|
|
|
|
|
|
|
const captureScreenshot = () => {
|
2023-04-07 13:21:20 +08:00
|
|
|
return takeScreenShot(ref.current);
|
2023-04-06 02:48:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { screenshotTargetRef: ref, captureScreenshot };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ScreenshotProvider = ({ children }) => {
|
|
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
|
|
|
|
return <ScreenshotContext.Provider value={{ ref }}>{children}</ScreenshotContext.Provider>;
|
|
|
|
|
};
|