From 4e2ac7c92e57f9e34596d9e01453834debd0a6b2 Mon Sep 17 00:00:00 2001 From: zhangxuelin <959231531@qq.com> Date: Fri, 26 Jul 2024 10:26:34 +0800 Subject: [PATCH] =?UTF-8?q?pdf=E7=9A=84=E5=B1=95=E7=A4=BA=20=E7=BF=BB?= =?UTF-8?q?=E9=A1=B5=20=E5=8F=8A=E6=95=B0=E6=8D=AE=E5=9B=9E=E6=98=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/src/components/pdf/index.vue | 266 +++++++++++++----- .../src/plugins/fabric/event/clickEvent.js | 6 + src/renderer/src/utils/pdfAndFabric.js | 129 +++++++++ src/renderer/src/views/classBegins/index.vue | 173 ++++++------ 4 files changed, 418 insertions(+), 156 deletions(-) create mode 100644 src/renderer/src/utils/pdfAndFabric.js diff --git a/src/renderer/src/components/pdf/index.vue b/src/renderer/src/components/pdf/index.vue index ce95200..15c1821 100644 --- a/src/renderer/src/components/pdf/index.vue +++ b/src/renderer/src/components/pdf/index.vue @@ -1,97 +1,213 @@ \ No newline at end of file diff --git a/src/renderer/src/plugins/fabric/event/clickEvent.js b/src/renderer/src/plugins/fabric/event/clickEvent.js index 98c5816..1bc085f 100644 --- a/src/renderer/src/plugins/fabric/event/clickEvent.js +++ b/src/renderer/src/plugins/fabric/event/clickEvent.js @@ -28,6 +28,8 @@ export class CanvasClickEvent { // 事件:按下鼠标 canvas?.on('mouse:down', (e) => { + console.log(222222222222222222222222222) + this.isMouseDown = true if (this.isSpaceKeyDown) { return @@ -148,6 +150,8 @@ export class CanvasClickEvent { // 事件:移动鼠标 canvas?.on('mouse:move', (e) => { + console.log(222222222222222222222222222) + if (this.isMouseDown) { // Press space, drag the canvas, stop drawing. if (this.isSpaceKeyDown) { @@ -180,6 +184,8 @@ export class CanvasClickEvent { // 事件:松开鼠标 canvas?.on('mouse:up', (e) => { + console.log(222222222222222222222222222) + this.isMouseDown = false if (this.autoDrawInk?.[0]?.length > 3) { autoDrawData.addInk([...this.autoDrawInk]) diff --git a/src/renderer/src/utils/pdfAndFabric.js b/src/renderer/src/utils/pdfAndFabric.js new file mode 100644 index 0000000..f4b0ca9 --- /dev/null +++ b/src/renderer/src/utils/pdfAndFabric.js @@ -0,0 +1,129 @@ +// 所有事件 +export function handleevent(canvas, imgarr, type = 'defalut') { + // 鼠标按下 + canvas.on('mouse:down', function (e) {}) + // // 监听鼠标移动事件 + // canvas.on('mouse:move', (options) => { + // console.log('Mouse move event:', options); + // }); + + // 监听鼠标释放事件 + canvas.on('mouse:up', (options) => { + //判断是点击的哪一个 + if (type == 'defalut') { + if (imgarr.value[0].index == 0) { + imgarr.value[0].JSONdata = canvas.toJSON() + } + if (imgarr.value[1].index == 0) { + imgarr.value[1].JSONdata = canvas.toJSON() + } + } else { + if (imgarr.value[0].index == 1) { + imgarr.value[0].JSONdata = canvas.toJSON() + } + if (imgarr.value[1].index == 1) { + imgarr.value[1].JSONdata = canvas.toJSON() + } + } + }) +} +// 保存数据 +export function savecanvsStore(imgarr, canvsStore) { + canvsStore.pageArr = mergeAndReplace(canvsStore.pageArr, imgarr.value) + // console.log(canvsStore.pageArr,22222222222222222222+'存入') +} +// 重显数据 +export function displayData(canvas, canvsStore, canvasobj, fabric, img) { + // 初始化 + if (!canvsStore.pageArr.length) { + fabric.Image.fromURL(img.src, (img) => { + img.set({ + left: 0, + top: 0, + scaleX: canvas.value.width / img.width, + scaleY: canvas.value.height / img.height + }) + canvas.value.setBackgroundImage(img, canvas.value.renderAll.bind(canvas.value)) + }) + return + } + canvsStore.pageArr.forEach((item) => { + //初始化 + if (item.page == canvasobj.page) { + canvas.value.clear() // 清除 Canvas + // console.log(item.JSONdata, '找到一样的数据') + canvas.value.loadFromJSON(item.JSONdata, () => { + // 在所有对象加载完成后重新渲染画布 + requestAnimationFrame(() => { + // 渲染所有对象 + canvas.value.renderAll.bind(canvas.value) + canvas.value.renderAll() + }) + }) + } else { + // 使用 requestAnimationFrame 来更新画布,确保在下一帧进行重绘 + canvas.value.clear() // 清除 Canvas + requestAnimationFrame(function () { + fabric.Image.fromURL(img.src, (img) => { + img.set({ + left: 0, + top: 0, + scaleX: canvas.value.width / img.width, + scaleY: canvas.value.height / img.height + }) + canvas.value.setBackgroundImage(img, canvas.value.renderAll.bind(canvas.value)) + }) + // 渲染所有对象 + canvas.value.renderAll.bind(canvas.value) + }) + } + }) +} + +//page 一样替换 +const mergeAndReplace = (arr1, arr2) => { + // // 用于存储替换后的数组 + // const resultArray = array1.map(item1 => { + // // 在 array2 中查找 page 相同的对象 + // const replacement = array2.find(item2 => item2.page == item1.page); + // // 如果找到替换对象,则返回替换对象,否则返回原对象 + // return replacement ? replacement : item1; + // }); + + // // 将 array2 中 page 不在 array1 中的对象追加到结果数组中 + // array2.forEach(item2 => { + // const existsInArray1 = array1.some(item1 => item1.page == item2.page); + // if (!existsInArray1) { + // resultArray.push(item2); + // } + // }); + + // return resultArray; + + // 创建一个映射,将 arr2 中的对象按 page 属性存储 + let map = new Map(arr2.map((item) => [item.page, item])) + + // 使用 map 替换 arr1 中相应 page 的对象,并添加 arr2 中的对象 + arr1 = arr1.map((item) => (map.has(item.page) ? map.get(item.page) : item)) + + // 将 map 中存在但 arr1 中不存在的对象添加到 arr1 + for (let [page, obj] of map) { + if (!arr1.some((item) => item.page === page)) { + arr1.push(obj) + } + } + return arr1 +} + +// 初始化数据 +export function initcanvasdata(canvas) { + canvas.value.clear() // 清除 Canvas + // 设置画布的背景色或其他属性 + canvas.value.backgroundColor = 'rgba(255, 255, 255, 1)' // 白色背景 + + // 使用 requestAnimationFrame 来更新画布,确保在下一帧进行重绘 + requestAnimationFrame(function () { + // 渲染所有对象 + canvas.value.renderAll.bind(canvas.value) + }) +} diff --git a/src/renderer/src/views/classBegins/index.vue b/src/renderer/src/views/classBegins/index.vue index d80c99a..26e226d 100644 --- a/src/renderer/src/views/classBegins/index.vue +++ b/src/renderer/src/views/classBegins/index.vue @@ -1,78 +1,89 @@