Files
FiDA_Front/src/components/Canvas/DepthCanvas/manager/CanvasManager.ts

168 lines
4.1 KiB
TypeScript
Raw Normal View History

2026-03-09 13:44:32 +08:00
import { fabric } from 'fabric-with-all'
import { ref } from 'vue'
import { createCanvas } from '../tools/canvasFactory'
2026-03-09 14:20:44 +08:00
import { AnimationManager } from './AnimationManager'
2026-03-09 13:44:32 +08:00
import { detectDeviceType } from '../tools/index'
import { CanvasEventManager } from "./events/CanvasEventManager";
import { OperationType } from '../tools/layerHelper'
interface CanvasInitOptions {
canvasRef: any
canvasViewWidth?: number
canvasViewHeight?: number
canvasWidth?: number
canvasHeight?: number
}
export class CanvasManager {
stateManager: any
2026-03-09 16:45:30 +08:00
layerManager: any
animationManager: any
eventManager: any
2026-03-09 13:44:32 +08:00
deviceInfo: any
canvas: any
canvasViewWidth: number
canvasViewHeight: number
canvasWidth: number
canvasHeight: number
currentZoom: any
constructor(options) {
this.stateManager = options.stateManager;
this.deviceInfo = detectDeviceType();
this.currentZoom = ref(100)
}
setCanvasViewSize(options) {
this.canvasViewWidth = options.canvasViewWidth || 1920
this.canvasViewHeight = options.canvasViewHeight || 1080
}
initCanvas(options: CanvasInitOptions) {
2026-03-09 16:45:30 +08:00
this.layerManager = this.stateManager.layerManager
2026-03-09 13:44:32 +08:00
this.setCanvasViewSize(options)
this.canvasWidth = options.canvasWidth || 750
this.canvasHeight = options.canvasHeight || 600
this.canvas = createCanvas(options.canvasRef.value, {
width: this.canvasViewWidth,
height: this.canvasViewHeight,
preserveObjectStacking: true,
enableRetinaScaling: true,
stopContextMenu: true,
fireRightClick: true,
backgroundColor: '#fff',
})
this.canvas.clipPath = new fabric.Rect({
left: 0,
top: 0,
width: this.canvasWidth,
height: this.canvasHeight
})
// 画布居中
const canvasX = this.canvasViewWidth / 2 - this.canvasWidth / 2
const canvasY = this.canvasViewHeight / 2 - this.canvasHeight / 2
this.canvas.viewportTransform = [1, 0, 0, 1, canvasX, canvasY]
// 创建矩形
const rect = new fabric.Rect({
left: 20,
top: 20,
width: 100,
height: 100,
2026-03-09 16:45:30 +08:00
fill: '#f00',
info: {
id: 'rect1',
name: 'rect1',
}
2026-03-09 13:44:32 +08:00
})
this.canvas.add(rect)
//创建圆形
const circle = new fabric.Circle({
left: 200,
top: 200,
radius: 50,
2026-03-09 16:45:30 +08:00
fill: '#0f0',
info: {
id: 'circle',
name: 'circle',
}
2026-03-09 13:44:32 +08:00
})
this.canvas.add(circle)
2026-03-09 16:45:30 +08:00
// 文字
const text = new fabric.Text('Hello World', {
left: 300,
top: 300,
fontSize: 24,
fill: '#000',
info: {
id: 'text1',
name: 'text',
}
})
this.canvas.add(text)
// 文字
const text2 = new fabric.Text('Hello World', {
left: 300,
top: 300,
fontSize: 24,
fill: '#000',
info: {
id: 'text2',
name: 'tex2t',
}
})
this.canvas.add(text2)
2026-03-09 13:44:32 +08:00
this.animationManager = new AnimationManager(this.canvas, {
currentZoom: this.currentZoom,
canvasManager: this,
wheelThrottleTime: 15, // 降低滚轮事件节流时间,提高响应性
defaultEase: "power2.lin",
defaultDuration: 0.3, // 缩短默认动画时间
});
this.setupCanvasEvents()
2026-03-09 16:45:30 +08:00
this.stateManager.toolManager.setTool(OperationType.SELECT)
this.layerManager.updateLayers()
this.layerManager.setActiveID(text2.info.id)
2026-03-09 13:44:32 +08:00
}
setupCanvasEvents() {
// 创建画布事件管理器
this.eventManager = new CanvasEventManager(this.canvas, {
canvasManager: this,
animationManager: this.animationManager,
toolManager: this.stateManager.toolManager,
});
// 设置动画交互效果
this.animationManager.setupInteractionAnimations();
}
resetZoom() {
this.animationManager.resetZoom()
}
2026-03-09 16:45:30 +08:00
getObjects() {
return this.canvas.getObjects() || []
}
getObjectById(id: string) {
return this.getObjects().find((item: any) => item.info.id === id)
}
renderAll() {
this.canvas.renderAll()
}
2026-03-09 13:44:32 +08:00
2026-03-09 16:45:30 +08:00
deleteObjectById(id: string) {
const object = this.getObjectById(id)
if (object) {
this.canvas.remove(object)
this.layerManager.updateLayers()
this.renderAll()
}
}
// 拖拽排序
dragSort(id, newIndex) {
this.canvas.moveTo(this.getObjectById(id), newIndex)
this.layerManager.updateLayers()
this.renderAll()
}
getBitObjects() {
return this.getObjects().map(v => {
const object = v.toJSON("info");
return object
})
}
2026-03-09 13:44:32 +08:00
}