-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
123 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,89 @@ | ||
/** | ||
* Random avatar generator | ||
*/ | ||
|
||
import { createCanvas } from 'canvas'; | ||
import * as gen from 'random-seed'; | ||
|
||
const size = 512; // px | ||
const n = 5; // resolution | ||
const margin = (size / n) / 1.5; | ||
const colors = [ | ||
'#e57373', | ||
'#F06292', | ||
'#BA68C8', | ||
'#9575CD', | ||
'#7986CB', | ||
'#64B5F6', | ||
'#4FC3F7', | ||
'#4DD0E1', | ||
'#4DB6AC', | ||
'#81C784', | ||
'#8BC34A', | ||
'#AFB42B', | ||
'#F57F17', | ||
'#FF5722', | ||
'#795548', | ||
'#455A64', | ||
]; | ||
const bg = '#e9e9e9'; | ||
|
||
const actualSize = size - (margin * 2); | ||
const cellSize = actualSize / n; | ||
const sideN = Math.floor(n / 2); | ||
|
||
/** | ||
* Generate buffer of random avatar by seed | ||
*/ | ||
export function genAvatar(seed: string) { | ||
const rand = gen.create(seed); | ||
const canvas = createCanvas(size, size); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
ctx.fillStyle = bg; | ||
ctx.beginPath(); | ||
ctx.fillRect(0, 0, size, size); | ||
|
||
ctx.fillStyle = colors[rand(colors.length)]; | ||
|
||
// side bitmap (filled by false) | ||
const side: boolean[][] = new Array(sideN); | ||
for (let i = 0; i < side.length; i++) { | ||
side[i] = new Array(n).fill(false); | ||
} | ||
|
||
// 1*n (filled by false) | ||
const center: boolean[] = new Array(n).fill(false); | ||
|
||
// tslint:disable-next-line:prefer-for-of | ||
for (let x = 0; x < side.length; x++) { | ||
for (let y = 0; y < side[x].length; y++) { | ||
side[x][y] = rand(3) === 0; | ||
} | ||
} | ||
|
||
for (let i = 0; i < center.length; i++) { | ||
center[i] = rand(3) === 0; | ||
} | ||
|
||
// Draw | ||
for (let x = 0; x < n; x++) { | ||
for (let y = 0; y < n; y++) { | ||
const isXCenter = x === ((n - 1) / 2); | ||
if (isXCenter && !center[y]) continue; | ||
|
||
const isLeftSide = x < ((n - 1) / 2); | ||
if (isLeftSide && !side[x][y]) continue; | ||
|
||
const isRightSide = x > ((n - 1) / 2); | ||
if (isRightSide && !side[sideN - (x - sideN)][y]) continue; | ||
|
||
const actualX = margin + (cellSize * x); | ||
const actualY = margin + (cellSize * y); | ||
ctx.beginPath(); | ||
ctx.fillRect(actualX, actualY, cellSize, cellSize); | ||
} | ||
} | ||
|
||
return canvas.toBuffer(); | ||
} |
This file contains 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 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 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 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 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
Binary file not shown.
This file contains 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 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 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