Skip to content

Commit 5a0a033

Browse files
authored
Merge pull request #9 from monooso/6-radius-bug
Fix radius bug
2 parents 6a3e4dc + 58cf9c4 commit 5a0a033

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

CONTRIBUTING.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# Contributing
2-
Thank you for thinking about contributing to this project. This document will help you to get started.
2+
3+
Thank you for thinking about contributing to this project. This document will
4+
help you to get started.
35

46
## Adding a feature
5-
If you'd like to add a feature to Avatar (or modify its existing behaviour), please start by [creating an issue][create_issue] so we can discuss it.
6-
Don't go straight to a pull request in case it's something I don't wish to accept; I don't want to waste your time.
7+
8+
If you'd like to add a feature to Avatar (or modify its existing behaviour),
9+
please start by [creating an issue][create_issue] so we can discuss it. Don't go
10+
straight to a pull request in case it's something I don't wish to accept; I
11+
don't want to waste your time.
712

813
## Fixing a bug
9-
If you've found a bug, please start by [creating an issue][create_issue]. If you'd like to [make a pull request][create_pr] for the fix once I've confirmed it's a bug in Avatar, that would be great.
14+
15+
If you've found a bug, please start by [creating an issue][create_issue]. If
16+
you'd like to [make a pull request][create_pr] for the fix once I've confirmed
17+
it's a bug in Avatar, that would be great.
1018

1119
[create_issue]: https://github.com/monooso/avatar/issues/new
1220
[create_pr]: https://github.com/monooso/avatar/compare
13-

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ avatars based on usernames or ids.
99
## Usage
1010

1111
```typescript
12-
import {
13-
generatePng,
14-
generateSvg,
15-
} from "https://deno.land/x/[email protected]/mod.ts";
12+
import { generatePng, generateSvg } from "https://deno.land/x/avatar/mod.ts";
1613

1714
// Generate an SVG avatar with the default options.
1815
let avatar = await generateSvg("jimbob");
@@ -31,6 +28,7 @@ avatar = await generatePng("cleetus", { radius: 20, size: 256 });
3128
```
3229

3330
## License
31+
3432
Avatar is open source software, released under [the MIT license](./LICENSE.txt).
3533

3634
## Credits

lib/options.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { z } from "https://deno.land/x/[email protected]/mod.ts";
22

3+
const defaults = { radius: 0, size: 64 };
4+
35
const validator = z.object({
4-
radius: z.number().int().min(0).default(0),
5-
size: z.number().int().min(1).default(64),
6+
radius: z.number().int().min(0).default(defaults.radius),
7+
size: z.number().int().min(1).default(defaults.size),
68
});
79

8-
type Options = z.input<typeof validator>;
10+
type InputOptions = z.input<typeof validator>;
11+
type OutputOptions = z.output<typeof validator>;
912

1013
/**
1114
* Validates and normalizes the given options. Raises if validation fails.
1215
*/
13-
function validate(opts: Options): Options {
16+
function validate(opts: InputOptions): OutputOptions {
1417
return validator.parse(opts);
1518
}
1619

17-
export { validate };
18-
export type { Options };
20+
export { defaults, validate };
21+
export type { InputOptions, OutputOptions };

lib/png.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { resvg } from "../deps.ts";
22
import * as svg from "./svg.ts";
3-
import type { Options } from "./options.ts";
3+
import type { InputOptions } from "./options.ts";
44

55
/**
66
* Generates a reproducible PNG image from a given seed.
77
* Renders the image as an SVG, and then converts it to a PNG using resvg.
88
*/
9-
async function generate(seed: string, opts?: Options): Promise<Uint8Array> {
9+
async function generate(
10+
seed: string,
11+
opts?: InputOptions,
12+
): Promise<Uint8Array> {
1013
const svgString = await svg.generate(seed, opts);
1114
return resvg.render(svgString);
1215
}

lib/svg.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import * as keys from "./keys.ts";
22
import miniavs from "./collections/miniavs/index.ts";
3-
import * as options from "./options.ts";
4-
import type { Options } from "./options.ts";
3+
import { defaults, validate } from "./options.ts";
4+
import type { InputOptions } from "./options.ts";
55

66
/**
77
* Generates a reproducible SVG image for the given seed.
88
*/
9-
async function generate(seed: string, opts?: Options): Promise<string> {
9+
async function generate(seed: string, opts?: InputOptions): Promise<string> {
1010
const key = await keys.generate(seed);
11-
opts = options.validate(opts || {});
11+
const { radius, size } = validate(opts || {});
12+
const scaledRadius = (defaults.size / size) * radius;
1213

1314
return `
14-
<svg width="${opts.size}" height="${opts.size}" viewBox="0 0 64 64" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg">
15+
<svg width="${size}" height="${size}" viewBox="0 0 64 64" fill="none" version="1.1" xmlns="http://www.w3.org/2000/svg">
1516
<clipPath id="avatarShape">
16-
<rect x="0" y="0" width="64" height="64" rx="${opts.radius}" ry="${opts.radius}" />
17+
<rect x="0" y="0" width="64" height="64" rx="${scaledRadius}" ry="${scaledRadius}" />
1718
</clipPath>
1819
1920
<g clip-path="url(#avatarShape)">

tests/lib/svg_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ Deno.test("svg", async (t) => {
5353
},
5454
);
5555

56+
await t.step("it treats the radius as an absolute value", async () => {
57+
// 64 is the default size, 256 is the given size, 10 is the desired radius.
58+
const radius = (64 / 256) * 10;
59+
const regex = new RegExp(`<rect [^>]+ rx="${radius}" ry="${radius}"`, "is");
60+
const svg = (await generate("test", { size: 256, radius: 10 })).trim();
61+
assert(regex.test(svg));
62+
});
63+
5664
// keys_test.ts covers the seed validation, this is just a sanity check.
5765
await t.step("it raises if the seed is invalid", () => {
5866
assertRejects(() => generate(1));

0 commit comments

Comments
 (0)