1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128

ash / webui / diagnostics_ui / resources / drawing_provider.ts [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {assert} from 'chrome://resources/js/assert.js';

import {constructRgba, DESTINATION_OVER, getTrailOpacityFromPressure, LINE_CAP, LINE_WIDTH, lookupCssVariableValue, MARK_COLOR, MARK_OPACITY, MARK_RADIUS, SOURCE_OVER, TRAIL_COLOR} from './drawing_provider_utils.js';


/**
 * DrawingProvider interface provides drawing methods for touchscreen and
 * touchpad tester to draw on screen.
 */
interface DrawingProvider {
  /**
   * For touchscreen tester to draw a trail on screen. A trail is connected by
   * multiple lines. This function essentially draws a line by connecting two
   * points.
   * @param x0 The x coordinate of the first point.
   * @param y0 The y coordinate of the first point.
   * @param x1 The x coordinate of the second point.
   * @param y1 The y coordinate of the second point.
   * @param pressure The pressure of the touch to indicate the opacity of the
   *     trail.
   */
  drawTrail(x0: number, y0: number, x1: number, y1: number, pressure: number):
      void;

  /**
   * For touchscreen tester to draw a trail starting or ending mark on screen.
   * A mark is essentially a circle on screen.
   * @param x The x coordinate of the circle center.
   * @param y The y coordinate of the circle center.
   */
  drawTrailMark(x: number, y: number): void;
}

/**
 * CanvasDrawingProvider implements the DrawingProvider interface using html
 * Canvas API. This design makes the drawing mechanism in a replaceable module.
 */
export class CanvasDrawingProvider implements DrawingProvider {
  private ctx: CanvasRenderingContext2D;

  constructor(ctx: CanvasRenderingContext2D) {
    this.ctx = ctx;
    this.setup();
  }

  setup(): void {
    assert(this.ctx);
    this.ctx.lineCap = LINE_CAP;
    this.ctx.lineWidth = LINE_WIDTH;
  }

  /**
   * For testing only.
   */
  getCtx(): CanvasRenderingContext2D {
    return this.ctx;
  }

  /**
   * For testing only.
   */
  getLineCap(): string {
    return this.ctx.lineCap;
  }

  /**
   * For testing only.
   */
  getLineWidth(): number {
    return this.ctx.lineWidth;
  }

  /**
   * For testing only.
   */
  getStrokeStyle(): string|CanvasGradient|CanvasPattern {
    return this.ctx.strokeStyle;
  }

  /**
   * For testing only.
   */
  getFillStyle(): string|CanvasGradient|CanvasPattern {
    return this.ctx.fillStyle;
  }

  /**
   * For testing only.
   */
  getGlobalCompositeOperation(): string {
    return this.ctx.globalCompositeOperation;
  }

  /**
   * Draw a line on canvas.
   */
  drawTrail(x0: number, y0: number, x1: number, y1: number, pressure: number):
      void {
    assert(this.ctx);
    this.ctx.strokeStyle = constructRgba(
        lookupCssVariableValue(TRAIL_COLOR),
        getTrailOpacityFromPressure(pressure));
    this.ctx.beginPath();
    this.ctx.moveTo(x0, y0);
    this.ctx.lineTo(x1, y1);
    this.ctx.stroke();
  }

  /**
   * Draw a trail mark on canvas.
   */
  drawTrailMark(x: number, y: number): void {
    assert(this.ctx);
    // Making sure the mark is always on top.
    this.ctx.globalCompositeOperation = SOURCE_OVER;
    this.ctx.fillStyle = constructRgba(
        lookupCssVariableValue(MARK_COLOR),
        lookupCssVariableValue(MARK_OPACITY));
    this.ctx.beginPath();
    this.ctx.arc(x, y, MARK_RADIUS, 0, 2 * Math.PI);
    this.ctx.fill();
    this.ctx.globalCompositeOperation = DESTINATION_OVER;
  }
}