• 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// Checker board the range [leftPx, rightPx].
19export function checkerboard(
20    ctx: CanvasRenderingContext2D,
21    heightPx: number,
22    leftPx: number,
23    rightPx: number): void {
24  const widthPx = rightPx - leftPx;
25  ctx.font = '12px Roboto Condensed';
26  ctx.fillStyle = '#eee';
27  ctx.fillRect(leftPx, 0, widthPx, heightPx);
28  ctx.fillStyle = '#666';
29  const oldBaseline = ctx.textBaseline;
30  ctx.textBaseline = 'middle';
31  if (LOADING_TEXT_WIDTH === 0) {
32    LOADING_TEXT_WIDTH = ctx.measureText(LOADING_TEXT).width;
33  }
34  if (LOADING_TEXT_WIDTH <= widthPx) {
35    ctx.fillText(
36        LOADING_TEXT,
37        leftPx + widthPx / 2 - LOADING_TEXT_WIDTH / 2,
38        heightPx / 2);
39  }
40  ctx.textBaseline = oldBaseline;
41}
42
43// Checker board everything between [startPx, endPx] except [leftPx, rightPx].
44export function checkerboardExcept(
45    ctx: CanvasRenderingContext2D,
46    heightPx: number,
47    startPx: number,
48    endPx: number,
49    leftPx: number,
50    rightPx: number): void {
51  // [leftPx, rightPx] doesn't overlap [startPx, endPx] at all:
52  if (rightPx <= startPx || leftPx >= endPx) {
53    checkerboard(ctx, heightPx, startPx, endPx);
54    return;
55  }
56
57  // Checkerboard [startPx, leftPx]:
58  if (leftPx > startPx) {
59    checkerboard(ctx, heightPx, startPx, leftPx);
60  }
61
62  // Checkerboard [rightPx, endPx]:
63  if (rightPx < endPx) {
64    checkerboard(ctx, heightPx, rightPx, endPx);
65  }
66}
67