blob: d9aeec511b0c34d8b8ae2b48095dcd556135388a [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
15// TODO(hjd): Dedupe these.
16const SLICE_HEIGHT = 30;
17const TRACK_PADDING = 5;
18
19/**
20 * Checker board the range [leftPx, rightPx].
21 */
22export function checkerboard(
23 ctx: CanvasRenderingContext2D, leftPx: number, rightPx: number): void {
24 const widthPx = rightPx - leftPx;
25 ctx.font = '12px Google Sans';
26 ctx.fillStyle = '#eee';
27 ctx.fillRect(leftPx, TRACK_PADDING, widthPx, SLICE_HEIGHT);
28 ctx.fillStyle = '#666';
29 ctx.fillText(
30 'loading...',
31 leftPx + widthPx / 2,
32 TRACK_PADDING + SLICE_HEIGHT / 2,
33 widthPx);
34}
35
36/**
37 * Checker board everything between [startPx, endPx] except [leftPx, rightPx].
38 */
39export function checkerboardExcept(
40 ctx: CanvasRenderingContext2D,
41 startPx: number,
42 endPx: number,
43 leftPx: number,
44 rightPx: number): void {
45 // [leftPx, rightPx] doesn't overlap [startPx, endPx] at all:
46 if (rightPx <= startPx || leftPx >= endPx) {
47 checkerboard(ctx, startPx, endPx);
48 return;
49 }
50
51 // Checkerboard [startPx, leftPx]:
52 if (leftPx > startPx) {
53 checkerboard(ctx, startPx, leftPx);
54 }
55
56 // Checkerboard [rightPx, endPx]:
57 if (rightPx < endPx) {
58 checkerboard(ctx, rightPx, endPx);
59 }
60}