init
This commit is contained in:
178
.gitignore
vendored
Normal file
178
.gitignore
vendored
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
|
||||||
|
logs
|
||||||
|
_.log
|
||||||
|
npm-debug.log_
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# Caches
|
||||||
|
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
|
||||||
|
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
|
||||||
|
pids
|
||||||
|
_.pid
|
||||||
|
_.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
|
||||||
|
.temp
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.*
|
||||||
|
|
||||||
|
# IntelliJ based IDEs
|
||||||
|
.idea
|
||||||
|
|
||||||
|
# Finder (MacOS) folder config
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
output
|
||||||
|
assets
|
||||||
15
README.md
Normal file
15
README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# generative-art
|
||||||
|
|
||||||
|
To install dependencies:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun install
|
||||||
|
```
|
||||||
|
|
||||||
|
To run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bun run index.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
This project was created using `bun init` in bun v1.0.33. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
||||||
52
index.ts
Normal file
52
index.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import sharp, { type OverlayOptions } from 'sharp';
|
||||||
|
import { LAYERS, weightedChoice } from './layers';
|
||||||
|
|
||||||
|
const BASE_SHAPE_PATH = './assets/base_shape.png';
|
||||||
|
|
||||||
|
export type Trait = 'skin' | 'nose' | 'eyes' | 'mouth' | 'detail';
|
||||||
|
|
||||||
|
export type Layer = {
|
||||||
|
trait: Trait,
|
||||||
|
path: string,
|
||||||
|
chance?: number,
|
||||||
|
options: TraitOption[]
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TraitOption = {
|
||||||
|
fileName: string,
|
||||||
|
weight?: number
|
||||||
|
};
|
||||||
|
|
||||||
|
async function generateRandomImage(fileName: string, orderedLayers: Layer[]) {
|
||||||
|
const layers: { input: string }[] = [];
|
||||||
|
|
||||||
|
for (const layer of orderedLayers) {
|
||||||
|
if (layer.chance && Math.random() > layer.chance) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const choice = weightedChoice(layer.options);
|
||||||
|
|
||||||
|
layers.push({ input: `${layer.path}${choice}` });
|
||||||
|
}
|
||||||
|
|
||||||
|
const image = sharp(BASE_SHAPE_PATH).composite(layers);
|
||||||
|
|
||||||
|
await Bun.write(`./output/${fileName}`, await image.toBuffer());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
await generateRandomImage(`characters/${i}.png`, LAYERS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// combine into a single grid image
|
||||||
|
const characters: OverlayOptions[] = [];
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
characters.push({ input: `./output/characters/${i}.png`, left: (i % 10) * 16, top: Math.floor(i / 10.0) * 16 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const grid = sharp({ create: { width: 160, height: 160, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } } }).composite(characters).png();
|
||||||
|
|
||||||
|
await Bun.write('./output/grid.png', await grid.toBuffer());
|
||||||
|
|
||||||
|
console.log('Finished generation');
|
||||||
72
layers.ts
Normal file
72
layers.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import type { Layer, TraitOption } from ".";
|
||||||
|
|
||||||
|
export const LAYERS: Layer[] = [
|
||||||
|
{
|
||||||
|
trait: 'skin',
|
||||||
|
path: './assets/skin/',
|
||||||
|
options: [
|
||||||
|
{ fileName: 'sand.png' },
|
||||||
|
{ fileName: 'sienna.png' },
|
||||||
|
{ fileName: 'bole.png' },
|
||||||
|
{ fileName: 'chocolate.png' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
trait: 'nose',
|
||||||
|
path: './assets/nose/',
|
||||||
|
options: [
|
||||||
|
{ fileName: 'small.png' },
|
||||||
|
{ fileName: 'big.png' }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
trait: 'eyes',
|
||||||
|
path: './assets/eyes/',
|
||||||
|
options: [
|
||||||
|
{ fileName: 'blue.png', weight: 27 },
|
||||||
|
{ fileName: 'green.png', weight: 9 },
|
||||||
|
{ fileName: 'brown.png', weight: 19 },
|
||||||
|
{ fileName: 'dark_brown.png', weight: 45 }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
trait: 'mouth',
|
||||||
|
path: './assets/mouth/',
|
||||||
|
options: [
|
||||||
|
{ fileName: 'neutral.png', weight: 8 },
|
||||||
|
{ fileName: 'happy.png', weight: 4 },
|
||||||
|
{ fileName: 'smirk.png', weight: 2 },
|
||||||
|
{ fileName: 'shock.png', weight: 1 }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
trait: 'detail',
|
||||||
|
chance: 0.1,
|
||||||
|
path: './assets/detail/',
|
||||||
|
options: [
|
||||||
|
{ fileName: 'halo.png', weight: 1 },
|
||||||
|
{ fileName: 'red_beanie.png', weight: 25 },
|
||||||
|
{ fileName: 'green_beanie.png', weight: 25 }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
export function weightedChoice(options: TraitOption[]): string {
|
||||||
|
let i;
|
||||||
|
|
||||||
|
let weights: number[] = [options[0].weight ?? 1];
|
||||||
|
|
||||||
|
for (i = 1; i < options.length; i++) {
|
||||||
|
weights[i] = (options[i].weight ?? 1) + weights[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
const random = Math.random() * weights[weights.length - 1];
|
||||||
|
|
||||||
|
for (i = 0; i < weights.length; i++) {
|
||||||
|
if (weights[i] > random) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return options[i].fileName;
|
||||||
|
}
|
||||||
14
package.json
Normal file
14
package.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "generative-art",
|
||||||
|
"module": "index.ts",
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/bun": "latest"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"typescript": "^5.0.0"
|
||||||
|
},
|
||||||
|
"type": "module",
|
||||||
|
"dependencies": {
|
||||||
|
"sharp": "^0.33.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
27
tsconfig.json
Normal file
27
tsconfig.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
// Enable latest features
|
||||||
|
"lib": ["ESNext"],
|
||||||
|
"target": "ESNext",
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"allowJs": true,
|
||||||
|
|
||||||
|
// Bundler mode
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"noEmit": true,
|
||||||
|
|
||||||
|
// Best practices
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
|
||||||
|
// Some stricter flags (disabled by default)
|
||||||
|
"noUnusedLocals": false,
|
||||||
|
"noUnusedParameters": false,
|
||||||
|
"noPropertyAccessFromIndexSignature": false
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user