From c7b33fe92cf6d4449ca5919353c15611ae9ad410 Mon Sep 17 00:00:00 2001 From: user1 <10901603+no-distractions1@user.noreply.gitee.com> Date: 星期五, 28 六月 2024 09:40:32 +0800 Subject: [PATCH] Merge branch 'master' of http://182.92.203.7:2001/r/testbookLayout --- src/components/graffiti/index.vue | 79 +++++++++++++++++++++++++++++++++------ 1 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/components/graffiti/index.vue b/src/components/graffiti/index.vue index de1beef..11c1519 100644 --- a/src/components/graffiti/index.vue +++ b/src/components/graffiti/index.vue @@ -1,3 +1,4 @@ +<!-- 娑傝壊杩炵嚎棰樻帶浠� --> <template> <div class="page"> <div class="main"> @@ -5,7 +6,6 @@ <canvas id="canvas" :style="{ - backgroundImage: `url(${backgroundImage})`, backgroundSize: 'cover', backgroundPosition: 'center', }" @@ -38,7 +38,7 @@ return { canvas: null, context: null, - painting: false, + painting: false, // 璁板綍鐘舵�侊紝榧犳爣鏄惁鍦ㄦ寜涓嬬姸鎬� historyData: [], // 瀛樺偍鍘嗗彶鏁版嵁锛岀敤浜庢挙閿� brushSize: 5, // 绗斿埛澶у皬 brushColor: "#000000", // 绗斿埛棰滆壊 @@ -68,18 +68,39 @@ this.canvas.addEventListener("mouseleave", this.closePaint); } this.toolClear(); - const oldData = localStorage.getItem('graffiti-data') + const oldData = localStorage.getItem(this.config.activeBook.name + '-graffiti-' + this.page) if(oldData) { this.backgroundImage = oldData } }, + // watch:{ + // backgroundImage:{ + // handler(newValue) { + // if(newValue && this.context) { + // const imgData = new Image(); + // // 鍒嗕袱绉嶆儏鍐碉紝鍒濇鐨勫浘鐗� 鐩存帴鐢ㄥ閮ㄩ摼鎺ワ紝闇�瑕佸姞璺ㄥ煙鍜屾椂闂存埑锛屼簩娆′繚瀛樼殑浣滀负鑳屾櫙鍒欎笉闇�瑕� + // const oldData = localStorage.getItem(this.config.activeBook.name + '-graffiti-' + this.page) + // if(oldData) { + // imgData.src = newValue + // } else { + // imgData.src = newValue + '?' + new Date().getTime(); + // imgData.setAttribute('crossOrigin', ''); + // } + // imgData.onload = () => { + // this.context.drawImage(imgData,0,0) + // } + // } + // } + // } + // }, methods: { changeBackground(imgUrl) { this.backgroundImage = imgUrl; }, + // 鍒濆鍖� 鐢诲竷锛岃缃ぇ灏忚儗鏅壊 initCanvas() { const that = this - function resetCanvas() { + const resetCanvas = () => { const elPanel = document.getElementById("canvas_panel"); that.canvas.width = elPanel.clientWidth; that.canvas.height = elPanel.clientHeight; @@ -90,9 +111,22 @@ that.context.fillRect(0, 0, that.canvas.width, that.canvas.height); that.context.fillStyle = "black"; that.getCanvasOffset(); // 鏇存柊鐢诲竷浣嶇疆 + // 璁剧疆鐢诲竷鑳屾櫙鍥� + const imgData = new Image(); + // 鍒嗕袱绉嶆儏鍐碉紝鍒濇鐨勫浘鐗� 鐩存帴鐢ㄥ閮ㄩ摼鎺ワ紝闇�瑕佸姞璺ㄥ煙鍜屾椂闂存埑锛屼簩娆′繚瀛樼殑浣滀负鑳屾櫙鍒欎笉闇�瑕� + const oldData = localStorage.getItem(this.config.activeBook.name + '-graffiti-' + this.page) + if(oldData) { + imgData.src = oldData + } else { + imgData.src = this.backgroundImage + '?' + new Date().getTime(); + imgData.setAttribute('crossOrigin', ''); + } + imgData.onload = () => { + this.context.drawImage(imgData,0,0) + } } - resetCanvas(); + // 鐩戝惉绐楀彛澶у皬 锛岀獥鍙f敼鍙橀噸鏂版覆鏌撶敾甯� window.addEventListener("resize", resetCanvas); }, @@ -110,7 +144,7 @@ y: y - this.canvasOffset.top, }; }, - + // 榧犳爣鎶捣鏂规硶 downCallback(event) { // 鍏堜繚瀛樹箣鍓嶇殑鏁版嵁锛岀敤浜庢挙閿�鏃舵仮澶嶏紙缁樺埗鍓嶄繚瀛橈紝涓嶆槸缁樺埗鍚庡啀淇濆瓨锛� const data = this.context.getImageData( @@ -129,7 +163,7 @@ this.brushTool === "eraser" ? "#FFFFFF" : this.brushColor; this.painting = true; }, - + // 榧犳爣绉诲姩鏂规硶(璁$畻鍧愭爣骞舵覆鏌撹建杩�) moveCallback(event) { if (!this.painting) { return; @@ -142,19 +176,21 @@ closePaint() { this.painting = false; }, + // 閲嶆柊璁$畻鐢诲竷鐨勫亸绉诲�� updateCanvasOffset() { - this.getCanvasOffset(); // 閲嶆柊璁$畻鐢诲竷鐨勫亸绉诲�� + this.getCanvasOffset(); }, - + // 鏀瑰彉绗斿埛澶у皬 onChangeSize(size) { this.brushSize = size; }, + // 鏀瑰彉绗斿埛棰滆壊 onChangeColor(color) { this.brushColor = color; }, + // 淇濆瓨锛屾竻绌虹瓑鎸夐挳 onChangeTool(tool) { this.brushTool = tool; - console.log(tool); switch (tool) { case "clear": this.toolClear(); @@ -167,16 +203,30 @@ break; } }, + // 娓呯┖canvas鎵�鏈夊唴瀹�(鑳屾櫙鍥鹃櫎澶�) toolClear() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); this.resetToolActive(); + // 鏂板姞鍐呭锛屾竻绌哄悗灏嗚儗鏅浘鍐嶈缃笂鍘� + const imgData = new Image(); + // 鍒嗕袱绉嶆儏鍐碉紝鍒濇鐨勫浘鐗� 鐩存帴鐢ㄥ閮ㄩ摼鎺ワ紝闇�瑕佸姞璺ㄥ煙鍜屾椂闂存埑锛屼簩娆′繚瀛樼殑浣滀负鑳屾櫙鍒欎笉闇�瑕� + const oldData = localStorage.getItem(this.config.activeBook.name + '-graffiti-' + this.page) + if(oldData) { + imgData.src = oldData + } else { + imgData.src = this.backgroundImage + '?' + new Date().getTime(); + imgData.setAttribute('crossOrigin', ''); + } + imgData.onload = () => { + this.context.drawImage(imgData,0,0) + } }, - // 鍘熶繚瀛樻柟娉� + // 淇濆瓨鐢诲竷鑳屾櫙鍜屽垝绾垮埌鏈湴鏂规硶 toolSave() { var imgData = this.canvas.toDataURL({format: 'png', quality:1, width:800,}); - localStorage.setItem('graffiti-data',imgData) + localStorage.setItem(this.config.activeBook.name + '-graffiti-' + this.page,imgData) }, - // 淇濆瓨鏂规硶锛屼繚瀛樹负涓�寮犲浘鐗囧苟涓嬭浇 + // 淇濆瓨涓轰竴寮犲浘鐗囧苟涓嬭浇鐨勬柟娉� saveImgData() { var link = document.createElement("a"); var imgData = this.canvas.toDataURL({format: 'png', quality:1, width:20000, height:4000}); @@ -196,11 +246,14 @@ return new Blob([u8arr], {type:mime}); } }, + // 杩斿洖涓婁竴姝ユ柟娉�(鎾ら攢) toolUndo() { + // if (this.historyData.length <= 0) { this.resetToolActive(); return; } + // 灏嗙敾鐨勪笂涓�姝ユ暟鎹啓鍏anvas 閲嶆柊娓叉煋 const lastIndex = this.historyData.length - 1; this.context.putImageData(this.historyData[lastIndex], 0, 0); this.historyData.pop(); -- Gitblit v1.9.1