Skip to content

AI-native design editor. Open-source Figma alternative.

Extract remaining magic numbers to constants

Danila Poyarkovab9b4ac37c41
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 2979689c6..2f8c20d31 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -2,6 +2,11 @@ import { useEventListener } from '@vueuse/core'
import { ref, type Ref } from 'vue'
import { computeSelectionBounds, computeSnap } from '../engine/snap'
+import {
+ AUTO_LAYOUT_BREAK_THRESHOLD, HANDLE_HIT_RADIUS, ROTATION_HIT_RADIUS,
+ PEN_CLOSE_THRESHOLD, ROTATION_SNAP_DEGREES, ROTATION_HIT_OFFSET,
+ DEFAULT_TEXT_WIDTH, DEFAULT_TEXT_HEIGHT
+} from '../constants'
import type { NodeType, SceneNode } from '../engine/scene-graph'
import type { EditorStore, Tool } from '../stores/editor'
@@ -25,8 +30,6 @@ interface DragMove {
brokeFromAutoLayout?: boolean
}
-const AUTO_LAYOUT_BREAK_THRESHOLD = 8
-
interface DragPan {
type: 'pan'
startScreenX: number
@@ -75,8 +78,7 @@ const TOOL_TO_NODE: Partial<Record<Tool, NodeType>> = {
TEXT: 'TEXT'
}
-const HANDLE_HIT_RADIUS = 6
-const ROTATION_HIT_RADIUS = 8
+
const HANDLE_CURSORS: Record<HandlePosition, string> = {
nw: 'nwse-resize',
@@ -194,7 +196,7 @@ function hitTestRotationHandle(
const ur = unrotate(sx, sy, cx, cy, rotation)
const mx = (x1 + x2) / 2
- const rotY = y1 - 24
+ const rotY = y1 - ROTATION_HIT_OFFSET
return Math.abs(ur.sx - mx) < ROTATION_HIT_RADIUS && Math.abs(ur.sy - rotY) < ROTATION_HIT_RADIUS
}
@@ -386,7 +388,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
// Text tool: click to create text node
if (tool === 'TEXT') {
- const nodeId = store.createShape('TEXT', cx, cy, 200, 24)
+ const nodeId = store.createShape('TEXT', cx, cy, DEFAULT_TEXT_WIDTH, DEFAULT_TEXT_HEIGHT)
store.graph.updateNode(nodeId, { text: '' })
store.select([nodeId])
store.startTextEditing(nodeId)
@@ -416,7 +418,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
const first = store.state.penState.vertices[0]
if (store.state.penState.vertices.length > 2 && first) {
const dist = Math.hypot(cx - first.x, cy - first.y)
- store.penSetClosingToFirst(dist < 8)
+ store.penSetClosingToFirst(dist < PEN_CLOSE_THRESHOLD)
}
store.requestRender()
}
@@ -497,7 +499,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
// Shift → snap to 15° increments
if (e.shiftKey) {
- rotation = Math.round(rotation / 15) * 15
+ rotation = Math.round(rotation / ROTATION_SNAP_DEGREES) * ROTATION_SNAP_DEGREES
}
// Normalize to -180..180
diff --git a/src/constants.ts b/src/constants.ts
index 32cdabc88..62b04ce2c 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -38,7 +38,44 @@ export const RULER_MINOR_TICK = 0.25
export const RULER_HIGHLIGHT_ALPHA = 0.3
export const PEN_HANDLE_RADIUS = 3
+export const PEN_VERTEX_RADIUS = 4
+export const PEN_CLOSE_RADIUS_BOOST = 2
+export const PEN_PATH_STROKE_WIDTH = 2
+export const PEN_CLOSE_THRESHOLD = 8
export const PARENT_OUTLINE_ALPHA = 0.5
+export const PARENT_OUTLINE_DASH = 4
export const DEFAULT_FONT_SIZE = 14
export const LABEL_FONT_SIZE = 11
export const SIZE_FONT_SIZE = 10
+
+export const ROTATION_HANDLE_RADIUS = 4
+export const ROTATION_SNAP_DEGREES = 15
+export const ROTATION_HIT_OFFSET = 24
+
+export const HANDLE_HALF_SIZE = 3
+
+export const LABEL_OFFSET_Y = 8
+export const SIZE_PILL_PADDING_X = 6
+export const SIZE_PILL_PADDING_Y = 6
+export const SIZE_PILL_HEIGHT = 18
+export const SIZE_PILL_RADIUS = 4
+export const SIZE_PILL_TEXT_OFFSET_Y = 13
+
+export const MARQUEE_FILL_ALPHA = 0.08
+export const SELECTION_DASH_ALPHA = 0.6
+export const DROP_HIGHLIGHT_ALPHA = 0.8
+export const DROP_HIGHLIGHT_STROKE = 2
+
+export const LAYOUT_INDICATOR_STROKE = 2
+
+export const DEFAULT_TEXT_WIDTH = 200
+export const DEFAULT_TEXT_HEIGHT = 24
+
+export const AUTO_LAYOUT_BREAK_THRESHOLD = 8
+export const HANDLE_HIT_RADIUS = 6
+export const ROTATION_HIT_RADIUS = 8
+
+export const ZOOM_SENSITIVITY = 0.99
+
+export const RULER_TARGET_PIXEL_SPACING = 100
+export const RULER_MAJOR_TOLERANCE = 0.01
diff --git a/src/engine/renderer.ts b/src/engine/renderer.ts
index 4a60567da..422c9da3d 100644
--- a/src/engine/renderer.ts
+++ b/src/engine/renderer.ts
@@ -3,7 +3,15 @@ import {
RULER_SIZE, RULER_BG_COLOR, RULER_TICK_COLOR, RULER_TEXT_COLOR,
RULER_BADGE_HEIGHT, RULER_BADGE_PADDING, RULER_BADGE_RADIUS, RULER_BADGE_EXCLUSION,
RULER_TEXT_BASELINE, RULER_MAJOR_TICK, RULER_MINOR_TICK, RULER_HIGHLIGHT_ALPHA,
- PEN_HANDLE_RADIUS, PARENT_OUTLINE_ALPHA, DEFAULT_FONT_SIZE, LABEL_FONT_SIZE, SIZE_FONT_SIZE
+ PEN_HANDLE_RADIUS, PEN_VERTEX_RADIUS, PEN_CLOSE_RADIUS_BOOST, PEN_PATH_STROKE_WIDTH,
+ PARENT_OUTLINE_ALPHA, PARENT_OUTLINE_DASH,
+ DEFAULT_FONT_SIZE, LABEL_FONT_SIZE, SIZE_FONT_SIZE,
+ ROTATION_HANDLE_RADIUS, HANDLE_HALF_SIZE,
+ LABEL_OFFSET_Y, SIZE_PILL_PADDING_X, SIZE_PILL_PADDING_Y, SIZE_PILL_HEIGHT,
+ SIZE_PILL_RADIUS, SIZE_PILL_TEXT_OFFSET_Y,
+ MARQUEE_FILL_ALPHA, SELECTION_DASH_ALPHA, DROP_HIGHLIGHT_ALPHA, DROP_HIGHLIGHT_STROKE,
+ LAYOUT_INDICATOR_STROKE,
+ RULER_TARGET_PIXEL_SPACING, RULER_MAJOR_TOLERANCE
} from '../constants'
import type { SceneNode, SceneGraph, Fill } from './scene-graph'
import type { SnapGuide } from './snap'
@@ -92,7 +100,7 @@ export class SkiaRenderer {
this.parentOutlinePaint.setStrokeWidth(1)
this.parentOutlinePaint.setColor(this.selColor(PARENT_OUTLINE_ALPHA))
this.parentOutlinePaint.setAntiAlias(true)
- this.parentOutlinePaint.setPathEffect(ck.PathEffect.MakeDash([4, 4], 0))
+ this.parentOutlinePaint.setPathEffect(ck.PathEffect.MakeDash([PARENT_OUTLINE_DASH, PARENT_OUTLINE_DASH], 0))
this.snapPaint = new ck.Paint()
this.snapPaint.setStyle(ck.PaintStyle.Stroke)
@@ -242,7 +250,7 @@ export class SkiaRenderer {
this.drawHandle(canvas, x2, my)
// Rotation handle (line extending above + circle)
- const rotHandleY = y1 - ROTATION_HANDLE_OFFSET - 4
+ const rotHandleY = y1 - ROTATION_HANDLE_OFFSET - ROTATION_HANDLE_RADIUS
const rotLinePaint = new this.ck.Paint()
rotLinePaint.setStyle(this.ck.PaintStyle.Stroke)
rotLinePaint.setStrokeWidth(1)
@@ -254,8 +262,8 @@ export class SkiaRenderer {
rotFill.setStyle(this.ck.PaintStyle.Fill)
rotFill.setColor(this.ck.WHITE)
rotFill.setAntiAlias(true)
- canvas.drawCircle(mx, rotHandleY, 4, rotFill)
- canvas.drawCircle(mx, rotHandleY, 4, rotLinePaint)
+ canvas.drawCircle(mx, rotHandleY, ROTATION_HANDLE_RADIUS, rotFill)
+ canvas.drawCircle(mx, rotHandleY, ROTATION_HANDLE_RADIUS, rotLinePaint)
rotLinePaint.delete()
rotFill.delete()
@@ -303,7 +311,7 @@ export class SkiaRenderer {
labelPaint.setStyle(this.ck.PaintStyle.Fill)
labelPaint.setColor(this.selColor())
labelPaint.setAntiAlias(true)
- canvas.drawText(node.name, sx1, sy1 - 8, labelPaint, this.labelFont)
+ canvas.drawText(node.name, sx1, sy1 - LABEL_OFFSET_Y, labelPaint, this.labelFont)
labelPaint.delete()
}
}
@@ -316,24 +324,24 @@ export class SkiaRenderer {
const widths = this.sizeFont.getGlyphWidths(glyphIds)
let textWidth = 0
for (let i = 0; i < widths.length; i++) textWidth += widths[i]
- const pillW = textWidth + 12
- const pillH = 18
+ const pillW = textWidth + SIZE_PILL_PADDING_X * 2
+ const pillH = SIZE_PILL_HEIGHT
const pillX = smx - pillW / 2
- const pillY = sy2 + 6
+ const pillY = sy2 + SIZE_PILL_PADDING_Y
const pillPaint = new this.ck.Paint()
pillPaint.setStyle(this.ck.PaintStyle.Fill)
pillPaint.setColor(this.selColor())
pillPaint.setAntiAlias(true)
- const rrect = this.ck.RRectXY(this.ck.LTRBRect(pillX, pillY, pillX + pillW, pillY + pillH), 4, 4)
+ const rrect = this.ck.RRectXY(this.ck.LTRBRect(pillX, pillY, pillX + pillW, pillY + pillH), SIZE_PILL_RADIUS, SIZE_PILL_RADIUS)
canvas.drawRRect(rrect, pillPaint)
const textPaint = new this.ck.Paint()
textPaint.setStyle(this.ck.PaintStyle.Fill)
textPaint.setColor(this.ck.WHITE)
textPaint.setAntiAlias(true)
- canvas.drawText(sizeText, pillX + 6, pillY + 13, textPaint, this.sizeFont)
+ canvas.drawText(sizeText, pillX + SIZE_PILL_PADDING_X, pillY + SIZE_PILL_TEXT_OFFSET_Y, textPaint, this.sizeFont)
pillPaint.delete()
textPaint.delete()
@@ -426,7 +434,7 @@ export class SkiaRenderer {
const dashPaint = new this.ck.Paint()
dashPaint.setStyle(this.ck.PaintStyle.Stroke)
dashPaint.setStrokeWidth(1)
- dashPaint.setColor(this.selColor(0.6))
+ dashPaint.setColor(this.selColor(SELECTION_DASH_ALPHA))
dashPaint.setAntiAlias(true)
canvas.drawRect(this.ck.LTRBRect(minX, minY, maxX, maxY), dashPaint)
@@ -464,12 +472,11 @@ export class SkiaRenderer {
}
private drawHandle(canvas: Canvas, x: number, y: number): void {
- const S = 3
const handleFill = new this.ck.Paint()
handleFill.setStyle(this.ck.PaintStyle.Fill)
handleFill.setColor(this.ck.WHITE)
- const rect = this.ck.LTRBRect(x - S, y - S, x + S, y + S)
+ const rect = this.ck.LTRBRect(x - HANDLE_HALF_SIZE, y - HANDLE_HALF_SIZE, x + HANDLE_HALF_SIZE, y + HANDLE_HALF_SIZE)
canvas.drawRect(rect, handleFill)
canvas.drawRect(rect, this.selectionPaint)
handleFill.delete()
@@ -511,7 +518,7 @@ export class SkiaRenderer {
const fill = new this.ck.Paint()
fill.setStyle(this.ck.PaintStyle.Fill)
- fill.setColor(this.selColor(0.08))
+ fill.setColor(this.selColor(MARQUEE_FILL_ALPHA))
canvas.drawRect(rect, fill)
canvas.drawRect(rect, this.selectionPaint)
fill.delete()
@@ -527,7 +534,7 @@ export class SkiaRenderer {
const paint = new this.ck.Paint()
paint.setStyle(this.ck.PaintStyle.Stroke)
- paint.setStrokeWidth(2)
+ paint.setStrokeWidth(LAYOUT_INDICATOR_STROKE)
paint.setColor(this.selColor())
paint.setAntiAlias(true)
@@ -582,8 +589,8 @@ export class SkiaRenderer {
if (overlays.dropTargetId === nodeId) {
const highlight = new this.ck.Paint()
highlight.setStyle(this.ck.PaintStyle.Stroke)
- highlight.setStrokeWidth(2 / this.zoom)
- highlight.setColor(this.selColor(0.8))
+ highlight.setStrokeWidth(DROP_HIGHLIGHT_STROKE / this.zoom)
+ highlight.setColor(this.selColor(DROP_HIGHLIGHT_ALPHA))
highlight.setAntiAlias(true)
canvas.drawRect(this.ck.LTRBRect(0, 0, node.width, node.height), highlight)
highlight.delete()
@@ -716,10 +723,10 @@ export class SkiaRenderer {
textStyle: {
color: this.fillPaint.getColor(),
fontFamilies: [node.fontFamily || 'Inter'],
- fontSize: node.fontSize || 14,
+ fontSize: node.fontSize || DEFAULT_FONT_SIZE,
fontStyle: { weight: { value: node.fontWeight || 400 } as FontWeight },
letterSpacing: node.letterSpacing || 0,
- heightMultiplier: node.lineHeight ? node.lineHeight / (node.fontSize || 14) : undefined
+ heightMultiplier: node.lineHeight ? node.lineHeight / (node.fontSize || DEFAULT_FONT_SIZE) : undefined
}
})
const builder = this.ck.ParagraphBuilder.MakeFromFontProvider(paraStyle, this.fontProvider)
@@ -730,7 +737,7 @@ export class SkiaRenderer {
paragraph.delete()
builder.delete()
} else if (this.textFont) {
- canvas.drawText(text, 0, node.fontSize || 14, this.fillPaint, this.textFont)
+ canvas.drawText(text, 0, node.fontSize || DEFAULT_FONT_SIZE, this.fillPaint, this.textFont)
}
}
@@ -766,11 +773,9 @@ export class SkiaRenderer {
if (!penState || penState.vertices.length === 0) return
const { vertices, segments, dragTangent, cursorX, cursorY } = penState
- const VERTEX_RADIUS = 4
-
const pathPaint = new this.ck.Paint()
pathPaint.setStyle(this.ck.PaintStyle.Stroke)
- pathPaint.setStrokeWidth(2)
+ pathPaint.setStrokeWidth(PEN_PATH_STROKE_WIDTH)
pathPaint.setColor(this.selColor())
pathPaint.setAntiAlias(true)
@@ -787,7 +792,7 @@ export class SkiaRenderer {
const vertexStroke = new this.ck.Paint()
vertexStroke.setStyle(this.ck.PaintStyle.Stroke)
- vertexStroke.setStrokeWidth(2)
+ vertexStroke.setStrokeWidth(PEN_PATH_STROKE_WIDTH)
vertexStroke.setColor(this.selColor())
vertexStroke.setAntiAlias(true)
@@ -875,7 +880,7 @@ export class SkiaRenderer {
// Draw vertices
for (let i = 0; i < vertices.length; i++) {
const v = toScreen(vertices[i].x, vertices[i].y)
- const radius = i === 0 && penState.closingToFirst ? VERTEX_RADIUS + 2 : VERTEX_RADIUS
+ const radius = i === 0 && penState.closingToFirst ? PEN_VERTEX_RADIUS + PEN_CLOSE_RADIUS_BOOST : PEN_VERTEX_RADIUS
canvas.drawCircle(v.x, v.y, radius, vertexFill)
canvas.drawCircle(v.x, v.y, radius, vertexStroke)
}
@@ -954,7 +959,7 @@ export class SkiaRenderer {
for (let wx = startX; wx <= worldRight; wx += minorStep) {
const sx = wx * this.zoom + this.panX
if (sx < R) continue
- const isMajor = Math.abs(wx % step) < 0.01
+ const isMajor = Math.abs(wx % step) < RULER_MAJOR_TOLERANCE
const tickLen = isMajor ? R * RULER_MAJOR_TICK : R * RULER_MINOR_TICK
canvas.drawLine(sx, R - tickLen, sx, R, tickPaint)
@@ -979,7 +984,7 @@ export class SkiaRenderer {
for (let wy = startY; wy <= worldBottom; wy += minorStep) {
const sy = wy * this.zoom + this.panY
if (sy < R) continue
- const isMajor = Math.abs(wy % step) < 0.01
+ const isMajor = Math.abs(wy % step) < RULER_MAJOR_TOLERANCE
const tickLen = isMajor ? R * RULER_MAJOR_TICK : R * RULER_MINOR_TICK
canvas.drawLine(R - tickLen, sy, R, sy, tickPaint)
@@ -1060,8 +1065,7 @@ export class SkiaRenderer {
private rulerStep(): number {
const pixelsPerUnit = this.zoom
- const targetPixelSpacing = 100
- const rawStep = targetPixelSpacing / pixelsPerUnit
+ const rawStep = RULER_TARGET_PIXEL_SPACING / pixelsPerUnit
const magnitude = Math.pow(10, Math.floor(Math.log10(rawStep)))
const normalized = rawStep / magnitude
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index b8f323375..2c86ecc3f 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -1,6 +1,6 @@
import { reactive, shallowRef, computed } from 'vue'
-import { DEFAULT_SHAPE_FILL, DEFAULT_FRAME_FILL, CANVAS_BG_COLOR } from '../constants'
+import { DEFAULT_SHAPE_FILL, DEFAULT_FRAME_FILL, CANVAS_BG_COLOR, ZOOM_SENSITIVITY } from '../constants'
import type { Color } from '../types'
import {
parseFigmaClipboard,
@@ -976,7 +976,7 @@ export function createEditorStore() {
}
function applyZoom(delta: number, centerX: number, centerY: number) {
- const factor = Math.pow(0.99, delta)
+ const factor = Math.pow(ZOOM_SENSITIVITY, delta)
const newZoom = Math.max(0.02, Math.min(256, state.zoom * factor))
state.panX = centerX - (centerX - state.panX) * (newZoom / state.zoom)
state.panY = centerY - (centerY - state.panY) * (newZoom / state.zoom)