Repository navigation
Preview: rulers along the edges, a scale bar, and a 3D height ruler #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Rulers and a scale bar for the previews: steps that read well at any | ||
| // zoom, and where their ticks land. | ||
|
|
||
| /** The smallest of 1, 2 and 5 times a power of ten (whole blocks, at least 1) that is at least `min`. */ | ||
| export function niceStep(min: number): number { | ||
| if (!(min > 1)) return 1; | ||
| const p = 10 ** Math.floor(Math.log10(min)); | ||
| for (const m of [1, 2, 5, 10]) if (m * p >= min) return m * p; | ||
| return 10 * p; | ||
| } | ||
|
|
||
| /** | ||
| * The multiples of step between lo and hi (either way round). Counted, not | ||
| * stepped, and capped, so a huge coordinate can't loop forever. | ||
| */ | ||
| export function multiples(lo: number, hi: number, step: number): number[] { | ||
| const a = Math.min(lo, hi); | ||
| const b = Math.max(lo, hi); | ||
| const first = Math.ceil(a / step); | ||
| const n = Math.max(0, Math.min(200, Math.floor(b / step) - first + 1)); | ||
| // Far out, adding one no longer changes a float: keep only values that move on. | ||
| const out: number[] = []; | ||
| for (let i = 0; i < n; i++) { | ||
| const v = (first + i) * step; | ||
| if (!out.length || v > out[out.length - 1]) out.push(v); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** A tick on a ruler: its world coordinate and its offset along the edge in CSS pixels. */ | ||
| export interface Tick { | ||
| value: number; | ||
| at: number; | ||
| } | ||
|
|
||
| /** | ||
| * The ticks along an edge `length` CSS pixels long, where the coordinate at | ||
| * offset p is origin + per·p (per is negative when it counts down the edge). | ||
| * Ticks are at least `gap` pixels apart. | ||
| */ | ||
| export function ticks(origin: number, per: number, length: number, gap: number): Tick[] { | ||
| if (!per || !Number.isFinite(per) || !Number.isFinite(origin) || length <= 0) return []; | ||
| const step = niceStep(Math.abs(per) * gap); | ||
| return multiples(origin, origin + per * length, step).map((value) => ({ value, at: (value - origin) / per })); | ||
| } | ||
|
|
||
| /** | ||
| * A scale bar for a view at `blocksPerPixel`: a power of two blocks (so | ||
| * it lands on chunks and regions) whose bar is about 60 to 120 pixels long. | ||
| */ | ||
| export function scaleBar(blocksPerPixel: number, target = 80): { blocks: number; px: number; label: string } | null { | ||
| if (!(blocksPerPixel > 0) || !Number.isFinite(blocksPerPixel)) return null; | ||
| const blocks = 2 ** Math.max(0, Math.round(Math.log2(target * blocksPerPixel))); | ||
| const px = blocks / blocksPerPixel; | ||
| let label = `${blocks} block${blocks === 1 ? '' : 's'}`; | ||
| if (blocks >= 512) label += ` · ${blocks / 512} region${blocks === 512 ? '' : 's'}`; | ||
| else if (blocks >= 16) label += ` · ${blocks / 16} chunk${blocks === 16 ? '' : 's'}`; | ||
| return { blocks, px, label }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.