CanvasTerminal.withCanvas constructor

CanvasTerminal.withCanvas(
  1. CanvasElement canvas,
  2. CanvasRenderer renderer,
  3. int pixelWidth,
  4. int pixelHeight, [
  5. int? scale,
  6. Vec2? minSize,
])

Builds a CanvasTerminal using the given html.CanvasElement and a pre-configured CanvasRenderer. Will update the canvas to fit into the supplied pixel dimensions (or the given minSize columns and rows) using the given or current scale.

Implementation

factory CanvasTerminal.withCanvas(html.CanvasElement canvas,
    CanvasRenderer renderer, int pixelWidth, int pixelHeight,
    [int? scale, Vec2? minSize]) {
  scale ??= html.window.devicePixelRatio.toInt();
  var cols = pixelWidth ~/ renderer.charWidth;
  var rows = pixelHeight ~/ renderer.charHeight;

  if (minSize != null) {
    cols = math.max<int>(cols, minSize.x);
    rows = math.max<int>(rows, minSize.y);
  }

  var cWidth = cols * renderer.charWidth;
  var cHeight = rows * renderer.charHeight;

  canvas.width = cWidth * scale;
  canvas.height = cHeight * scale;
  canvas.style.width = '${cWidth}px';
  canvas.style.height = '${cHeight}px';

  // do our best to get pixel-perfect rendering
  canvas.style.imageRendering = 'pixelated';
  canvas.style.fontSmoothing = 'none';

  return CanvasTerminal._(canvas, renderer, cols, rows);
}