Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 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 | |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 15 | const LOADING_TEXT = 'Loading...'; |
| 16 | let LOADING_TEXT_WIDTH = 0; |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Checker board the range [leftPx, rightPx]. |
| 20 | */ |
Hector Dearman | d164fd3 | 2019-09-17 10:28:06 +0100 | [diff] [blame] | 21 | export function checkerboard( |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 22 | ctx: CanvasRenderingContext2D, |
| 23 | heightPx: number, |
| 24 | leftPx: number, |
| 25 | rightPx: number): void { |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 26 | const widthPx = rightPx - leftPx; |
Primiano Tucci | 32ea103 | 2020-01-07 13:53:23 +0000 | [diff] [blame] | 27 | ctx.font = '12px Roboto Condensed'; |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 28 | ctx.fillStyle = '#eee'; |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 29 | ctx.fillRect(leftPx, 0, widthPx, heightPx); |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 30 | ctx.fillStyle = '#666'; |
Hector Dearman | 52b8d77 | 2019-08-23 11:18:00 -0700 | [diff] [blame] | 31 | const oldBaseline = ctx.textBaseline; |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 32 | ctx.textBaseline = 'middle'; |
| 33 | if (LOADING_TEXT_WIDTH === 0) { |
| 34 | LOADING_TEXT_WIDTH = ctx.measureText(LOADING_TEXT).width; |
| 35 | } |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 36 | ctx.fillText( |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 37 | LOADING_TEXT, |
| 38 | leftPx + widthPx / 2 - LOADING_TEXT_WIDTH, |
| 39 | heightPx / 2, |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 40 | widthPx); |
Hector Dearman | 52b8d77 | 2019-08-23 11:18:00 -0700 | [diff] [blame] | 41 | ctx.textBaseline = oldBaseline; |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Checker board everything between [startPx, endPx] except [leftPx, rightPx]. |
| 46 | */ |
| 47 | export function checkerboardExcept( |
| 48 | ctx: CanvasRenderingContext2D, |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 49 | heightPx: number, |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 50 | 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 Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 56 | checkerboard(ctx, heightPx, startPx, endPx); |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | // Checkerboard [startPx, leftPx]: |
| 61 | if (leftPx > startPx) { |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 62 | checkerboard(ctx, heightPx, startPx, leftPx); |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Checkerboard [rightPx, endPx]: |
| 66 | if (rightPx < endPx) { |
Hector Dearman | 49057cf | 2019-08-22 16:21:42 -0700 | [diff] [blame] | 67 | checkerboard(ctx, heightPx, rightPx, endPx); |
Hector Dearman | 9e06154 | 2018-09-27 11:00:25 +0100 | [diff] [blame] | 68 | } |
| 69 | } |