Canvas

public final class Canvas

Canvas provides an interface for drawing, and how the drawing is clipped and transformed. Canvas contains a stack of Matrix and clip values.

Canvas and Paint together provide the state to draw into Surface or BaseDevice. Each Canvas draw call transforms the geometry of the object by the concatenation of all Matrix values in the stack. The transformed geometry is clipped by the intersection of all of clip values in the stack. The Canvas draw calls use Paint to supply drawing state such as color, Typeface, text size, stroke width, Shader and so on.

To draw to a pixel-based destination, create raster surface or GPU surface. Request Canvas from Surface to obtain the interface to draw. Canvas generated by raster surface draws to memory visible to the CPU. Canvas generated by GPU surface uses Vulkan or OpenGL to draw to the GPU.

To draw to a document, obtain Canvas from SVG canvas, document PDF, or PictureRecorder. Document based Canvas and other Canvas subclasses reference BaseDevice describing the destination.

Typically you create the Canvas from a bitmap, but an empty canvas can be created with the some fo the static make methods, and serve special purposes:

  • makeEmpty(width:height) - creates an empty canvas that ignores all drawing commands
  • makeOverdraw(canvas:) - A canvas that captures all drawing commands, and rather than draw the actual content, it increments the alpha channel of each pixel every time it would have been touched by a draw call.
  • makeNway(width:height:) - will create a passthrough n-way canvas. Once created you can add canvas with addCanvas, and remove them with removeCanvas