# Rendering

# Canvas

# createCanvas

createCanvas ft.createCanvas()

Creates a canvas object.

Return Value

Canvas

# Canvas

# Canvas.getContext

RenderingContext Canvas.getContext(string contextType)

Gets the drawing context of the canvas object.

Parameters

string contextType

Context type

Valid values for contextType

Value Description Minimum Version
2d 2D drawing context

Not Supported

Value Description Minimum Version
webgl WebGL drawing context

Return Value

RenderingContext

# Canvas.toDataURL()

Returns a data URI containing the image display.

In WebGL, it may return black screen data. This can be resolved by passing the parameter preserveDrawingBuffer: true when obtaining the GL context.

# RenderingContext

Drawing context of the canvas object.

Properties

  • string|CanvasGradient fillStyle
    Fill color. Usage is the same as RenderingContext.setFillStyle().

  • string|CanvasGradient strokeStyle
    Border color. Usage is the same as RenderingContext.setStrokeStyle().

  • number shadowOffsetX
    Horizontal offset of the shadow relative to the shape.

  • number shadowOffsetY
    Vertical offset of the shadow relative to the shape.

  • number shadowColor
    Color of the shadow.

  • number shadowBlur
    Blur level of the shadow.

  • number lineWidth
    Width of the line. Usage is the same as RenderingContext.setLineWidth().

  • string lineCap
    Endpoint style of the line. Usage is the same as RenderingContext.setLineCap().

  • string lineJoin
    Intersection style of the line. Usage is the same as RenderingContext.setLineJoin().

Valid values for lineJoin

Value Description Minimum Version
bevel Bevel
round Round
miter Miter
  • number miterLimit
    Maximum miter length. Usage is the same as RenderingContext.setMiterLimit().

  • number lineDashOffset
    Dash pattern offset, initial value is 0.

  • string font
    Properties of the current font style. A DOMString string conforming to CSS font syntax, requiring at least font size and font family. Default value is 10px sans-serif.

  • number globalAlpha
    Global brush transparency. Range 0-1, where 0 indicates fully transparent and 1 indicates fully opaque.

  • string globalCompositeOperation
    Type of compositing operation applied when drawing new shapes. Currently, the Android version only supports compositing for fill blocks; compositing effects for stroke lines are all source-over.

Currently supported operations include:

Methods

  • CanvasGradient RenderingContext.createLinearGradient(number x0, number y0, number x1, number y1)
    Creates a linear gradient color. The returned CanvasGradient object requires using CanvasGradient.addColorStop() to specify gradient stops, with at least two required.

  • CanvasGradient RenderingContext.createCircularGradient(number x, number y, number r)
    Creates a circular gradient color. The starting point is at the center, and the ending point is at the circumference. The returned CanvasGradient object requires using CanvasGradient.addColorStop() to specify gradient stops, with at least two required.

  • RenderingContext.createPattern(string image, string repetition)
    Method to create a pattern for the specified image, repeating the source image in the specified direction.

  • Object RenderingContext.measureText(string text)
    Measures text dimension information. Currently only returns text width. Synchronous interface.

  • RenderingContext.save()
    Saves the drawing context.

  • RenderingContext.restore()
    Restores the previously saved drawing context.

  • RenderingContext.beginPath()
    Starts creating a path. Requires calling fill or stroke to use the path for filling or stroking.

  • RenderingContext.moveTo(number x, number y)
    Moves the path to the specified point on the canvas without creating a line. Use the stroke method to draw lines.

  • RenderingContext.lineTo(number x, number y)
    Adds a new point and creates a line from the last specified point to the target point. Use the stroke method to draw lines.

  • RenderingContext.quadraticCurveTo(number cpx, number cpy, number x, number y)
    Creates a quadratic Bézier curve path. The starting point of the curve is the previous point in the path.

  • RenderingContext.bezierCurveTo(number cp1x, number cp1y, number cp2x, number cp2y, number x, number y)
    Creates a cubic Bézier curve path. The starting point of the curve is the previous point in the path.

  • RenderingContext.arc(number x, number y, number rnumber sAngle, number eAngle, boolean counterclockwise)
    Creates an arc.

  • RenderingContext.rect(number x, number y, number width, number height)
    Creates a rectangular path. Requires using the fill or stroke method to actually draw the rectangle onto the canvas.

  • RenderingContext.arcTo(number x1, number y1, number x2, number y2, number radius)
    Draws an arc path based on control points and radius.

  • RenderingContext.clip()
    Clips any shape and size from the original canvas. Once an area is clipped, all subsequent drawings will be confined to the clipped area (cannot access other areas on the canvas). You can save the current canvas area using the save method before using the clip method and restore it at any time later using the restore method.

  • RenderingContext.fillRect(number x, number y, number width, number height)
    Fills a rectangle. Use setFillStyle to set the fill color of the rectangle; defaults to black if not set.

  • RenderingContext.strokeRect(number x, number y, number width,number height)
    Draws a rectangle (non-filled). Use setStrokeStyle to set the color of the rectangle lines; defaults to black if not set.

  • RenderingContext.clearRect(number x, number y, number width, number height)
    Clears the content within the rectangular area on the canvas.

  • RenderingContext.fill()
    Fills the content in the current path. Default fill color is black.

  • RenderingContext.stroke()
    Draws the border of the current path. Default color is black.

  • RenderingContext.closePath()
    Closes a path. Connects the start and end points. If a new path is started after closing without calling fill or stroke, the previous path will not be rendered.

  • RenderingContext.scale(number scaleWidth, number scaleHeight)
    After calling, the coordinates of subsequently created paths will be scaled. Multiple calls multiply the scaling factors.

  • RenderingContext.rotate(number rotate)
    Rotates the current coordinate system clockwise around the origin. Multiple calls accumulate the rotation angles. The origin can be modified using the translate method.

  • RenderingContext.translate(number x, number y)
    Transforms the origin (0, 0) of the current coordinate system. The default coordinate system origin is the top-left corner of the page.

  • RenderingContext.drawImage(string imageResource, number sx, number sy, number sWidth, number sHeight, number dx, number dy, number dWidth, number dHeight)
    Draws an image onto the canvas.

  • RenderingContext.strokeText(string text, number x, number y, number maxWidth)
    Method to draw stroked text at the given (x, y) position.

  • RenderingContext.transform(number scaleX, number skewX, number skewY, number scaleY, number translateX, number translateY)
    Method to multiply the current transformation matrix multiple times.

  • RenderingContext.setTransform(number scaleX, number skewX, number skewY, number scaleY, number translateX, number translateY)
    Method to reset (override) the current transformation using a matrix.

  • RenderingContext.setFillStyle(string|CanvasGradient color)
    Sets the fill color.

  • RenderingContext.setStrokeStyle(string|CanvasGradient color)
    Sets the stroke color.

  • RenderingContext.setShadow(number offsetX, number offsetY, number blur, string color)
    Sets the shadow style.

  • RenderingContext.setGlobalAlpha(number alpha)
    Sets the global brush transparency.

  • RenderingContext.setLineWidth(number lineWidth)
    Sets the line width.

-numbers RenderingContext.setLineJoin(string lineJoin)**
Sets the line join style.

  • RenderingContext.setLineCap(string lineCap)
    Sets the line cap style.

  • RenderingContext.setLineDash(Array.<number> pattern, number offset)
    Sets the dash pattern style.

  • RenderingContext.setMiterLimit(number miterLimit)
    Sets the maximum miter length. Miter length refers to the distance between the inner and outer corners at the intersection of two lines. Only effective when RenderingContext.setLineJoin() is set to miter. If exceeded, the connection will be displayed with lineJoin as bevel.

  • RenderingContext.fillText(string text, number x, number y, number maxWidth)
    Draws filled text on the canvas.

  • RenderingContext.setFontSize(number fontSize)
    Sets the font size.

  • RenderingContext.setTextAlign(string align)
    Sets the text alignment.

  • RenderingContext.setTextBaseline(string textBaseline)
    Sets the vertical alignment of the text.

# createImage

Creates an image object.

Return Value

Image

Image object

# Image

Image object

Properties

string src

URL of the image

number width

Actual width of the image

number height

Actual height of the image

function onload

Callback function triggered after the image is loaded

function onerror

Callback function triggered when an error occurs during image loading

# Fonts

# loadFont

loadFont(string path)

Loads a custom font file.

Parameters

string path

Font file path. Supports local paths and code package paths.

Return Value

res string

If the font is loaded successfully, returns the font family value; otherwise, returns undefined.

const res = ft.loadFont('path/to/font.woff')

# getTextLineHeight

getTextLineHeight(Object object)

Gets the line height of a line of text.

Parameters

Object object

Property Type Default Value Required Description
fontStyle string normal No Font style
fontWeight string normal No Font weight
fontSize number 16 No Font size
fontFamily string No Font family name
text string Yes Text content

Valid values for object.fontStyle

Value Description
normal Normal
italic Italic

Valid values for object.fontWeight

Value Description
normal Normal
bold Bold

Return Value

res number

Example Code

const height = ft.getTextLineHeight({
  text: 'some text',
  fontStyle: 'normal',
  fontSize: 22
})
© FinClip with ❤ , Since 2017