文字从「字号 + 颜色」扩到完整样式:字重(常规/粗/特粗)、对齐(左/中/右)、 行距、描边、底色块、多行(textarea + Shift+Enter)。新增叶子模块 text.js, fontOf / textMetrics / textBox / drawTextObject 成为唯一事实来源, objects.js(预览/命中/包围盒)与 layers.js(栅格化)共用,避免量一套画一套; 描边与底色撑大包围盒,手柄、吸附、缩略图随之变化。单行无描边无底色的老文字 包围盒与旧实现逐像素一致,老作业打开不变形。 吸附参考线:snap.js 只做纯计算。目标线为画布 0/中/满 + 其他可见元素的左中右、 上中下;阈值 6 屏幕像素按 viewScale() 折回图片像素,磁力不随缩放变大; 命中画洋红参考线(区别于选中框的橙),松手清空;Alt 全程禁用。 位图/文字/形状三条拖动路径都接,文字需补 b.x - t.x 原点偏移。 矩形圆角:shapeRadius 进 drawShape,半径夹到 min(w,h)/2,路径手写 arcTo (不用 ctx.roundRect,老内核没有)。最近使用颜色最多 8 个,存 ctx.settings 跨会话保留,只有落定的改色才记录;色板改用事件委托以覆盖动态插入的色块。 导出「复制」:结果编成 PNG 写 ClipboardItem,可直接粘进聊天窗口/后台上传框 且不关闭编辑器。优先 createImageBitmap 绕开宿主 CSP 的 img-src 限制; 非安全上下文无 navigator.clipboard 时明确提示改用「导出」而非静默失败。 顺带修:选项栏中文双字标签加 white-space:nowrap(否则 36px 一行内竖排成 「常/规」);.iwb-editor-optionbar-tip 补基础样式(原来只有 display:none, 裸 span 参与 flex 收缩被挤成两行贴边);文字工具的 hint 移到按钮之后, 放不下时让说明滚出视野而不是把操作按钮顶没。 harness 140 → 158 条,全部通过;validate 9/9。
95 lines
3.4 KiB
JavaScript
95 lines
3.4 KiB
JavaScript
/* snap.js - 拖动吸附与参考线(v1.9)
|
|
*
|
|
* 只做两件事:算「要不要吸、吸多少」,以及把参考线画出来。
|
|
* 不碰对象的写入 —— 调用方拿到 dx/dy 自己应用到 x/y,再在 render 里画线。
|
|
* 这样位图、文字、形状三种拖动可以共用同一套吸附,不会各写一遍阈值。
|
|
*
|
|
* 吸附目标:画布中线/四边 + 其它可见元素的左/中/右与上/中/下。
|
|
* 阈值按屏幕像素给(6px),换算成图片像素后再比较 —— 否则放大图片时
|
|
* 吸附范围会跟着放大到几十像素,鼠标还没靠近就被拽走。
|
|
*/
|
|
|
|
'use strict'
|
|
|
|
import { S } from './state.js'
|
|
import { viewScale } from './scale.js'
|
|
|
|
/** 吸附触发距离(屏幕像素) */
|
|
export const SNAP_SCREEN_PX = 6
|
|
|
|
/** 参考线颜色:洋红,和选中框的橙色区分开 */
|
|
const GUIDE_COLOR = '#ff2d95'
|
|
|
|
function pushBox(targets, box) {
|
|
const x2 = box.x + box.w
|
|
const y2 = box.y + box.h
|
|
targets.x.push(box.x, box.x + box.w / 2, x2)
|
|
targets.y.push(box.y, box.y + box.h / 2, y2)
|
|
}
|
|
|
|
/** 在候选线里找「离得最近且不超过阈值」的那条,返回需要平移的量 */
|
|
function nearest(targets, sources, tol, axis) {
|
|
let best = null
|
|
for (let i = 0; i < targets.length; i++) {
|
|
for (let j = 0; j < sources.length; j++) {
|
|
const delta = targets[i] - sources[j]
|
|
const abs = Math.abs(delta)
|
|
if (abs > tol) continue
|
|
if (!best || abs < Math.abs(best.delta)) best = { delta: delta, at: targets[i], axis: axis }
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
/**
|
|
* 计算吸附位移。
|
|
* @param {{x:number,y:number,w:number,h:number}} box 被拖元素的包围盒(已按指针位移算好,未吸附)
|
|
* @param {Array<{x:number,y:number,w:number,h:number}>} others 其它元素的包围盒
|
|
* @returns {{dx:number, dy:number, guides:Array}}
|
|
*/
|
|
export function snapMove(box, others) {
|
|
const tol = SNAP_SCREEN_PX / Math.max(0.02, viewScale())
|
|
const targets = { x: [0, S.imgW / 2, S.imgW], y: [0, S.imgH / 2, S.imgH] }
|
|
for (const o of others || []) {
|
|
if (!o || !isFinite(o.x) || !isFinite(o.y)) continue
|
|
pushBox(targets, o)
|
|
}
|
|
const sourcesX = [box.x, box.x + box.w / 2, box.x + box.w]
|
|
const sourcesY = [box.y, box.y + box.h / 2, box.y + box.h]
|
|
const gx = nearest(targets.x, sourcesX, tol, 'x')
|
|
const gy = nearest(targets.y, sourcesY, tol, 'y')
|
|
const guides = []
|
|
if (gx) guides.push(gx)
|
|
if (gy) guides.push(gy)
|
|
return { dx: gx ? gx.delta : 0, dy: gy ? gy.delta : 0, guides: guides }
|
|
}
|
|
|
|
/** 画参考线(贯穿整个画布 + overlay 外扩区,拖动时一眼看到对齐到哪) */
|
|
export function drawGuides(ctx, guides) {
|
|
if (!guides || !guides.length || !ctx) return
|
|
const scale = viewScale()
|
|
const pad = S.overlayPad || 0
|
|
ctx.save()
|
|
ctx.strokeStyle = GUIDE_COLOR
|
|
ctx.lineWidth = 1 / Math.max(0.02, scale)
|
|
ctx.setLineDash([6 / Math.max(0.02, scale), 4 / Math.max(0.02, scale)])
|
|
for (const g of guides) {
|
|
ctx.beginPath()
|
|
if (g.axis === 'x') {
|
|
ctx.moveTo(g.at, -pad)
|
|
ctx.lineTo(g.at, S.imgH + pad)
|
|
} else {
|
|
ctx.moveTo(-pad, g.at)
|
|
ctx.lineTo(S.imgW + pad, g.at)
|
|
}
|
|
ctx.stroke()
|
|
}
|
|
ctx.setLineDash([])
|
|
ctx.restore()
|
|
}
|
|
|
|
export function clearGuides() {
|
|
if (!S.guides) return
|
|
S.guides = null
|
|
}
|