blob: d2717596a5dbf38d9b7d5bff286ac0b5676da25d [file] [log] [blame]
Hector Dearman9e061542018-09-27 11:00:25 +01001// 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
Hector Dearman49057cf2019-08-22 16:21:42 -070015const LOADING_TEXT = 'Loading...';
16let LOADING_TEXT_WIDTH = 0;
Hector Dearman9e061542018-09-27 11:00:25 +010017
18/**
19 * Checker board the range [leftPx, rightPx].
20 */
Hector Dearmand164fd32019-09-17 10:28:06 +010021export function checkerboard(
Hector Dearman49057cf2019-08-22 16:21:42 -070022 ctx: CanvasRenderingContext2D,
23 heightPx: number,
24 leftPx: number,
25 rightPx: number): void {
Hector Dearman9e061542018-09-27 11:00:25 +010026 const widthPx = rightPx - leftPx;
Primiano Tucci32ea1032020-01-07 13:53:23 +000027 ctx.font = '12px Roboto Condensed';
Hector Dearman9e061542018-09-27 11:00:25 +010028 ctx.fillStyle = '#eee';
Hector Dearman49057cf2019-08-22 16:21:42 -070029 ctx.fillRect(leftPx, 0, widthPx, heightPx);
Hector Dearman9e061542018-09-27 11:00:25 +010030 ctx.fillStyle = '#666';
Hector Dearman52b8d772019-08-23 11:18:00 -070031 const oldBaseline = ctx.textBaseline;
Hector Dearman49057cf2019-08-22 16:21:42 -070032 ctx.textBaseline = 'middle';
33 if (LOADING_TEXT_WIDTH === 0) {
34 LOADING_TEXT_WIDTH = ctx.measureText(LOADING_TEXT).width;
35 }
Hector Dearman9e061542018-09-27 11:00:25 +010036 ctx.fillText(
Hector Dearman49057cf2019-08-22 16:21:42 -070037 LOADING_TEXT,
38 leftPx + widthPx / 2 - LOADING_TEXT_WIDTH,
39 heightPx / 2,
Hector Dearman9e061542018-09-27 11:00:25 +010040 widthPx);
Hector Dearman52b8d772019-08-23 11:18:00 -070041 ctx.textBaseline = oldBaseline;
Hector Dearman9e061542018-09-27 11:00:25 +010042}
43
44/**
45 * Checker board everything between [startPx, endPx] except [leftPx, rightPx].
46 */
47export function checkerboardExcept(
48 ctx: CanvasRenderingContext2D,
Hector Dearman49057cf2019-08-22 16:21:42 -070049 heightPx: number,
Hector Dearman9e061542018-09-27 11:00:25 +010050 startPx: number,
51 endPx: number,
52 leftPx: number,
53 rightPx: number): void {
54 // [leftPx, rightPx] doesn't overlap [startPx, endPx] at all:
55 if (rightPx <= startPx || leftPx >= endPx) {
Hector Dearman49057cf2019-08-22 16:21:42 -070056 checkerboard(ctx, heightPx, startPx, endPx);
Hector Dearman9e061542018-09-27 11:00:25 +010057 return;
58 }
59
60 // Checkerboard [startPx, leftPx]:
61 if (leftPx > startPx) {
Hector Dearman49057cf2019-08-22 16:21:42 -070062 checkerboard(ctx, heightPx, startPx, leftPx);
Hector Dearman9e061542018-09-27 11:00:25 +010063 }
64
65 // Checkerboard [rightPx, endPx]:
66 if (rightPx < endPx) {
Hector Dearman49057cf2019-08-22 16:21:42 -070067 checkerboard(ctx, heightPx, rightPx, endPx);
Hector Dearman9e061542018-09-27 11:00:25 +010068 }
69}