| | |
| | | <template> |
| | | <div class="paint"> |
| | | <canvas width="400" height="600" id="canvas"></canvas> |
| | | <canvas id="canvas" width="400" :height="canvasHeight"></canvas> |
| | | <!-- 操作按钮 --> |
| | | <ul class="paint-btn"> |
| | | <li class="btn-box"> |
| | | <li class="paint-btn-box"> |
| | | <button @click="changeDrawMode"> |
| | | {{ isDraw ? "框选模式" : "绘图模式" }} |
| | | </button> |
| | |
| | | <button @click="saveImgData">保存</button> |
| | | </li> |
| | | <li> |
| | | |
| | | </li> |
| | | <li> |
| | | <label>画笔:</label> |
| | |
| | | <option |
| | | v-for="(item, index) in modelList" |
| | | :value="item.value" |
| | | ::key="index" |
| | | :key="index" |
| | | > |
| | | {{ item.name }} |
| | | </option> |
| | |
| | | type: Number, |
| | | default: 1, |
| | | }, |
| | | canvasHeight:{ |
| | | type:Number, |
| | | default:380 |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.init(); |
| | |
| | | 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, |
| | | } |
| | | ); |
| | | // 渲染画布 |
| | |
| | | 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 { |
| | | width: 96%; |
| | | margin-top:40px; |
| | | padding:20px; |
| | | border:1px solid #ededed; |
| | | width:max-content; |
| | | li { |
| | | margin-bottom:6px; |
| | | } |
| | | } |
| | | .btn-box { |
| | | .paint-btn-box { |
| | | display:flex; |
| | | justify-content:space-between; |
| | | justify-content:space-evenly; |
| | | } |
| | | </style> |