|
@@ -13,13 +13,14 @@
|
|
boxWidth: 100,
|
|
boxWidth: 100,
|
|
boxHeight: 100,
|
|
boxHeight: 100,
|
|
canvas: null,
|
|
canvas: null,
|
|
- position: []
|
|
|
|
|
|
+ position: [],
|
|
|
|
+ backgroundImageUrl: 'https://file-node.oss-cn-shanghai.aliyuncs.com/youji/01a0120395b249ecbae565600f2fb3ce'
|
|
}
|
|
}
|
|
},
|
|
},
|
|
beforeDestroy() {
|
|
beforeDestroy() {
|
|
let _self = this;
|
|
let _self = this;
|
|
window.removeEventListener('resize', function() {
|
|
window.removeEventListener('resize', function() {
|
|
- _self.init();
|
|
|
|
|
|
+ _self.initResize();
|
|
});
|
|
});
|
|
},
|
|
},
|
|
mounted() {
|
|
mounted() {
|
|
@@ -28,73 +29,71 @@
|
|
_self.init();
|
|
_self.init();
|
|
// 监听窗口大小变化
|
|
// 监听窗口大小变化
|
|
window.addEventListener('resize', function() {
|
|
window.addEventListener('resize', function() {
|
|
- _self.init();
|
|
|
|
|
|
+ _self.initResize();
|
|
});
|
|
});
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
init() {
|
|
init() {
|
|
let box = document.getElementById('fabricContainer');
|
|
let box = document.getElementById('fabricContainer');
|
|
if (!box) return;
|
|
if (!box) return;
|
|
- let boxWidth = box.offsetWidth;
|
|
|
|
- let boxHeight = box.offsetHeight;
|
|
|
|
- if (!this.canvas) {
|
|
|
|
|
|
+ let boxWidth = box.offsetWidth,
|
|
|
|
+ boxHeight = box.offsetHeight;
|
|
|
|
+ let imgObj = new Image();
|
|
|
|
+ imgObj.src = this.backgroundImageUrl;
|
|
|
|
+ imgObj.onload = () => {
|
|
|
|
+ let imgWidth = imgObj.width,
|
|
|
|
+ imgHeight = imgObj.height;
|
|
|
|
+ let scale = imgWidth / imgHeight;
|
|
|
|
+ let canvasWidth = boxWidth >= boxHeight ? boxHeight * scale : boxWidth,
|
|
|
|
+ canvasHeight = boxWidth < boxHeight ? boxWidth / scale : boxHeight;
|
|
this.canvas = new fabric.Canvas('myCanvas', {
|
|
this.canvas = new fabric.Canvas('myCanvas', {
|
|
fireRightClick: true, // 启用右键,button的数字为3
|
|
fireRightClick: true, // 启用右键,button的数字为3
|
|
stopContextMenu: true, // 禁止默认右键菜单
|
|
stopContextMenu: true, // 禁止默认右键菜单
|
|
controlsAboveOverlay: true, // 超出clipPath后仍然展示控制条
|
|
controlsAboveOverlay: true, // 超出clipPath后仍然展示控制条
|
|
preserveObjectStacking: true, // 当选择画布中的对象时,让对象不在顶层。
|
|
preserveObjectStacking: true, // 当选择画布中的对象时,让对象不在顶层。
|
|
- width: boxWidth, // 设置画布宽度
|
|
|
|
- height: boxHeight, // 设置画布高度
|
|
|
|
|
|
+ width: canvasWidth, // 设置画布宽度
|
|
|
|
+ height: canvasHeight, // 设置画布高度
|
|
});
|
|
});
|
|
- // this.clickFunction()
|
|
|
|
|
|
+ document.getElementById('canvas-container').style.width = canvasWidth;
|
|
|
|
+ document.getElementById('canvas-container').style.hight = canvasWidth;
|
|
this.initBackgroundImage();
|
|
this.initBackgroundImage();
|
|
- } else {
|
|
|
|
- this.canvas.setWidth(boxWidth);
|
|
|
|
- this.canvas.setHeight(boxHeight);
|
|
|
|
- const canvasWidth = this.canvas.getWidth();
|
|
|
|
- const canvasHeight = this.canvas.getHeight();
|
|
|
|
- // 调整画布内容的大小
|
|
|
|
- const objects = this.canvas.getObjects();
|
|
|
|
- objects.forEach(obj => {
|
|
|
|
- console.log(obj);
|
|
|
|
- if (!obj.stroke) {
|
|
|
|
- const scaleX = canvasWidth / obj.width;
|
|
|
|
- const scaleY = canvasHeight / obj.height;
|
|
|
|
- const scale = scaleX < scaleY ? scaleX : scaleY;
|
|
|
|
- obj.scale(scale);
|
|
|
|
- obj.set({
|
|
|
|
- left: (canvasWidth - obj.width * scale) / 2,
|
|
|
|
- top: (canvasHeight - obj.height * scale) / 2
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- this.canvas.renderAll(); // 重新渲染画布
|
|
|
|
|
|
+ this.clickFunction();
|
|
|
|
+
|
|
}
|
|
}
|
|
},
|
|
},
|
|
|
|
+ initResize() {
|
|
|
|
+ let box = document.getElementById('fabricContainer');
|
|
|
|
+ if (!box) return;
|
|
|
|
+ let boxWidth = box.offsetWidth,
|
|
|
|
+ container = document.getElementById('myCanvas').offsetWidth;
|
|
|
|
+ let scale = boxWidth / container >= 1 ? 1 : boxWidth / container;
|
|
|
|
+ document.getElementById('canvas-container').style.transform = 'scale(' + scale + ')';
|
|
|
|
+ },
|
|
clickFunction() {
|
|
clickFunction() {
|
|
let _self = this;
|
|
let _self = this;
|
|
// 监听点击事件
|
|
// 监听点击事件
|
|
- _self.canvas.on('mouse:down', function(options) {
|
|
|
|
- // 获取点击位置的坐标
|
|
|
|
- const pointer = _self.canvas.getPointer(options.e);
|
|
|
|
- const x = pointer.x;
|
|
|
|
- const y = pointer.y;
|
|
|
|
- _self.position.push({
|
|
|
|
- x: x,
|
|
|
|
- y: y
|
|
|
|
- })
|
|
|
|
- console.log(_self.position);
|
|
|
|
|
|
+ _self.canvas.on('mouse:down', function(options) {
|
|
|
|
+ console.log(options);
|
|
|
|
+ // // 获取点击位置的坐标
|
|
|
|
+ // const pointer = _self.canvas.getPointer(options.e);
|
|
|
|
+ // const x = pointer.x;
|
|
|
|
+ // const y = pointer.y;
|
|
|
|
+ // _self.position.push({
|
|
|
|
+ // x: x,
|
|
|
|
+ // y: y
|
|
|
|
+ // })
|
|
|
|
+ // console.log(_self.position);
|
|
|
|
|
|
- // 在点击位置添加一个圆形标记
|
|
|
|
- const circle = new fabric.Circle({
|
|
|
|
- radius: 5,
|
|
|
|
- fill: 'red',
|
|
|
|
- left: x,
|
|
|
|
- top: y,
|
|
|
|
- selectable: false // 禁止选中
|
|
|
|
- });
|
|
|
|
- _self.canvas.add(circle);
|
|
|
|
- _self.canvas.renderAll();
|
|
|
|
|
|
+ // // 在点击位置添加一个圆形标记
|
|
|
|
+ // const circle = new fabric.Circle({
|
|
|
|
+ // radius: 5,
|
|
|
|
+ // fill: 'red',
|
|
|
|
+ // left: x,
|
|
|
|
+ // top: y,
|
|
|
|
+ // selectable: false // 禁止选中
|
|
|
|
+ // });
|
|
|
|
+ // _self.canvas.add(circle);
|
|
|
|
+ // _self.canvas.renderAll();
|
|
});
|
|
});
|
|
},
|
|
},
|
|
initBackgroundImage() {
|
|
initBackgroundImage() {
|
|
@@ -111,8 +110,8 @@
|
|
// 设置图片的缩放比例
|
|
// 设置图片的缩放比例
|
|
img.scale(scale);
|
|
img.scale(scale);
|
|
img.set({
|
|
img.set({
|
|
- left: (canvasWidth - img.width * scale) / 2,
|
|
|
|
- top: (canvasHeight - img.height * scale) / 2,
|
|
|
|
|
|
+ left: 0,
|
|
|
|
+ top: 0,
|
|
selectable: false
|
|
selectable: false
|
|
})
|
|
})
|
|
_self.canvas.add(img);
|
|
_self.canvas.add(img);
|
|
@@ -121,36 +120,27 @@
|
|
},
|
|
},
|
|
addPolygon() {
|
|
addPolygon() {
|
|
const starPoints = [{
|
|
const starPoints = [{
|
|
- "x": 127,
|
|
|
|
- "y": 210
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "x": 192,
|
|
|
|
- "y": 208
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "x": 193,
|
|
|
|
- "y": 179
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "x": 337,
|
|
|
|
- "y": 179
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "x": 335,
|
|
|
|
- "y": 0
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "x": 127,
|
|
|
|
- "y": 1
|
|
|
|
- }
|
|
|
|
- ];
|
|
|
|
|
|
+ "x": 705,
|
|
|
|
+ "y": 213
|
|
|
|
+ }, {
|
|
|
|
+ "x": 855,
|
|
|
|
+ "y": 216
|
|
|
|
+ }, {
|
|
|
|
+ "x": 855,
|
|
|
|
+ "y": 354
|
|
|
|
+ }, {
|
|
|
|
+ "x": 705,
|
|
|
|
+ "y": 354
|
|
|
|
+ }];
|
|
const star = new fabric.Polygon(starPoints, {
|
|
const star = new fabric.Polygon(starPoints, {
|
|
- left: 300,
|
|
|
|
- top: 100,
|
|
|
|
- fill: 'yellow',
|
|
|
|
- stroke: 'orange',
|
|
|
|
- strokeWidth: 2
|
|
|
|
|
|
+ fill: 'rgba(113, 179, 255, 0.6)',
|
|
|
|
+ stroke: 'rgba(113, 179, 255, 1.0)',
|
|
|
|
+ strokeWidth: 1,
|
|
|
|
+ selectable: false,
|
|
|
|
+ id:1
|
|
|
|
+ });
|
|
|
|
+ star.on('click', function(e) {
|
|
|
|
+ console.log(e);
|
|
});
|
|
});
|
|
this.canvas.add(star);
|
|
this.canvas.add(star);
|
|
this.canvas.renderAll();
|
|
this.canvas.renderAll();
|
|
@@ -164,5 +154,9 @@
|
|
width: 100%;
|
|
width: 100%;
|
|
height: 100%;
|
|
height: 100%;
|
|
overflow: hidden;
|
|
overflow: hidden;
|
|
|
|
+
|
|
|
|
+ #canvas-container {
|
|
|
|
+ transform-origin: 0% 0%;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
</style>
|
|
</style>
|