- override
Paints this node to the canvas.
Subclasses, such as Sprite
, override this method to do the actual painting of the node. To do custom
drawing override this method and make calls to the canvas
object. All drawing is done in the node's local
coordinate system, relative to the node's position. If you want to make the drawing relative to the node's
bounding box's origin, override NodeWithSize
and call the applyTransformForPivot method before making calls for
drawing.
void paint(Canvas canvas) {
canvas.save();
applyTransformForPivot(canvas);
// Do painting here
canvas.restore();
}
Source
@override void paint(Canvas canvas) { // Account for pivot point applyTransformForPivot(canvas); if (texture != null) { double w = texture.size.width; double h = texture.size.height; if (w <= 0 || h <= 0) return; double scaleX = size.width / w; double scaleY = size.height / h; if (constrainProportions) { // Constrain proportions, using the smallest scale and by centering the image if (scaleX < scaleY) { canvas.translate(0.0, (size.height - scaleX * h) / 2.0); scaleY = scaleX; } else { canvas.translate((size.width - scaleY * w) / 2.0, 0.0); scaleX = scaleY; } } canvas.scale(scaleX, scaleY); // Setup paint object for opacity and transfer mode _updatePaint(_cachedPaint); // Do actual drawing of the sprite texture.drawTexture(canvas, Point.origin, _cachedPaint); } else { // Paint a red square for missing texture canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), new Paint()..color = new Color.fromARGB(255, 255, 0, 0)); } }