123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- import { useMarkStore } from "@/store";
- import { PictureSlice, Task } from "@/types";
- import { loadImage } from "@/utils/utils";
- // 存放裁切图的ObjectUrls
- let objectUrlMap = new Map<string, string>();
- const OBJECT_URLS_MAP_MAX_SIZE =
- window.APP_OPTIONS?.OBJECT_URLS_MAP_MAX_SIZE ?? 100;
- // 清理缓存的过时数据(清除头10张),First in first out
- function cacheFIFO() {
- if (objectUrlMap.size > OBJECT_URLS_MAP_MAX_SIZE) {
- const ary = [...objectUrlMap.entries()];
- const toRelease = ary.splice(0, 10);
- // 为了避免部分图片还没显示就被revoke了,这里做一个延迟revoke
- // 此处有个瑕疵,缩略图的显示与试卷不是同时显示,是有可能被清除了的,只能让用户刷新了。 => 见下面的fix
- for (const u of toRelease) {
- // 如果当前图片仍在引用 objectUrl , 则将其放入缓存中
- if (document.querySelector(`img[src="${u[1]}"]`)) {
- ary.push(u);
- } else {
- URL.revokeObjectURL(u[1]);
- }
- }
- objectUrlMap = new Map(ary);
- }
- }
- export default function useDraw() {
- const markStore = useMarkStore();
- async function getDataUrlForSliceConfig(
- image: HTMLImageElement,
- sliceConfig: PictureSlice,
- maxSliceWidth: number,
- urlForCache: string
- ) {
- const { i, x, y, w, h } = sliceConfig;
- const key = `${urlForCache}-${i}-${x}-${y}-${w}-${h}`;
- if (objectUrlMap.get(key)) {
- // console.log("cached slice objectUrl");
- return objectUrlMap.get(key);
- }
- const canvas = document.createElement("canvas");
- canvas.width = Math.max(sliceConfig.w, maxSliceWidth);
- canvas.height = sliceConfig.h;
- const ctx = canvas.getContext("2d");
- if (!ctx) {
- console.log('canvas.getContext("2d") error');
- throw new Error("canvas ctx error");
- }
- // drawImage 画图软件透明色
- ctx.drawImage(
- image,
- sliceConfig.x,
- sliceConfig.y,
- sliceConfig.w,
- sliceConfig.h,
- 0,
- 0,
- sliceConfig.w,
- sliceConfig.h
- );
- // console.log(image, canvas.height, sliceConfig, ctx);
- // console.log(canvas.toDataURL());
- // 如果用toBlob,则产生异步,而且URL.createObjectURL还会需要手动释放
- // const dataurl = canvas.toDataURL();
- const blob: Blob = await new Promise((res) => {
- canvas.toBlob((b) => res(b));
- });
- const dataurl = URL.createObjectURL(blob);
- cacheFIFO();
- objectUrlMap.set(key, dataurl);
- return dataurl;
- }
- async function getDataUrlForSplitConfig(
- image: HTMLImageElement,
- config: [number, number],
- maxSliceWidth: number,
- urlForCache: string
- ) {
- const [start, end] = config;
- const key = `${urlForCache}-${start}-${end}`;
- if (objectUrlMap.get(key)) {
- console.log("cached split objectUrl");
- return objectUrlMap.get(key);
- }
- const width = image.naturalWidth * (end - start);
- const canvas = document.createElement("canvas");
- canvas.width = Math.max(width, maxSliceWidth);
- canvas.height = image.naturalHeight;
- const ctx = canvas.getContext("2d");
- if (!ctx) {
- console.log('canvas.getContext("2d") error');
- throw new Error("canvas ctx error");
- }
- // drawImage 画图软件透明色
- ctx.drawImage(
- image,
- image.naturalWidth * start,
- 0,
- image.naturalWidth * end,
- image.naturalHeight,
- 0,
- 0,
- image.naturalWidth * end,
- image.naturalHeight
- );
- // 如果用toBlob,则产生异步,而且URL.createObjectURL还会需要手动释放
- // const dataurl = canvas.toDataURL();
- const blob: Blob = await new Promise((res) => {
- canvas.toBlob((b) => res(b));
- });
- const dataurl = URL.createObjectURL(blob);
- cacheFIFO();
- objectUrlMap.set(key, dataurl);
- return dataurl;
- }
- async function getDataUrlForCoverConfig(
- image: HTMLImageElement,
- configs: PictureSlice[]
- ) {
- const key = `${image.src}-slice`;
- if (objectUrlMap.get(key)) {
- return objectUrlMap.get(key);
- }
- const canvas = document.createElement("canvas");
- canvas.width = image.naturalWidth;
- canvas.height = image.naturalHeight;
- const ctx = canvas.getContext("2d");
- if (!ctx) {
- console.log('canvas.getContext("2d") error');
- throw new Error("canvas ctx error");
- }
- ctx.drawImage(image, 0, 0);
- ctx.fillStyle = "#ffffff";
- configs.forEach((config) => {
- ctx.fillRect(config.x, config.y, config.w, config.h);
- });
- const blob: Blob = await new Promise((res) => {
- canvas.toBlob((b) => res(b));
- });
- const dataurl = URL.createObjectURL(blob);
- cacheFIFO();
- objectUrlMap.set(key, dataurl);
- return dataurl;
- }
- async function preDrawImage(_currentTask: Task | undefined) {
- // console.log("preDrawImage=>curTask:", _currentTask);
- if (!_currentTask) return;
- let maxSliceWidth = 0; // 最大的裁切块宽度,图片容器以此为准
- const hasSliceConfig = _currentTask?.sliceConfig?.length;
- const images = [];
- if (hasSliceConfig) {
- // 必须要先加载一遍,把“选择整图”的宽高重置后,再算总高度
- const sliceNum = _currentTask.sliceUrls.length;
- if (_currentTask.sliceConfig.some((v) => v.i > sliceNum)) {
- console.warn("裁切图设置的数量小于该学生的总图片数量");
- }
- _currentTask.sliceConfig = _currentTask.sliceConfig.filter(
- (v) => v.i <= sliceNum
- );
- for (const sliceConfig of _currentTask.sliceConfig) {
- const url = _currentTask.sliceUrls[sliceConfig.i - 1];
- const image = await loadImage(url);
- images[sliceConfig.i] = image;
- const { x, y, w, h } = sliceConfig;
- x < 0 && (sliceConfig.x = 0);
- y < 0 && (sliceConfig.y = 0);
- if (sliceConfig.w === 0 && sliceConfig.h === 0) {
- // 选择整图时,w/h 为0
- sliceConfig.w = image.naturalWidth;
- sliceConfig.h = image.naturalHeight;
- }
- if (x <= 1 && y <= 1 && sliceConfig.w <= 1 && sliceConfig.h <= 1) {
- sliceConfig.x = image.naturalWidth * x;
- sliceConfig.y = image.naturalHeight * y;
- sliceConfig.w = image.naturalWidth * w;
- sliceConfig.h = image.naturalHeight * h;
- }
- }
- maxSliceWidth = Math.max(..._currentTask.sliceConfig.map((v) => v.w));
- // 用来保存sliceImage在整个图片容器中(不包括image-seperator)的高度范围
- for (const sliceConfig of _currentTask.sliceConfig) {
- const url = _currentTask.sliceUrls[sliceConfig.i - 1];
- const image = images[sliceConfig.i];
- try {
- await getDataUrlForSliceConfig(
- image,
- sliceConfig,
- maxSliceWidth,
- url
- );
- } catch (error) {
- console.log("preDrawImage failed: ", error);
- }
- }
- } else {
- for (const url of _currentTask.sliceUrls) {
- const image = await loadImage(url);
- images.push(image);
- }
- const splitConfigPairs = markStore.setting.splitConfig.reduce<
- [number, number][]
- >((a, v, index) => {
- index % 2 === 0 ? a.push([v, -1]) : (a.at(-1)![1] = v);
- return a;
- }, []);
- const maxSplitConfig = Math.max(...markStore.setting.splitConfig);
- maxSliceWidth =
- Math.max(...images.map((v) => v.naturalWidth)) * maxSplitConfig;
- for (const url of _currentTask.sliceUrls) {
- for (const config of splitConfigPairs) {
- const indexInSliceUrls = _currentTask.sliceUrls.indexOf(url) + 1;
- const image = images[indexInSliceUrls - 1];
- try {
- await getDataUrlForSplitConfig(image, config, maxSliceWidth, url);
- } catch (error) {
- console.log("preDrawImage failed: ", error);
- }
- }
- }
- }
- }
- async function processSliceUrls(_currentTask: Task | undefined) {
- if (!_currentTask) return;
- const getNum = (num) => Math.max(Math.min(1, num), 0);
- const sheetUrls = _currentTask.sheetUrls || [];
- const sheetConfig = (markStore.setting.sheetConfig || []).map((item) => {
- return { ...item };
- });
- const urls = [];
- for (let i = 0; i < sheetUrls.length; i++) {
- const url = sheetUrls[i];
- const configs = sheetConfig.filter((item) => item.i === i + 1);
- if (!configs.length) {
- urls[i] = url;
- continue;
- }
- const image = await loadImage(url);
- configs.forEach((item) => {
- item.x = image.naturalWidth * getNum(item.x);
- item.y = image.naturalHeight * getNum(item.y);
- item.w = image.naturalWidth * getNum(item.w);
- item.h = image.naturalHeight * getNum(item.h);
- });
- urls[i] = await getDataUrlForCoverConfig(image, configs);
- }
- return urls;
- }
- async function preDrawImageHistory(_currentTask: Task | undefined) {
- // console.log("preDrawImageHistory=>curTask:", _currentTask);
- if (!_currentTask) return;
- let maxSliceWidth = 0; // 最大的裁切块宽度,图片容器以此为准
- const hasSliceConfig = _currentTask.sliceConfig?.length;
- // _currentTask.sheetUrls = ["/1-1.jpg", "/1-2.jpg"];
- _currentTask.sliceUrls = await processSliceUrls(_currentTask);
- const images = [];
- if (hasSliceConfig) {
- // 必须要先加载一遍,把“选择整图”的宽高重置后,再算总高度
- const sliceNum = _currentTask.sliceUrls.length;
- if (_currentTask.sliceConfig.some((v) => v.i > sliceNum)) {
- console.warn("裁切图设置的数量小于该学生的总图片数量");
- }
- _currentTask.sliceConfig = _currentTask.sliceConfig.filter(
- (v) => v.i <= sliceNum
- );
- for (const sliceConfig of _currentTask.sliceConfig) {
- const url = _currentTask.sliceUrls[sliceConfig.i - 1];
- const image = await loadImage(url);
- images[sliceConfig.i] = image;
- const { x, y, w, h } = sliceConfig;
- x < 0 && (sliceConfig.x = 0);
- y < 0 && (sliceConfig.y = 0);
- if (sliceConfig.w === 0 && sliceConfig.h === 0) {
- // 选择整图时,w/h 为0
- sliceConfig.w = image.naturalWidth;
- sliceConfig.h = image.naturalHeight;
- }
- if (x <= 1 && y <= 1 && sliceConfig.w <= 1 && sliceConfig.h <= 1) {
- sliceConfig.x = image.naturalWidth * x;
- sliceConfig.y = image.naturalHeight * y;
- sliceConfig.w = image.naturalWidth * w;
- sliceConfig.h = image.naturalHeight * h;
- }
- }
- maxSliceWidth = Math.max(..._currentTask.sliceConfig.map((v) => v.w));
- // 用来保存sliceImage在整个图片容器中(不包括image-seperator)的高度范围
- for (const sliceConfig of _currentTask.sliceConfig) {
- const url = _currentTask.sliceUrls[sliceConfig.i - 1];
- const image = images[sliceConfig.i];
- try {
- await getDataUrlForSliceConfig(
- image,
- sliceConfig,
- maxSliceWidth,
- url
- );
- } catch (error) {
- console.log("preDrawImage failed: ", error);
- }
- }
- } else {
- for (const url of _currentTask.sliceUrls) {
- const image = await loadImage(url);
- images.push(image);
- }
- const splitConfigPairs = markStore.setting.splitConfig.reduce<
- [number, number][]
- >((a, v, index) => {
- index % 2 === 0 ? a.push([v, -1]) : (a.at(-1)![1] = v);
- return a;
- }, []);
- const maxSplitConfig = Math.max(...markStore.setting.splitConfig);
- maxSliceWidth =
- Math.max(...images.map((v) => v.naturalWidth)) * maxSplitConfig;
- for (const url of _currentTask.sliceUrls) {
- for (const config of splitConfigPairs) {
- const indexInSliceUrls = _currentTask.sliceUrls.indexOf(url) + 1;
- const image = images[indexInSliceUrls - 1];
- try {
- await getDataUrlForSplitConfig(image, config, maxSliceWidth, url);
- } catch (error) {
- console.log("preDrawImage failed: ", error);
- }
- }
- }
- }
- }
- return {
- getDataUrlForSliceConfig,
- getDataUrlForSplitConfig,
- getDataUrlForCoverConfig,
- preDrawImage,
- processSliceUrls,
- preDrawImageHistory,
- };
- }
|