You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Konva.Text在Canvas中显示分数(如2¼)

Nice question! When it comes to displaying mixed fractions like 2¼ using Konva.Text, you’ve got two reliable approaches—pick the one that fits your needs best.

1. Quick Win: Use Unicode Fraction Characters

For common fractions (like ¼, ½, ¾), the simplest solution is to use pre-built Unicode characters. These render seamlessly in Konva.Text just like regular text, no extra setup needed.

Here’s a quick example:

// Initialize stage and layer
const stage = new Konva.Stage({
  container: 'container',
  width: 200,
  height: 100
});

const layer = new Konva.Layer();
stage.add(layer);

// Render mixed fraction with Unicode
const mixedFraction = new Konva.Text({
  x: 50,
  y: 30,
  text: '2¼',
  fontSize: 36,
  fontFamily: 'Arial',
  fill: '#333'
});

layer.add(mixedFraction);
layer.draw();

This works out of the box for most common fractions. A few useful Unicode values to keep handy:

  • ¼ = U+00BC
  • ½ = U+00BD
  • ¾ = U+00BE
  • ⅛ = U+215B
  • ⅜ = U+215C
  • ⅝ = U+215D
  • ⅞ = U+215E

The downside? It won’t work for less common fractions (like 2/7) since there’s no Unicode character for every possible fraction.

2. Fully Customizable: Build a Fraction with Konva Groups

If you need to display arbitrary fractions (or want full control over styling), combine multiple Konva.Text components (plus a simple rectangle for the fraction line) in a Konva.Group. This lets you position the integer, numerator, denominator, and line exactly how you want.

Here’s a complete example:

const stage = new Konva.Stage({
  container: 'container',
  width: 200,
  height: 100
});

const layer = new Konva.Layer();
stage.add(layer);

// Create a group to hold the entire mixed fraction
const mixedFractionGroup = new Konva.Group({
  x: 50,
  y: 20
});

// Integer part (2)
const integerText = new Konva.Text({
  text: '2',
  fontSize: 36,
  fontFamily: 'Arial',
  fill: '#333',
  y: 8 // Adjust to align with the fraction's baseline
});
mixedFractionGroup.add(integerText);

// Group for the fraction part (1/4)
const fractionGroup = new Konva.Group({
  x: integerText.width() + 5 // Add space between integer and fraction
});

// Numerator (1)
const numeratorText = new Konva.Text({
  text: '1',
  fontSize: 18,
  fontFamily: 'Arial',
  fill: '#333',
  x: 3 // Center numerator over the line
});
fractionGroup.add(numeratorText);

// Fraction line (using a rectangle)
const fractionLine = new Konva.Rect({
  x: 0,
  y: numeratorText.height() + 2,
  width: numeratorText.width() + 6,
  height: 1,
  fill: '#333'
});
fractionGroup.add(fractionLine);

// Denominator (4)
const denominatorText = new Konva.Text({
  text: '4',
  fontSize: 18,
  fontFamily: 'Arial',
  fill: '#333',
  y: fractionLine.y() + fractionLine.height() + 2
});
fractionGroup.add(denominatorText);

mixedFractionGroup.add(fractionGroup);
layer.add(mixedFractionGroup);
layer.draw();

With this approach, you can tweak:

  • Font sizes for the integer vs. fraction parts
  • Spacing between elements
  • The thickness/color of the fraction line
  • Alignment to match your design

Pro tip: Wrap this logic into a reusable function that takes integer, numerator, denominator as parameters—this will save you time if you need to render multiple mixed fractions!

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:31:49