diff --git a/lib/core/background.dart b/lib/core/background.dart index 0f2cef6..17f9397 100644 --- a/lib/core/background.dart +++ b/lib/core/background.dart @@ -9,24 +9,50 @@ abstract class CanvasBackground { /// draw the backround on this context. Implement this to have /// different kinds of backgrounds - void paint(Canvas canvas, Size canvasSize, double scale); + void paint(Canvas canvas, Offset offset, double scale, Size canvasSize); } class NoBackground extends CanvasBackground { const NoBackground(); @override - void paint(Canvas canvas, Size canvasSize, double scale) {} + void paint(Canvas canvas, Offset offset, double scale, Size canvasSize) {} } class SingleColorBackround extends CanvasBackground { const SingleColorBackround(Color backgroundColor) : super(bgColor: backgroundColor); @override - void paint(Canvas canvas, Size canvasSize, double scale) { + void paint(Canvas canvas, Offset offset, double scale, Size canvasSize) { final paint = Paint() ..color = bgColor ..style = PaintingStyle.fill; canvas.drawRect(Rect.fromLTWH(0, 0, canvasSize.width, canvasSize.height), paint); } } + +class DotGridBackround extends CanvasBackground { + final double size; + final double spacing; + final Color color; + + const DotGridBackround({this.size = 2.0, this.spacing = 50.0, this.color = Colors.black12}) : super(); + + @override + void paint(Canvas canvas, Offset offset, double scale, Size canvasSize) { + final paint = Paint() + ..color = color + ..strokeWidth = 1.0 * scale; + double scaledSpacing = spacing * scale; + double scaledSize = size * scale; + + double startX = (offset.dx % scaledSpacing) - scaledSpacing; + double startY = (offset.dy % scaledSpacing) - scaledSpacing; + + for (double x = startX; x < canvasSize.width + scaledSpacing; x += scaledSpacing) { + for (double y = startY; y < canvasSize.height + scaledSpacing; y += scaledSpacing) { + canvas.drawCircle(Offset(x, y), scaledSize, paint); + } + } + } +} diff --git a/lib/core/render.dart b/lib/core/render.dart index 2a3ea28..e0abadd 100644 --- a/lib/core/render.dart +++ b/lib/core/render.dart @@ -216,7 +216,7 @@ class _CanvasRenderBox extends RenderBox void paint(PaintingContext context, Offset canvasStartOffset) { assert(childCount == _ssPositions.length); // use the canvas background painter, pass it the canvas and that should handle drawing the background - _canvasBackground.paint(context.canvas, size, _scale); + _canvasBackground.paint(context.canvas, canvasStartOffset, _scale, size); // though using ssPositionns here directly worked for me but docs using the parentData // to get this info is the convention as child can be reordered ( though this will always