| | |
| | | <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:400, |
| | | backgroundImgUrl: "", // 背景 |
| | | isDraw: true, // 绘画、框选模式 |
| | | brush: "Pencil", // 画笔类型 |
| | |
| | | type: Number, |
| | | default: 1, |
| | | }, |
| | | canvasHeight:{ |
| | | type:Number, |
| | | default:380 |
| | | } |
| | | canvasHeight: { |
| | | type: Number, |
| | | default: 380, |
| | | }, |
| | | }, |
| | | mounted() { |
| | | this.handleCanvasWidth() |
| | | this.init(); |
| | | }, |
| | | methods: { |
| | | handleCanvasWidth() { |
| | | if(window.innerWidth < 450) { |
| | | this.canvasWidth = window.innerWidth - 30 |
| | | } |
| | | }, |
| | | // 初始化画布 |
| | | 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 = |
| | | fabric[this.container ? this.container : 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 = |
| | | fabric[this.container ? this.container : 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 = |
| | | fabric[this.container ? this.container : 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 = |
| | | fabric[this.container ? this.container : document].createElement( |
| | | "canvas" |
| | | ); |
| | | const rect = new fabric.Rect({ |
| | | width: squareWidth, |
| | | height: squareWidth, |
| | |
| | | // 清空画布 |
| | | clearCanvas() { |
| | | this.canvas.clear(); |
| | | this.setBackgroundImage() |
| | | this.setBackgroundImage(); |
| | | }, |
| | | // 修改画笔颜色 |
| | | changeLineColor(e) { |
| | |
| | | } |
| | | .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> |