feat(canvas): 交互可读性 - 优化连线、字号与工作条
This commit is contained in:
1 parent
db9e18da1e
commit
ec5abc967e
12 files changed
+210
-57
No files matched your search
@@ -0,0 +1,19 @@
|
||||
const DEFAULT_TEXT_NODE_WIDTH = 340;
|
||||
const DEFAULT_TEXT_NODE_HEIGHT = 240;
|
||||
const DEFAULT_TEXT_FONT_SIZE = 14;
|
||||
const MIN_TEXT_FONT_SIZE = 8;
|
||||
const MAX_TEXT_FONT_SIZE = 72;
|
||||
|
||||
/**
|
||||
* Keep text readable while a text node is resized.
|
||||
*
|
||||
* The geometric mean makes the scale respond to both dimensions without making
|
||||
* a very wide, short node grow as aggressively as a purely width-based rule.
|
||||
*/
|
||||
export function canvasTextFontSize(width: number, height: number, baseFontSize = DEFAULT_TEXT_FONT_SIZE) {
|
||||
const safeWidth = Number.isFinite(width) && width > 0 ? width : DEFAULT_TEXT_NODE_WIDTH;
|
||||
const safeHeight = Number.isFinite(height) && height > 0 ? height : DEFAULT_TEXT_NODE_HEIGHT;
|
||||
const safeBaseFontSize = Number.isFinite(baseFontSize) && baseFontSize > 0 ? baseFontSize : DEFAULT_TEXT_FONT_SIZE;
|
||||
const scale = Math.sqrt((safeWidth / DEFAULT_TEXT_NODE_WIDTH) * (safeHeight / DEFAULT_TEXT_NODE_HEIGHT));
|
||||
return Math.min(MAX_TEXT_FONT_SIZE, Math.max(MIN_TEXT_FONT_SIZE, safeBaseFontSize * scale));
|
||||
}
|
||||
Reference in new issue
Block a user