feat(image-annotate): v1.9.0 文字样式包、吸附参考线、圆角与最近色、复制到剪贴板
文字从「字号 + 颜色」扩到完整样式:字重(常规/粗/特粗)、对齐(左/中/右)、 行距、描边、底色块、多行(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。
This commit is contained in:
1 parent
040b08fe28
commit
e699c22fa0
17 files changed
+1678
-99
No files matched your search
@@ -294,3 +294,64 @@ import { toast } from './commands.js'
|
||||
setTimeout(() => { try { URL.revokeObjectURL(url) } catch (e) { /* ignore */ } }, 2000)
|
||||
toast('已下载 ' + name + '(' + Math.round(blob.size / 1024) + 'KB)', 'success')
|
||||
}
|
||||
|
||||
/** 任意图片 blob → PNG blob(剪贴板对 image/png 支持最稳,体积在剪贴板里无所谓) */
|
||||
async function toPngBlob(blob) {
|
||||
// 优先 createImageBitmap:不经过 URL,绕开宿主 CSP 对 img-src 的限制
|
||||
try {
|
||||
if (typeof createImageBitmap === 'function') {
|
||||
const bmp = await createImageBitmap(blob)
|
||||
const c = document.createElement('canvas')
|
||||
c.width = bmp.width
|
||||
c.height = bmp.height
|
||||
c.getContext('2d').drawImage(bmp, 0, 0)
|
||||
if (typeof bmp.close === 'function') bmp.close()
|
||||
const out = await new Promise(res => c.toBlob(b => res(b), 'image/png'))
|
||||
c.width = 0
|
||||
c.height = 0
|
||||
if (out) return out
|
||||
}
|
||||
} catch (e) { /* 走 img 回退 */ }
|
||||
return await new Promise((resolve) => {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const img = new Image()
|
||||
img.onload = () => {
|
||||
const c = document.createElement('canvas')
|
||||
c.width = img.naturalWidth
|
||||
c.height = img.naturalHeight
|
||||
c.getContext('2d').drawImage(img, 0, 0)
|
||||
try { URL.revokeObjectURL(url) } catch (e) { /* ignore */ }
|
||||
c.toBlob(b => resolve(b || blob), 'image/png')
|
||||
}
|
||||
img.onerror = () => {
|
||||
try { URL.revokeObjectURL(url) } catch (e) { /* ignore */ }
|
||||
resolve(blob)
|
||||
}
|
||||
img.src = url
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制到系统剪贴板:运营场景最常用的一条路 —— 复制后直接粘进聊天窗口/小红书后台,
|
||||
* 省掉「下载 → 再上传」。必须在用户手势里调用(浏览器要求),失败给可操作提示。
|
||||
*/
|
||||
export async function copyBlobToClipboard(blob) {
|
||||
const nav = typeof navigator !== 'undefined' ? navigator : null
|
||||
const CI = typeof window !== 'undefined' ? window.ClipboardItem : null
|
||||
if (!nav || !nav.clipboard || typeof nav.clipboard.write !== 'function' || typeof CI !== 'function') {
|
||||
toast('当前浏览器不支持复制图片,请用「导出」下载', 'warn')
|
||||
return false
|
||||
}
|
||||
let png = blob
|
||||
if (blob.type !== 'image/png') {
|
||||
try { png = await toPngBlob(blob) } catch (e) { png = blob }
|
||||
}
|
||||
try {
|
||||
await nav.clipboard.write([new CI({ 'image/png': png })])
|
||||
toast('已复制到剪贴板(' + Math.round(png.size / 1024) + 'KB),可直接粘贴', 'success')
|
||||
return true
|
||||
} catch (e) {
|
||||
toast('复制失败:' + ((e && e.message) || '浏览器拒绝了剪贴板写入') + ';可用「导出」下载', 'error')
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user