1) 输入框偏到左上:showTextInput 用 canvasStack 的矩形换算,但 stack 尺寸是
(imgW + 2*overlayPad)*scale、图片在 stack 内还偏移 pad*scale(图层 canvas 的
left/top)。比例偏小又漏偏移,两个误差同向叠加。改用 textCanvas 的矩形
(与图层 canvas 同基准,精确等于图片显示区域),并按 textarea 的边框 2px +
内边距 8/4px 反向偏移,让输入框里的文字起点正压在点击点上;提交后
drawTextObject 也从 t.x/t.y 起画,所见即所得。
2) 选「选择」工具后三个选区图标不见了:两处叠加。
- selCmdHTML 排在 hint 之后,而 .iwb-opt-hint 带 margin-left:auto;
- 更关键的是 .iwb-opt-sep 写成 margin: 0 4px 0 auto,分隔线自带 auto,
会把分隔线之后的内容整组推到最右,选项栏排满时直接滚出可视区。
修法:分隔线只分组(margin: 0 2px),留白交给末尾 hint;选区按钮排在 hint 之前。
现在「文字」「选择」两个工具下都是同一组三个图标,未选中元素时也在。
3) 设了底色看不到:底色一直在画,只是「只改颜色没点开关」时 bgFill 仍是 false。
- 改 bgColor 时自动打开 bgFill,按钮 active 同步;
- 顺带修掉更硬的 bug:「底色」按钮只能开不能关 —— data-value 在生成 HTML 时
写死,状态变了没人更新,每次点都传「开启」。改由 syncOptionBar 按当前状态翻面;
- 默认底色透明度 0.55 → 0.68(浅色/花色底图上 0.55 太透,容易被当成没生效)。
harness 158 → 162 条全通(新增输入框落点误差 ≤3px、Esc 不留空对象、
选择工具未选中仍有 3 个图标、只选颜色即自动开底色);validate 9/9。
158 lines
5.8 KiB
JavaScript
158 lines
5.8 KiB
JavaScript
/* text.js - 文字的字体串、度量与绘制(v1.9)
|
||
*
|
||
* 为什么单独一个模块:文字要在两处画 —— 落层(layers.js#drawVectorToCanvas)与
|
||
* 预览/度量(objects.js)。两边各写一遍必然出现「量出来的框和画出来的字不一样」。
|
||
* 这里做成零依赖叶子模块(不 import 任何编辑器模块),objects.js 与 layers.js
|
||
* 都只往下依赖它,不会形成循环 import(esbuild 打成 IIFE 时循环 import 会拿到 undefined)。
|
||
*
|
||
* 坐标语义(与 v1.8 之前保持一致,勿随意改动):
|
||
* t.x / t.y 是「文字块」左上角;底色 padding 与描边从这一点向内/外扩展。
|
||
* 单行 + 无底色 + 无描边时,textBox() 与老版 textBounds() 完全相等(w=文字宽,h=fontSize×1.25)。
|
||
*/
|
||
|
||
'use strict'
|
||
|
||
/** 默认行高倍数:必须等于老版的 fontSize×1.25,否则单行文字的选中框会变高 */
|
||
export const TEXT_LINE_HEIGHT = 1.25
|
||
|
||
/** 底色块相对字号的 padding 系数 */
|
||
const BG_PAD_RATIO = 0.3
|
||
|
||
let _measureCtx = null
|
||
|
||
/** 度量用的离屏 ctx:不依赖编辑器 DOM 是否已建好,避免 textBounds 在早期调用时抛错 */
|
||
function measureCtx() {
|
||
if (!_measureCtx) {
|
||
const c = document.createElement('canvas')
|
||
c.width = 8
|
||
c.height = 8
|
||
_measureCtx = c.getContext('2d')
|
||
}
|
||
return _measureCtx
|
||
}
|
||
|
||
/** 字重:'bold' | 'normal' | '900' …(默认粗体,与老版一致) */
|
||
export function weightOf(t) {
|
||
const w = t && t.fontWeight
|
||
return w == null || w === '' ? 'bold' : String(w)
|
||
}
|
||
|
||
export function fontOf(t) {
|
||
const size = Math.max(4, Number(t && t.fontSize) || 32)
|
||
return weightOf(t) + ' ' + size + 'px sans-serif'
|
||
}
|
||
|
||
/** 文字的排布度量:分行、每行宽度、行高、底色 padding、描边宽度 */
|
||
export function textMetrics(t) {
|
||
const ctx = measureCtx()
|
||
const font = fontOf(t)
|
||
ctx.font = font
|
||
const raw = (t && t.text) == null ? '' : String(t.text)
|
||
const lines = raw.split('\n')
|
||
const widths = lines.map(line => ctx.measureText(line).width)
|
||
const size = Math.max(4, Number(t && t.fontSize) || 32)
|
||
const lh = size * (Number(t && t.lineHeight) || TEXT_LINE_HEIGHT)
|
||
const w = lines.length ? Math.max.apply(null, widths) : 0
|
||
const pad = t && t.bgFill ? Math.round(size * BG_PAD_RATIO) : 0
|
||
const sw = Math.max(0, Number(t && t.strokeWidth) || 0)
|
||
return { lines, widths, w, h: lh * lines.length, lh, pad, sw, font, size }
|
||
}
|
||
|
||
/** 包围盒(图片坐标):含底色 padding 与描边向外扩出的一半 */
|
||
export function textBox(t) {
|
||
const m = textMetrics(t)
|
||
const half = m.sw / 2
|
||
return {
|
||
x: t.x - half,
|
||
y: t.y - half,
|
||
w: Math.max(m.w, 2) + m.pad * 2 + m.sw,
|
||
h: Math.max(m.h, 1) + m.pad * 2 + m.sw
|
||
}
|
||
}
|
||
|
||
function alignOf(t) {
|
||
const a = t && t.align
|
||
return a === 'center' || a === 'right' ? a : 'left'
|
||
}
|
||
|
||
/**
|
||
* 画一段文字(含底色块、描边、多行、旋转)。
|
||
* 底色块与描边都按「块」来画:底色覆盖整块,描边沿字形外沿。
|
||
*/
|
||
export function drawTextObject(ctx, t) {
|
||
const m = textMetrics(t)
|
||
ctx.save()
|
||
ctx.textBaseline = 'top'
|
||
ctx.globalAlpha = t.opacity != null ? t.opacity : 1
|
||
const b = textBox(t)
|
||
const rot = t.rotation || 0
|
||
if (rot) {
|
||
const cx = b.x + b.w / 2
|
||
const cy = b.y + b.h / 2
|
||
ctx.translate(cx, cy)
|
||
ctx.rotate(rot * Math.PI / 180)
|
||
ctx.translate(-cx, -cy)
|
||
}
|
||
ctx.font = m.font
|
||
const align = alignOf(t)
|
||
const boxW = m.w + m.pad * 2
|
||
|
||
// 底色块:先于文字画,圆角按字号比例给一点(纯直角贴小字会很硬)
|
||
if (t.bgFill) {
|
||
const r = Math.min(m.pad + 2, Math.min(boxW, m.h + m.pad * 2) / 2)
|
||
// 0.55 在浅色/花色底图上偏透,用户容易以为「底色没生效」;0.68 既压得住底图,
|
||
// 也还看得见一点背景,是「垫一块板让字立起来」的常用手感。
|
||
ctx.globalAlpha = (t.opacity != null ? t.opacity : 1) * (t.bgAlpha == null ? 0.68 : t.bgAlpha)
|
||
ctx.fillStyle = t.bgColor || '#000000'
|
||
const bx = t.x, by = t.y
|
||
const bw = boxW, bh = m.h + m.pad * 2
|
||
ctx.beginPath()
|
||
if (typeof ctx.roundRect === 'function') {
|
||
ctx.roundRect(bx, by, bw, bh, r)
|
||
} else {
|
||
ctx.moveTo(bx + r, by)
|
||
ctx.arcTo(bx + bw, by, bx + bw, by + bh, r)
|
||
ctx.arcTo(bx + bw, by + bh, bx, by + bh, r)
|
||
ctx.arcTo(bx, by + bh, bx, by, r)
|
||
ctx.arcTo(bx, by, bx + bw, by, r)
|
||
ctx.closePath()
|
||
}
|
||
ctx.fill()
|
||
ctx.globalAlpha = (t.opacity != null ? t.opacity : 1)
|
||
}
|
||
|
||
// 对齐以「块内可用宽度(不含 padding)」为基准:多行时各行按这个宽度对齐
|
||
ctx.textAlign = align
|
||
const left = t.x + m.pad
|
||
const ox = align === 'center' ? left + m.w / 2 : align === 'right' ? left + m.w : left
|
||
|
||
ctx.lineJoin = 'round'
|
||
ctx.miterLimit = 2
|
||
for (let i = 0; i < m.lines.length; i++) {
|
||
const y = t.y + m.pad + i * m.lh
|
||
if (m.sw > 0) {
|
||
ctx.lineWidth = m.sw
|
||
ctx.strokeStyle = t.strokeColor || '#ffffff'
|
||
ctx.strokeText(m.lines[i], ox, y)
|
||
}
|
||
ctx.fillStyle = t.color || '#ff4444'
|
||
ctx.fillText(m.lines[i], ox, y)
|
||
}
|
||
ctx.restore()
|
||
}
|
||
|
||
/** 从默认样式造一段文字的字段(新建文字 / 老对象补默认值共用) */
|
||
export function newTextStyle(src) {
|
||
const s = src || {}
|
||
return {
|
||
fontWeight: weightOf(s),
|
||
align: alignOf(s),
|
||
lineHeight: Number(s.lineHeight) || TEXT_LINE_HEIGHT,
|
||
strokeWidth: Math.max(0, Number(s.strokeWidth) || 0),
|
||
strokeColor: s.strokeColor || '#ffffff',
|
||
bgFill: !!s.bgFill,
|
||
bgColor: s.bgColor || '#000000',
|
||
bgAlpha: s.bgAlpha == null ? 0.68 : Number(s.bgAlpha)
|
||
}
|
||
}
|