Skip to main content
Version: v3

CANVAS

Start from API_LEVEL 3.0. Please refer to API_LEVEL

canvas_demo.jpg

Canvas

Current Canvas capabilities include

  1. Basic drawing, line, point, rectangle, rectangle fill, ellipse, sector, polygon
  2. Image drawing
  3. Text drawing
  4. Paint
  5. The canvas is stacked vertically, up to three layers can be stacked
  6. Clean up the canvas
  7. Support addEventListener method to listen for user interaction events

Create UI widget

import { createWidget, widget } from '@zos/ui'

const canvas = createWidget(widget.CANVAS, Param)

Param: object

PropertiesDescriptionRequiredType
xCanvas x coordinatesYESnumber
yCanvas y coordinatesYESnumber
wCanvas canvas widthYESnumber
hCanvas canvas heightYESnumber
const canvas = createWidget(widget.CANVAS, {
x: 0,
y: 0,
w: 100,
h: 100
})

canvas.setPaint

Set Paint

setPaint(Param: object): void

Param: object

PropertiesDescriptionRequiredType
colorColorYESnumber
line_widthLine WidthYESnumber
canvas.setPaint({
color: 0xff0000,
line_width: 10
})

canvas.drawPixel

Drawing individual pixel point

drawPixel(Param: object): void

Param: object

PropertiesDescriptionRequiredType
xx coordinatesYESnumber
yy coordinatesYESnumber
colorColorNOnumber
canvas.drawPixel({
x: 0,
y: 0,
color: 0xffffff
})

canvas.drawLine

Draws a line segment, with the line width and color set by setPaint paint

drawLine(Param: object): void

Param: object

PropertiesDescriptionRequiredType
x1Start point x coordinatesYESnumber
y1Start point y coordinatesYESnumber
x2End point x coordinatesYESnumber
y2End point y coordinatesYESnumber
colorColorNOnumber
canvas.drawLine({
x1: 100,
y1: 100,
x2: 200,
y2: 100,
color: 0xffffff
})

Rectangle

  1. Draw a rectangle path with line width and color set using the setPaint brush
strokeRect(Param: object): void
  1. Draw rectangle fill
drawRect(Param: object): void

Param: object

PropertiesDescriptionRequiredType
x1Start point x coordinatesYESnumber
y1Start point y coordinatesYESnumber
x2End point x coordinatesYESnumber
y2End point y coordinatesYESnumber
colorColorNOnumber
canvas.drawRect({
x1: 60,
y1: 20,
x2: 120,
y2: 60,
color: 0xff00ff
})

Circle

  1. Draw a circle path with line width and color set using the setPaint brush
strokeCircle(Param: object): void
  1. Draw circle fill
drawCircle(Param: object): void

Param: object

PropertiesDescriptionRequiredType
center_xCenter x coordinatesYESnumber
center_yCenter y coordinatesYESnumber
radiusRadiusYESnumber
colorColorNOnumber
canvas.drawCircle({
center_x: 80,
center_y: 140,
radius: 40,
color: 0xfff400
})

Ellipse

  1. Draw a ellipse path with line width and color set using the setPaint brush
strokeEllipse(Param: object): void
  1. Draw ellipse fill
drawEllipse(Param: object): void

Param: object

PropertiesDescriptionRequiredType
center_xEllipse center x coordinatesYESnumber
center_yEllipse center y-coordinateYESnumber
radius_xx-directional axis radiusYESnumber
radius_yy-directional axis radiusYESnumber
colorColorNOnumber
canvas.drawEllipse({
center_x: 80,
center_y: 300,
radius_x: 60,
radius_y: 80,
color: 0xff0000
})

Arc

Compared to drawEllipse, start_angle and end_angle are added to intercept a part of the ellipse from the center of the ellipse as a sector.

  1. Draw path
strokeArc(Param: object): void
  1. Draw fill
drawArc(Param: object): void

Param: object

PropertiesDescriptionRequiredType
center_xEllipse center x coordinatesYESnumber
center_yEllipse center y-coordinateYESnumber
radius_xx-directional axis radiusYESnumber
radius_yy-directional axis radiusYESnumber
start_angleStart angle (0 degrees in the 3 o'clock direction)YESnumber
end_angleStart angle (0 degrees in the 3 o'clock direction)YESnumber
colorColorNOnumber
canvas.drawArc({
center_x: 280,
center_y: 200,
radius_x: 60,
radius_y: 80,
start_angle: -150,
end_angle: -30,
color: 0xfff400
})

Polygon

  1. Draw a polygon path with line width and color set using the setPaint brush
strokePoly(Param: object): void
  1. Draw polygon fill
drawPoly(Param: object): void

Param: object

PropertiesDescriptionRequiredType
data_arrayAn array of coordinates of at least '3' lengthYESCoordinate
colorColorNOnumber

Coordinate

PropertiesDescriptionType
xx coordinatesnumber
yy coordinatesnumber
const coordinateArray = [
{ x: 233, y: 30 },
{ x: 130, y: 230 },
{ x: 400, y: 200 },
{ x: 233, y: 30 }
]

canvas.strokePoly({
data_array: coordinateArray
color: 0x00ffff,
})

canvas.drawText

Drawing text

drawText(Param: object): void

Param: object

PropertiesDescriptionRequiredType
xText x CoordinatesYESnumber
yText y CoordinatesYESnumber
textText ContentYESstring
text_sizeText SizeYESnumber
colorColorNOnumber
canvas.drawText({
x: 200,
y: 260,
text_size: 30,
text: 'Hello Zepp OS'
})

canvas.drawImage

Drawing Image

drawImage(Param: object): void

Param: object

PropertiesDescriptionRequiredType
xImage x CoordinatesYESnumber
yImage y CoordinatesYESnumber
wImage widthYESnumber
hImage heightYESnumber
imageImage pathYESstring
alphaTransparency [0-255], 0 for full transparencyNOnumber
canvas.drawImage({
x: 0,
y: 0,
w: 466,
h: 466,
alpha: 255,
image: 'images/canvas/background.png'
})

canvas.clear

Clean up the canvas by rectangular area

clear(Param: object): void

Param: object

PropertiesDescriptionRequiredType
xRectangle x CoordinatesYESnumber
yRectangle y CoordinatesYESnumber
wRectangle area widthYESnumber
hRectangle area heightYESnumber
canvas.clear({
x: 400,
y: 0,
w: 64,
h: 64
})

Interaction events

Canvas supports widget.addEventListener to listen for interaction events, see widget.addEventListener(eventId, callback)

import { event } from '@zos/ui'

canvas.addEventListener(event.CLICK_UP, function cb(info) {
console.log(info.x)
console.log(info.y)
})