四个问题来自同一张界面截图,都是真缺陷。 1) 三处颜色各司其职。'#ff4444'/'#ffffff'/'#000000'/0.68 这些默认值原先散在 state.js、text.js、events.js 三处,改一处另两处不跟 —— 界面上就成了「同一个 概念三种颜色」。现在 text.js 导出 TEXT_DEFAULTS 做单点定义,state.js 与选项栏 都从这里取。三个色块统一外观但标记「生效状态」:描边宽度 0 / 底色关闭 / 图形处于描边模式时,色块显示灰色斜纹(不显示颜色本身),语义是「这里还没有 颜色」;只做半透明灰化不够,深色照样是一块黑方块。文字底色默认从纯黑 #000000 改为中性近黑 #101014(68% 透明),色块按真实不透明度渲染。选描边色时若宽度 为 0 自动给 6px,与「选底色即开底色」同一条原则。 2) 选中框从虚线改实线。虚线在修图软件里的语义是像素选区(蚂蚁线),文字外面 套一圈虚线会被当成「多出来一个删不掉的选区」。objects.js 的文字/形状、 layers.js 的位图图层三处同步改为实线,虚线只留给真正的像素选区。 3) 选区命令(全选/反选/取消)归位到选区工具。它们操作的是像素选区,跟文字对象 无关:以前挂在「选中文字/图形」的选项栏里,选中文字就冒出来;而矩形/套索 选区工具反而没有。现在选区工具有,选择工具「未选中任何元素」时也有。 4) 矢量图形补上描边色与填充色。每个形状新增独立 fillColor;描边色不再另起一份, 直接复用主色(左侧色板/吸管/选项栏一个真相源,四处永远同步)。fillColor===null 表示跟随描边色,于是新画的实心图形是同色实心,换主色整块跟着变。 矩形/圆形/钢笔/箭头的选项栏都出「描边 ■ / 填充 ■」色块(箭头无填充区域,只给 描边色);选中对象读它自己的值,没选中时改的是新建默认值。选填充色即自动切 实心,色块从斜纹变成实际颜色。「样式 描边/实心」也改成读选中对象自己的 fill (以前只看全局默认值,选中已实心的矩形却高亮「描边」,和画面反着来)。 顺带简化:文本样式与形状颜色共用同一套色块控件与 CSS,is-off 用斜纹 + 虚线边框;底色色块用 --sw-a 变量体现半透明。 harness 162 → 172 条全通(新增:默认色不写死、色块生效态、选区命令归位、 选中框实线覆盖率 ≥0.85、图形描边/填充色与实际像素);validate 9/9。 harness 自身两处改法:清选中改用「切到别的工具再切回」(画布上早有别的用例 留下的图形,点空白会点到它们);像素断言改成全画布统计同色像素 (矢量画布含 overlayPad 外扩,按图片坐标换算单点会采到别处)。
175 lines
6.6 KiB
JavaScript
175 lines
6.6 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
|
||
|
||
/**
|
||
* 文字默认样式:**唯一的真相源**。
|
||
* 以前 '#ff4444' / '#ffffff' / '#000000' / 0.68 散落在 state.js、text.js、
|
||
* events.js 三处,改一处另两处不跟,界面上就会出现「同一个概念三种颜色」——
|
||
* 用户实测反馈「字体颜色有 3 个位置,那个黑色应该换成默认的参数」正是这么来的。
|
||
* 底色默认也不再是纯黑 #000000:纯黑在深色/花色底图上像抠了个洞,
|
||
* 而且和「文字色」看起来像是同一件事。改成中性近黑 #101014 + 68% 透明。
|
||
*/
|
||
export const TEXT_DEFAULTS = {
|
||
color: '#ff4444',
|
||
strokeWidth: 0,
|
||
strokeColor: '#ffffff',
|
||
bgFill: false,
|
||
bgColor: '#101014',
|
||
bgAlpha: 0.68
|
||
}
|
||
|
||
/** 底色块相对字号的 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 ? TEXT_DEFAULTS.bgAlpha : t.bgAlpha)
|
||
ctx.fillStyle = t.bgColor || TEXT_DEFAULTS.bgColor
|
||
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 || TEXT_DEFAULTS.strokeColor
|
||
ctx.strokeText(m.lines[i], ox, y)
|
||
}
|
||
ctx.fillStyle = t.color || TEXT_DEFAULTS.color
|
||
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 || TEXT_DEFAULTS.strokeColor,
|
||
bgFill: !!s.bgFill,
|
||
bgColor: s.bgColor || TEXT_DEFAULTS.bgColor,
|
||
bgAlpha: s.bgAlpha == null ? TEXT_DEFAULTS.bgAlpha : Number(s.bgAlpha)
|
||
}
|
||
}
|