| | |
| | | <template> |
| | | <div class="paint"> |
| | | <canvas id="canvas" width="400" :height="canvasHeight"></canvas> |
| | | <canvas id="canvas" :width="canvasWidth" :height="canvasHeight"></canvas> |
| | | <!-- 操作按钮 --> |
| | | <ul class="paint-btn"> |
| | | <li class="paint-btn-box"> |
| | |
| | | </button> |
| | | <button @click="saveImgData">保存</button> |
| | | </li> |
| | | <li> |
| | | </li> |
| | | <li></li> |
| | | <li> |
| | | <label>画笔:</label> |
| | | <select name="" id="" @change="changeBrush" v-model="brush"> |
| | |
| | | export default { |
| | | data() { |
| | | return { |
| | | canvasWidth: window.innerWidth > 450 ? 400 : window.innerWidth - 30, |
| | | backgroundImgUrl: "", // 背景 |
| | | isDraw: true, // 绘画、框选模式 |
| | | brush: "Pencil", // 画笔类型 |
| | |
| | | type: Number, |
| | | default: 1, |
| | | }, |
| | | canvasHeight:{ |
| | | type:Number, |
| | | default:380 |
| | | } |
| | | canvasHeight: { |
| | | type: Number, |
| | | default: 380, |
| | | }, |
| | | }, |
| | | mounted() { |
| | | this.init(); |
| | |
| | | methods: { |
| | | // 初始化画布 |
| | | init() { |
| | | this.canvas = new fabric.Canvas("canvas", { |
| | | isDrawingMode: true, |
| | | }); |
| | | this.canvas = new fabric.Canvas( |
| | | (this.container ? this.container : document).querySelector("#canvas"), |
| | | { |
| | | isDrawingMode: true, |
| | | } |
| | | ); |
| | | // 设置背景 |
| | | this.setBackgroundImage() |
| | | // |
| | | this.setBackgroundImage(); |
| | | fabric.Object.prototype.transparentCorners = false; |
| | | this.setBrush() |
| | | this.setBrush(); |
| | | }, |
| | | // 创建各种笔刷 |
| | | setBrush() { |
| | |
| | | // 画笔样式 |
| | | this.vLinePatternBrush = new fabric.PatternBrush(this.canvas); |
| | | this.vLinePatternBrush.getPatternSrc = () => { |
| | | let patternCanvas = fabric.document.createElement("canvas"); |
| | | let patternCanvas = document.createElement("canvas"); |
| | | patternCanvas.width = patternCanvas.height = 10; |
| | | let ctx = patternCanvas.getContext("2d"); |
| | | ctx.strokeStyle = this.lineColor; |
| | |
| | | }; |
| | | this.hLinePatternBrush = new fabric.PatternBrush(this.canvas); |
| | | this.hLinePatternBrush.getPatternSrc = function () { |
| | | let patternCanvas = fabric.document.createElement("canvas"); |
| | | let patternCanvas = document.createElement("canvas"); |
| | | patternCanvas.width = patternCanvas.height = 10; |
| | | let ctx = patternCanvas.getContext("2d"); |
| | | ctx.strokeStyle = this.lineColor; |
| | |
| | | this.squarePatternBrush.getPatternSrc = function () { |
| | | const squareWidth = 10; |
| | | const squareDistance = 2; |
| | | const patternCanvas = fabric.document.createElement("canvas"); |
| | | const patternCanvas = document.createElement("canvas"); |
| | | patternCanvas.width = patternCanvas.height = |
| | | squareWidth + squareDistance; |
| | | const ctx = patternCanvas.getContext("2d"); |
| | |
| | | this.diamondPatternBrush.getPatternSrc = function () { |
| | | const squareWidth = 10; |
| | | const squareDistance = 5; |
| | | const patternCanvas = fabric.document.createElement("canvas"); |
| | | const patternCanvas = document.createElement("canvas"); |
| | | const rect = new fabric.Rect({ |
| | | width: squareWidth, |
| | | height: squareWidth, |
| | |
| | | const oldData = localStorage.getItem( |
| | | this.config.activeBook.name + "-paint-" + this.page |
| | | ); |
| | | this.oldImg = oldData; |
| | | this.backgroundImgUrl = oldData || this.imgUrl; |
| | | fabric.Image.fromURL( |
| | | this.backgroundImgUrl, |
| | |
| | | // 清空画布 |
| | | clearCanvas() { |
| | | this.canvas.clear(); |
| | | this.setBackgroundImage() |
| | | this.setBackgroundImage(); |
| | | }, |
| | | // 修改画笔颜色 |
| | | changeLineColor(e) { |
| | |
| | | |
| | | <style lang="less" scoped> |
| | | .paint { |
| | | position: relative; |
| | | margin-top: 20px; |
| | | display: flex; |
| | | flex-direction: column; |
| | | align-items: center; |
| | | } |
| | | .bcImg { |
| | | position: absolute; |
| | | } |
| | | #canvas { |
| | | border: 1px solid #ccc; |
| | | } |
| | | .paint-btn { |
| | | width: 96%; |
| | | margin-top:40px; |
| | | padding:20px; |
| | | border:1px solid #ededed; |
| | | margin-top: 40px; |
| | | padding: 20px; |
| | | border: 1px solid #ededed; |
| | | li { |
| | | margin-bottom:6px; |
| | | margin-bottom: 6px; |
| | | } |
| | | } |
| | | .paint-btn-box { |
| | | display:flex; |
| | | justify-content:space-evenly; |
| | | display: flex; |
| | | justify-content: space-evenly; |
| | | } |
| | | </style> |