fabricjs 相关代码
<button @click="rectDraw()" class="rectDrawBtn">矩形</button>
<button @click="triangleDraw()" class="triangleDrawBtn">三角形</button>
rectDraw(){
this.currentType = 'rect';
this.initRect();
},
triangleDraw(){
this.currentType = 'triangle';
this.initTriangle();
},
initRect(){
let left = this.mouseFrom.x;
let top = this.mouseFrom.y;
let width = this.mouseTo.x - this.mouseFrom.x;
let height = this.mouseTo.y - this.mouseFrom.y;
let canvasObject = new fabric.Rect({
left: left,
top: top,
width : width,
height : height,
stroke: this.colors,
fill: "rgba(255, 255, 255, 0)",
strokeWidth: this.strokeWidth
});
this.toggleDrawingObject(canvasObject);
},
initTriangle(){
let left = this.mouseFrom.x;
let top = this.mouseFrom.y;
let height = this.mouseTo.y - this.mouseFrom.y;
let width = Math.sqrt(Math.pow(height, 2) + Math.pow(height / 2.0, 2));
let canvasObject = new fabric.Triangle({
left: left,
top: top,
width : width,
height : height,
stroke: this.colors,
fill: "rgba(255, 255, 255, 0)",
strokeWidth: this.strokeWidth
});
this.toggleDrawingObject(canvasObject);
},
步骤
- 添加 button
- 添加 方法
- 添加 对应的object到canvas
参考
联系我
加我的wx:wx:meethaowu,关注我的公众号:大前端说。欢迎交流关于前端的任何话题。