闫增涛
2024-11-06 85da14ad4377e081a744934c3ded049808427f59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<template>
  <div class="paint">
    <canvas id="canvas" :width="canvasWidth" :height="canvasHeight"></canvas>
    <!-- 操作按钮 -->
    <ul class="paint-btn">
      <li class="paint-btn-box">
        <button @click="changeDrawMode">
          {{ isDraw ? "框选模式" : "绘图模式" }}
        </button>
        <button @click="clearCanvas">清除</button>
        <button @click="setModelEraser">
          {{ isEraser ? "画笔" : "橡皮擦" }}
        </button>
        <button @click="saveImgData">保存</button>
      </li>
      <li></li>
      <li>
        <label>画笔:</label>
        <select name="" id="" @change="changeBrush" v-model="brush">
          <option
            v-for="(item, index) in modelList"
            :value="item.value"
            :key="index"
          >
            {{ item.name }}
          </option>
        </select>
      </li>
      <li>
        <label>线色:</label>
        <input type="color" v-model="lineColor" @input="changeLineColor" />
      </li>
      <li>
        <label>线宽:</label>
        <input type="range" v-model="lineWidth" @input="changeLineWidth" />
      </li>
      <li>
        <label>阴影色:</label>
        <input type="color" v-model="showColor" @input="changeShowColor" />
      </li>
      <li>
        <label>阴影宽度:</label>
        <input type="range" v-model="showWidth" @input="changeShowWidth" />
      </li>
      <li>
        <label>阴影偏移量:</label>
        <input type="range" v-model="showOffset" @input="changeShowOffset" />
      </li>
    </ul>
  </div>
</template>
 
<script>
import { fabric } from "fabric-with-erasing";
export default {
  data() {
    return {
      canvasWidth:window.innerWidth > 450 ? 400 : window.innerWidth - 30,
      backgroundImgUrl: "", // 背景
      isDraw: true, // 绘画、框选模式
      brush: "Pencil", // 画笔类型
      lineColor: "#000",
      lineWidth: 1,
      isEraser: false,
      showColor: "#000", // 阴影色
      showWidth: 0, // 阴影宽度
      showOffset: 0,
      modelList: [
        {
          name: "Pencil",
          value: "Pencil",
        },
        {
          name: "Circle",
          value: "Circle",
        },
        {
          name: "Spray",
          value: "Spray",
        },
        {
          name: "Pattern",
          value: "Pattern",
        },
        {
          name: "hline",
          value: "hline",
        },
        {
          name: "vline",
          value: "vline",
        },
        {
          name: "square",
          value: "square",
        },
        {
          name: "diamond",
          value: "diamond",
        },
        {
          name: "texture",
          value: "texture",
        },
      ],
      // 画笔模式
      vLinePatternBrush: null,
      hLinePatternBrush: null,
      squarePatternBrush: null,
      diamondPatternBrush: null,
      texturePatternBrush: null,
    };
  },
  props: {
    imgUrl: {
      type: String,
      default:
        "https://cdn.learnku.com/uploads/images/202206/29/97252/aArKOJpl2A.png!large",
    },
    page: {
      type: Number,
      default: 1,
    },
    canvasHeight: {
      type: Number,
      default: 380,
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化画布
    init() {
      this.canvas = new fabric.Canvas(
        (this.container ? this.container : document).querySelector("#canvas"),
        {
          isDrawingMode: true,
        }
      );
      // 设置背景
      this.setBackgroundImage();
      //
      fabric.Object.prototype.transparentCorners = false;
      this.setBrush();
    },
    // 创建各种笔刷
    setBrush() {
      if (fabric.PatternBrush) {
        // 画笔样式
        this.vLinePatternBrush = new fabric.PatternBrush(this.canvas);
        this.vLinePatternBrush.getPatternSrc = () => {
          let patternCanvas =
            fabric[this.container ? this.container : document].createElement(
              "canvas"
            );
          patternCanvas.width = patternCanvas.height = 10;
          let ctx = patternCanvas.getContext("2d");
          ctx.strokeStyle = this.lineColor;
          ctx.lineWidth = 5;
          ctx.beginPath();
          ctx.moveTo(0, 5);
          ctx.lineTo(10, 5);
          ctx.closePath();
          ctx.stroke();
          return patternCanvas;
        };
        this.hLinePatternBrush = new fabric.PatternBrush(this.canvas);
        this.hLinePatternBrush.getPatternSrc = function () {
          let patternCanvas =
            fabric[this.container ? this.container : document].createElement(
              "canvas"
            );
          patternCanvas.width = patternCanvas.height = 10;
          let ctx = patternCanvas.getContext("2d");
          ctx.strokeStyle = this.lineColor;
          ctx.lineWidth = 5;
          ctx.beginPath();
          ctx.moveTo(5, 0);
          ctx.lineTo(5, 10);
          ctx.closePath();
          ctx.stroke();
          return patternCanvas;
        };
        this.squarePatternBrush = new fabric.PatternBrush(this.canvas);
        this.squarePatternBrush.getPatternSrc = function () {
          const squareWidth = 10;
          const squareDistance = 2;
          const patternCanvas =
            fabric[this.container ? this.container : document].createElement(
              "canvas"
            );
          patternCanvas.width = patternCanvas.height =
            squareWidth + squareDistance;
          const ctx = patternCanvas.getContext("2d");
          ctx.fillStyle = this.color;
          ctx.fillRect(0, 0, squareWidth, squareWidth);
          return patternCanvas;
        };
        this.diamondPatternBrush = new fabric.PatternBrush(this.canvas);
        this.diamondPatternBrush.getPatternSrc = function () {
          const squareWidth = 10;
          const squareDistance = 5;
          const patternCanvas =
            fabric[this.container ? this.container : document].createElement(
              "canvas"
            );
          const rect = new fabric.Rect({
            width: squareWidth,
            height: squareWidth,
            angle: 45,
            fill: this.color,
          });
          var canvasWidth = rect.getBoundingRect().width;
          patternCanvas.width = patternCanvas.height =
            canvasWidth + squareDistance;
          rect.set({ left: canvasWidth / 2, top: canvasWidth / 2 });
          var ctx = patternCanvas.getContext("2d");
          rect.render(ctx);
          return patternCanvas;
        };
        const img = new Image();
        this.texturePatternBrush = new fabric.PatternBrush(this.canvas);
        this.texturePatternBrush.source = img;
      }
    },
    // 设置背景图方法
    setBackgroundImage() {
      // 使用fabric的Image.fromURL方法来加载图像
      const oldData = localStorage.getItem(
        this.config.activeBook.name + "-paint-" + this.page
      );
      this.backgroundImgUrl = oldData || this.imgUrl;
      fabric.Image.fromURL(
        this.backgroundImgUrl,
        (img) => {
          // 图像加载完成后,将其设置为画布的背景
          img
            .scale(
              this.canvas.width / img.width,
              this.canvas.height / img.height
            )
            .set({
              left: 0,
              top: 0,
              originX: "left",
              originY: "top",
            });
 
          // 将图像添加到画布中,并将其放在最底层
          this.canvas.setBackgroundImage(
            img,
            this.canvas.renderAll.bind(this.canvas),
            {
              // 可以设置图像的样式,比如不透明度
              // opacity: 0.5,
            }
          );
          // 渲染画布
          this.canvas.renderAll();
        },
        {
          crossOrigin: "Anonymous", // 如果图像在不同域上,需要设置crossOrigin
        }
      );
    },
    // 绘图 框选 模式切换
    changeDrawMode() {
      this.isDraw = !this.isDraw;
      this.canvas.isDrawingMode = !this.canvas.isDrawingMode;
    },
    // 清空画布
    clearCanvas() {
      this.canvas.clear();
      this.setBackgroundImage();
    },
    // 修改画笔颜色
    changeLineColor(e) {
      let brush = this.canvas.freeDrawingBrush;
      brush.color = e.srcElement.value;
      if (brush.getPatternSrc) {
        brush.source = brush.getPatternSrc.call(brush);
      }
    },
    // 修改画笔粗细
    changeLineWidth(e) {
      this.canvas.freeDrawingBrush.width =
        parseInt(e.srcElement.value, 10) || 1;
    },
    // 画笔样式切换
    changeBrush() {
      if (this.brush == "hline") {
        this.canvas.freeDrawingBrush = this.vLinePatternBrush;
      } else if (this.brush == "vline") {
        this.canvas.freeDrawingBrush = this.hLinePatternBrush;
      } else if (this.brush == "square") {
        this.canvas.freeDrawingBrush = this.squarePatternBrush;
      } else if (this.brush == "diamond") {
        this.canvas.freeDrawingBrush = this.diamondPatternBrush;
      } else if (this.brush == "texture") {
        this.canvas.freeDrawingBrush = this.texturePatternBrush;
      } else {
        this.canvas.freeDrawingBrush = new fabric[this.brush + "Brush"](
          this.canvas
        );
      }
      if (this.canvas.freeDrawingBrush) {
        var brush = this.canvas.freeDrawingBrush;
        brush.color = this.lineColor;
        if (brush.getPatternSrc) {
          brush.source = brush.getPatternSrc.call(brush);
        }
        brush.width = parseInt(this.lineWidth, 10) || 1;
        brush.shadow = new fabric.Shadow({
          blur: parseInt(this.showWidth, 10) || 0,
          offsetX: 0,
          offsetY: 0,
          affectStroke: true,
          color: this.showColor,
        });
      }
    },
    // 橡皮擦
    setModelEraser() {
      this.isEraser = !this.isEraser;
      if (this.isEraser) {
        this.canvas.freeDrawingBrush = new fabric.EraserBrush(this.canvas); // 使用橡皮擦画笔
        this.canvas.freeDrawingBrush.width = this.lineWidth;
      } else {
        this.changeBrush();
        // this.canvas.freeDrawingBrush = new fabric.PencilBrush(this.canvas); // 使用橡皮擦画笔
        // this.canvas.freeDrawingBrush.width = this.lineWidth; // 设置画笔粗细
      }
    },
    // 修改阴影色
    changeShowColor(e) {
      this.canvas.contextContainer.shadowColor = e.srcElement.value;
    },
    // 阴影宽度
    changeShowWidth(e) {
      console.log(this.canvas);
      this.canvas.contextContainer.shadowBlur =
        parseInt(e.srcElement.value, 10) || 0;
    },
    // 阴影偏移量
    changeShowOffset(e) {
      this.canvas.contextContainer.shadowOffsetX =
        parseInt(e.srcElement.value, 10) || 0;
      this.canvas.contextContainer.shadowOffsetY =
        parseInt(e.srcElement.value, 10) || 0;
    },
    // 保存图片
    saveImgData() {
      const imgData = this.canvas.toDataURL({
        format: "png", // 指定输出格式,通常是'png'或'jpeg'
        quality: 0.8, // 仅在输出格式为'jpeg'时有效
        multiplier: 1, // 提高分辨率,可选参数,默认为1
        left: 0, // 裁剪区域的左边界(可选)
        top: 0, // 裁剪区域的上边界(可选)
        width: canvas.width, // 裁剪区域的宽度(可选,默认为画布的宽度)
        height: canvas.height, // 裁剪区域的高度(可选,默认为画布的高度)
      });
      localStorage.setItem(
        this.config.activeBook.name + "-paint-" + this.page,
        imgData
      );
      console.log("本地图", imgData);
    },
  },
};
</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;
  li {
    margin-bottom: 6px;
  }
}
.paint-btn-box {
  display: flex;
  justify-content: space-evenly;
}
</style>