深度画布锁定

This commit is contained in:
lzp
2026-03-18 13:56:27 +08:00
parent 33901f7609
commit 0f99ea809d
6 changed files with 68 additions and 15 deletions

View File

@@ -20,7 +20,13 @@ fabric.Object.prototype.toObject = function () {
arr.push(...v)
}
})
return this.toObject_(arr)
const object = this.toObject_(arr)
if (object.info) {
let lock = !!object.info.lock
object.evented = !lock
object.selectable = !lock
}
return object
}
interface CanvasInitOptions {
@@ -71,9 +77,12 @@ export class CanvasManager {
top: 0,
scaleX: 1,
scaleY: 1,
evented: false,
selectable: false,
info: {
id: createId("image"),
name: "图片图层",
lock: true,
}
})
image = img
@@ -171,7 +180,7 @@ export class CanvasManager {
/** 设置激活对象 */
setActiveObjectById(id: string) {
const obj = this.getObjectById(id)
if (obj) this.canvas.setActiveObject(obj)
if (obj && obj.evented) this.canvas.setActiveObject(obj)
this.renderAll()
}
resetZoom(animated = true, adaptive = true) {
@@ -208,13 +217,13 @@ export class CanvasManager {
getObjectById(id: string) {
return this.getObjects().find((item: any) => item?.info?.id === id)
}
getActiveObject() {
return this.getObjectById(this.layerManager.activeID.value)
/** 获取选中对象 */
getSelectedObject() {
return this.canvas.getActiveObject()
}
renderAll() {
this.canvas.renderAll()
}
deleteObjectById(id: string) {
const object = this.getObjectById(id)
if (object) {
@@ -223,6 +232,11 @@ export class CanvasManager {
this.renderAll()
}
}
/** 取消选中对象 */
discardActiveObject() {
this.canvas.discardActiveObject()
this.renderAll()
}
// 拖拽排序
dragSort(id, newIndex) {
this.canvas.moveTo(this.getObjectById(id), newIndex)