• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15const LOADING_TEXT = 'Loading...';
16let LOADING_TEXT_WIDTH = 0;
17
18/**
19 * Checker board the range [leftPx, rightPx].
20 */
21export function checkerboard(
22    ctx: CanvasRenderingContext2D,
23    heightPx: number,
24    leftPx: number,
25    rightPx: number): void {
26  const widthPx = rightPx - leftPx;
27  ctx.font = '12px Roboto Condensed';
28  ctx.fillStyle = '#eee';
29  ctx.fillRect(leftPx, 0, widthPx, heightPx);
30  ctx.fillStyle = '#666';
31  const oldBaseline = ctx.textBaseline;
32  ctx.textBaseline = 'middle';
33  if (LOADING_TEXT_WIDTH === 0) {
34    LOADING_TEXT_WIDTH = ctx.measureText(LOADING_TEXT).width;
35  }
36  if (LOADING_TEXT_WIDTH <= widthPx) {
37    ctx.fillText(
38        LOADING_TEXT,
39        leftPx + widthPx / 2 - LOADING_TEXT_WIDTH / 2,
40        heightPx / 2);
41  }
42  ctx.textBaseline = oldBaseline;
43}
44
45/**
46 * Checker board everything between [startPx, endPx] except [leftPx, rightPx].
47 */
48export function checkerboardExcept(
49    ctx: CanvasRenderingContext2D,
50    heightPx: number,
51    startPx: number,
52    endPx: number,
53    leftPx: number,
54    rightPx: number): void {
55  // [leftPx, rightPx] doesn't overlap [startPx, endPx] at all:
56  if (rightPx <= startPx || leftPx >= endPx) {
57    checkerboard(ctx, heightPx, startPx, endPx);
58    return;
59  }
60
61  // Checkerboard [startPx, leftPx]:
62  if (leftPx > startPx) {
63    checkerboard(ctx, heightPx, startPx, leftPx);
64  }
65
66  // Checkerboard [rightPx, endPx]:
67  if (rightPx < endPx) {
68    checkerboard(ctx, heightPx, rightPx, endPx);
69  }
70}
71