Files
iwb-plugins-desktop/image-annotate/plugin/editor/state.js
T

105 lines
5.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* state.js - 编辑器唯一状态所有者
* 持有 S(全局状态对象)、ZOOM_* 常量,以及 runtime(可变持有对象)。
* 约定:ES 模块 import 绑定不可重新赋值,所以编辑器根节点 el 不用
* `export let el`,而是放在 runtime.el 里,各模块统一读写 runtime.el。
* 本模块不 import 任何编辑器内部模块(保持叶子节点,避免循环 TDZ)。
*/
'use strict'
export const ZOOM_MIN = 0.1
export const ZOOM_MAX = 10
export const ZOOM_STEP = 1.15
// ============ 状态 ============
export const S = {
open: false,
tool: 'brush',
color: '#ff4444',
brushSize: 3,
brushHardness: 100, // 画笔硬度 0~100:100=硬边描边,<100=径向渐变软边章
brushOpacity: 1, // 画笔透明度 0.05~1(一笔整体透明度,笔划内不叠加)
prevTool: null, // 吸管工具激活前的工具(取色后切回)
pickerComposite: null, // 吸管取样用的合成画布缓存(切工具/画布变化时失效)
rectFill: false, // 矩形/圆形是否实心填充
penFill: true, // 钢笔闭合路径是否实心填充(只对闭合路径有意义)
// v1.2 工具参数解耦:形状线宽与文字字号不再借用画笔粗细,
// 否则「调细画笔」会顺手把所有形状的描边一起改掉。
shapeLineWidth: 3, // 矩形/圆形/箭头/钢笔的描边粗细
textSize: 32, // 新建文字的字号 px
// 图层系统
layers: [], // 位图图层数组 [{ id, name, canvas, ctx, opacity, visible, z }]
activeLayerId: null, // 当前活跃位图图层 ID(画笔/橡皮操作目标)
// 画布引用(动态创建的 canvas 元素)
canvasWrap: null,
canvasStack: null,
textCanvas: null,
strokeCanvas: null,
overlayCanvas: null,
textCtx: null,
strokeCtx: null,
overlayCtx: null,
isDrawing: false,
panning: false,
panStart: null,
startX: 0, startY: 0,
history: [],
redoStack: [],
cropRect: null,
cropHandle: null,
cropMoving: false,
moveStart: null,
cropConstraint: { type: 'free', ratio: 0, sizeW: 0, sizeH: 0 },
// 文字图层(独立对象,可选中/移动/缩放/再编辑)
textLayers: [], // [{ id, x, y, text, color, fontSize, opacity, visible, z }]
selectedTextId: null,
textDrag: null, // { mode:'move'|'scale', start:{x,y}, layer:{x,y}, fontSize, baseX }
textEditingId: null,
selectedLayerId: null, // 选择工具当前选中的可编辑位图图层
bitmapDrag: null, // { mode:'move'|'scale', handle, start, layer, pending }
// 形状图层(矩形/箭头,独立对象,可选中/移动/调手柄)
shapeLayers: [], // [{ id, type:'rect'|'arrow', x1,y1,x2,y2, color, lw, fill, opacity, visible, z }]
selectedShapeId: null,
shapeDrag: null, // { mode:'move'|'handle', handle, start:{x,y}, shape:{...}, pending }
penPath: null, // 钢笔绘制中的点序列与预览点
penEditId: null, // 钢笔二次编辑中的形状 id
penDragPt: -1, // 正在拖动的节点下标(-1=无)
penDragBase: null,
penDragPending: null,
zCounter: 0, // 全局 z 序计数器(文字与形状统一排序)
// 图片放置中状态(导入图片时先调整大小/位置再确认)
placingImage: null, // { img, x, y, w, h } 图片坐标
placingHandle: null, // 'move' | 'nw'|'ne'|'sw'|'se'|'n'|'s'|'e'|'w'
placingStart: null, // { mx, my, x, y, w, h }
imgW: 0, imgH: 0,
overlayPad: 0,
originalImage: null, // 打开时的原始图片,用于「复原」
fitW: 0, fitH: 0, // 适应容器时的基准显示尺寸
zoom: 1, // 缩放倍率(基于 fit)
viewX: null, // 画布平移位置(wrap 内 left;null = 居中显示)
viewY: null,
callbacks: null,
spaceDown: false,
_shiftDown: false,
_canvasEventsBound: false,
// ---- v1.1 选区 / 便利性 ----
selection: null, // { kind, path, bbox, feather, active, fillRule, outline }
marquee: null, // 拉框中的临时选区 { x0,y0,x1,y1,kind }
lassoPts: null, // 套索绘制中的点序列
selectionKind: 'rect', // 选区工具的形状:rect | ellipse
selectionFeather: 0, // 羽化 px(0~20)
selectionCanvas: null, // 蚂蚁线专用层(与 overlay 分离,互不擦除)
selectionCtx: null,
cursorCanvas: null, // 笔刷环专用层
cursorCtx: null,
_antFrame: 0, // 蚂蚁线 rAF 句柄
_antOffset: 0,
exportOptions: null, // { preset, format, bg, maxKB, size },来自 ctx.settings
interaction: 'idle', // 轻量交互状态机:idle | marquee | lasso | placing | cropping
renderAll: null, // 由 index.js 注入的统一重绘出口
lastExport: null // 观测口:{ bytes, width, height, format, preset }
}
export const runtime = { el: null }