| | |
| | | <template> |
| | | <div class="paint"> |
| | | <canvas width="600" height="800" id="canvas"></canvas> |
| | | <canvas id="canvas" :width="canvasWidth" :height="canvasHeight"></canvas> |
| | | <!-- 操作按钮 --> |
| | | <ul class="paint-btn"> |
| | | <li class="btn-box"> |
| | | <li class="paint-btn-box"> |
| | | <button @click="changeDrawMode"> |
| | | {{ isDraw ? "框选模式" : "绘图模式" }} |
| | | </button> |
| | |
| | | </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, |
| | | }, |
| | | }, |
| | | 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, |
| | |
| | | rect.render(ctx); |
| | | return patternCanvas; |
| | | }; |
| | | |
| | | const img = new Image(); |
| | | // img.src = "../assets/images/drop.jpg"; |
| | | this.texturePatternBrush = new fabric.PatternBrush(this.canvas); |
| | | this.texturePatternBrush.source = img; |
| | | } |
| | |
| | | this.canvas.renderAll.bind(this.canvas), |
| | | { |
| | | // 可以设置图像的样式,比如不透明度 |
| | | opacity: 0.5, |
| | | // opacity: 0.5, |
| | | } |
| | | ); |
| | | // 渲染画布 |
| | |
| | | // 清空画布 |
| | | clearCanvas() { |
| | | this.canvas.clear(); |
| | | this.setBackgroundImage() |
| | | this.setBackgroundImage(); |
| | | }, |
| | | // 修改画笔颜色 |
| | | changeLineColor(e) { |
| | |
| | | if (brush.getPatternSrc) { |
| | | brush.source = brush.getPatternSrc.call(brush); |
| | | } |
| | | console.log(e.srcElement.value); |
| | | }, |
| | | // 修改画笔粗细 |
| | | changeLineWidth(e) { |
| | | console.log(e); |
| | | this.canvas.freeDrawingBrush.width = |
| | | parseInt(e.srcElement.value, 10) || 1; |
| | | }, |
| | |
| | | changeShowColor(e) { |
| | | this.canvas.contextContainer.shadowColor = e.srcElement.value; |
| | | }, |
| | | |
| | | // 阴影宽度 |
| | | changeShowWidth(e) { |
| | | console.log(this.canvas); |
| | |
| | | this.canvas.contextContainer.shadowOffsetY = |
| | | parseInt(e.srcElement.value, 10) || 0; |
| | | }, |
| | | // 保存图书 |
| | | // 保存图片 |
| | | saveImgData() { |
| | | const imgData = this.canvas.toDataURL({ |
| | | format: "png", // 指定输出格式,通常是'png'或'jpeg' |
| | |
| | | </script> |
| | | |
| | | <style lang="less" scoped> |
| | | .paint { |
| | | margin-top: 20px; |
| | | display: flex; |
| | | flex-direction: column; |
| | | align-items: center; |
| | | } |
| | | #canvas { |
| | | border: 1px solid #ccc; |
| | | } |
| | | .paint-btn { |
| | | margin-top:40px; |
| | | padding:20px; |
| | | border:1px solid #ededed; |
| | | width:max-content; |
| | | width: 96%; |
| | | margin-top: 40px; |
| | | padding: 20px; |
| | | border: 1px solid #ededed; |
| | | li { |
| | | margin-bottom:6px; |
| | | margin-bottom: 6px; |
| | | } |
| | | } |
| | | .btn-box { |
| | | display:flex; |
| | | justify-content:space-between; |
| | | .paint-btn-box { |
| | | display: flex; |
| | | justify-content: space-evenly; |
| | | } |
| | | </style> |