Skip to content
On this page

Text Shape

TIP

This class is a descendant of Shape it also inherits all the options that Shape provides.

TIP

This component also supports a return function

To create an text shape with fcanvas, we can instantiate a Text object.

In addition, this shape also provides a few other parameters:

Require Options

NameTypeDescription
textMayBeRef<string>text show

Inherit Shape

NameTypeDescription
xMayBeRef<number>offset x
yMayBeRef<number>offset y

Optional Options

NameTypeDefaultDescription
fontFamilyMayBeRef<string>Font default BrowserFont apply to text
fontSizeMayBeRef<number>12Font size
fontStyleMayBeRef<"normal" | "bold" | "italic" | "italic bold">"normal"display font style
fontVariantMayBeRef<"normal" | "small-caps">"normal"can be normal or small-caps
textDecorationMayBeRef<"line-through" | "underline" | "none">"none"underline style
alignMayBeRef<"left" | "center" | "right" | "justify">"left"ext will display the same style as text-align in CSS
verticalAlignMayBeRef<"top" | "middle" | "bottom">"bottom"This is like align but for vertical. it's the same as vertical-align in CSS
paddingMayBeRef<number>0padding text
lineHeightMayBeRef<number>1It like line-height in CSS
wrapMayBeRef<"word" | "char" | "none">
ellipsisMayBeRef<boolean>falseif Text config is set to wrap="none" and ellipsis=true, then it will add "..." to the end
letterSpacingMayBeRef<number>0letter spacing for text
textBaselineMayBeRef<CanvasTextBaseline>"middle"This is similar to verticalAlign but instead of calculating it itself it uses the canvas api (may improve performance)
widthMayBeRef<number | "auto">"auto"
heightMayBeRef<number | "auto">"auto"

Demo

ts
import { Stage, Layer, Text, Rect } from "fcanvas"

const stage = new Stage({ height: 300, width: 350 }).mount("#app")
const layer = new Layer().addTo(stage)

const simpleText = new Text({
  x: stage.size.width / 2,
  y: 15,
  text: "Simple Text",
  align: "center",
  fontSize: 30,
  fontFamily: "Calibri",
  fill: "green"
})
simpleText.$.x -= simpleText.clientRect.width / 2

const complexText = new Text({
  x: 20,
  y: 60,
  text: "COMPLEX TEXT\n\nAll the world's a stage, and all the men and women merely players. They have their exits and their entrances.",
  fontSize: 18,
  fontFamily: "Calibri",
  fill: "#555",
  width: 300,
  padding: 20,
  align: "center"
})
const rect = new Rect({
  x: 20,
  y: 60,
  stroke: "#555",
  strokeWidth: 5,
  fill: "#ddd",
  width: 300,
  height: complexText.getBoundingClientRect().height,
  shadow: {
    x: 10,
    y: 10,
    blur: 10,
    color: "rgba(0, 0, 0, 0.5)"
  },
  cornerRadius: 10
})
// add the shapes to the layer
layer.add(simpleText)
layer.add(rect)
layer.add(complexText)

Released under the MIT License.