| | |
| | | </div> |
| | | </div> |
| | | <div class="footer"> |
| | | <BrushSize :size="brushSize" @change-size="onChangeSize" /> |
| | | <ColorPicker :color="brushColor" @change-color="onChangeColor" /> |
| | | <BrushSize :size="brushSize" @change-size="onChangeSize" @change-color="onChangeColor" /> |
| | | <ToolBtns :tool="brushTool" @change-tool="onChangeTool" /> |
| | | </div> |
| | | </div> |
| | |
| | | |
| | | <script> |
| | | import BrushSize from "./components/brushSize.vue"; |
| | | import ColorPicker from "./components/colorPicker.vue"; |
| | | import ToolBtns from "./components/toolBtns.vue"; |
| | | export default { |
| | | name: "graffiti", |
| | | components: { BrushSize, ColorPicker, ToolBtns }, |
| | | components: { BrushSize, ToolBtns }, |
| | | props:{ |
| | | page:{ |
| | | type:Number |
| | | }, |
| | | bcImg:{ |
| | | type:String |
| | | } |
| | | }, |
| | | data() { |
| | | return { |
| | | canvas: null, |
| | |
| | | top: 0, |
| | | }, |
| | | backgroundImage: |
| | | "https://t7.baidu.com/it/u=1819248061,230866778&fm=193&f=GIF", |
| | | "", |
| | | }; |
| | | }, |
| | | mounted() { |
| | | this.backgroundImage = this.bcImg |
| | | this.canvas = document.getElementById("canvas"); |
| | | if (this.canvas.getContext) { |
| | | this.context = this.canvas.getContext("2d", { willReadFrequently: true }); |
| | | this.initCanvas(); |
| | | // window.addEventListener('resize', updateCanvasPosition); |
| | | window.addEventListener("scroll", this.updateCanvasOffset); // 添加滚动条滚动事件监听器 |
| | | window.addEventListener("scroll",this.updateCanvasOffset,true); // 添加滚动条滚动事件监听器 |
| | | this.getCanvasOffset(); |
| | | this.context.lineGap = "round"; |
| | | this.context.lineJoin = "round"; |
| | |
| | | this.canvas.addEventListener("mouseleave", this.closePaint); |
| | | } |
| | | this.toolClear(); |
| | | const oldData = localStorage.getItem('graffiti-data') |
| | | if(oldData) { |
| | | this.backgroundImage = oldData |
| | | } |
| | | }, |
| | | methods: { |
| | | changeBackground(imgUrl) { |
| | |
| | | }, |
| | | onChangeTool(tool) { |
| | | this.brushTool = tool; |
| | | console.log(tool); |
| | | switch (tool) { |
| | | case "clear": |
| | | this.toolClear(); |
| | |
| | | this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); |
| | | this.resetToolActive(); |
| | | }, |
| | | // 原保存方法 |
| | | toolSave() { |
| | | const imageDataUrl = this.canvas.toDataURL("image/png"); |
| | | console.log(imageDataUrl); |
| | | // const imgUrl = canvas.toDataURL('image/png'); |
| | | // const el = document.createElement('a'); |
| | | // el.setAttribute('href', imgUrl); |
| | | // el.setAttribute('target', '_blank'); |
| | | // el.setAttribute('download', `graffiti-${Date.now()}`); |
| | | // document.body.appendChild(el); |
| | | // el.click(); |
| | | // document.body.removeChild(el); |
| | | // resetToolActive(); |
| | | var imgData = this.canvas.toDataURL({format: 'png', quality:1, width:800,}); |
| | | localStorage.setItem('graffiti-data',imgData) |
| | | }, |
| | | // 保存方法,保存为一张图片并下载 |
| | | saveImgData() { |
| | | var link = document.createElement("a"); |
| | | var imgData = this.canvas.toDataURL({format: 'png', quality:1, width:20000, height:4000}); |
| | | var strDataURI = imgData.substr(22, imgData.length); |
| | | var blob = dataURLtoBlob(imgData); |
| | | var objurl = URL.createObjectURL(blob); |
| | | link.download = "grid1.png"; |
| | | link.href = objurl; |
| | | link.click(); |
| | | |
| | | function dataURLtoBlob(dataurl) { |
| | | var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], |
| | | bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); |
| | | while(n--){ |
| | | u8arr[n] = bstr.charCodeAt(n); |
| | | } |
| | | return new Blob([u8arr], {type:mime}); |
| | | } |
| | | }, |
| | | toolUndo() { |
| | | if (this.historyData.length <= 0) { |
| | |
| | | const lastIndex = this.historyData.length - 1; |
| | | this.context.putImageData(this.historyData[lastIndex], 0, 0); |
| | | this.historyData.pop(); |
| | | |
| | | this.resetToolActive(); |
| | | }, |
| | | // 存储数据 |
| | | saveData(data) { |
| | | this.historyData.length >= 50 && this.historyData.shift(); // 设置储存上限为50步 |
| | | this.historyData.push(data); |
| | | console.log('数据',this.historyData); |
| | | }, |
| | | // 清除、撤销、保存状态不需要保持,操作完后恢复笔刷状态 |
| | | resetToolActive() { |
| | |
| | | .page { |
| | | display: flex; |
| | | flex-direction: column; |
| | | width: 1038px; |
| | | width:100%; |
| | | height: 866px; |
| | | } |
| | | |