Skip to content

Commit

Permalink
Add node-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazerbeak12345 committed Apr 5, 2022
1 parent 3138988 commit cab9b6b
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 12 deletions.
116 changes: 116 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixelmanipulator",
"version": "5.0.0",
"version": "5.1.0",
"description": "Run any cellular automata on an html5 canvas.",
"main": "dist/main.js",
"browser": "dist/browser.js",
Expand Down Expand Up @@ -63,6 +63,7 @@
"@fortawesome/fontawesome-free": "^6.1.1",
"@parcel/packager-ts": "^2.4.1",
"@parcel/transformer-typescript-types": "^2.4.1",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.0",
"bootstrap-dark-5": "^1.1.3",
Expand All @@ -75,6 +76,7 @@
"eslint-plugin-tsdoc": "^0.2.14",
"gh-pages": "^3.2.3",
"parcel": "^2.4.1",
"ts-node": "^10.7.0",
"typedoc": "^0.22.13",
"typedoc-plugin-mdn-links": "^1.0.5",
"typescript": "<4.5.0"
Expand All @@ -90,6 +92,7 @@
"build:bundle": "browserify dist/browser.js -o dist/bundle.js --standalone pixelmanipulator",
"build": "npm run check && npm run build:docs && npm run build:parcel && npm run build:bundle",
"updatedemo": "npm run build && gh-pages -d docs -m \"Update $npm_package_version\" -tf",
"node-demo": "ts-node src/node-demo",
"coverage": "npm run test"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,13 @@ p.addMultipleElements({
// not quite white
renderAs: [254, 254, 254, 255],
// Cells that were in the dying state go into the off state
liveCell: ({ x, y }) => p.setPixel({ x, y, loop: false }, p.defaultId)
liveCell: loc => p.setPixel(loc, p.defaultId)
},
"Brian's Brain (on)": {
...rules.lifelike(p, 'B2/S'), // same pattern as seeds
renderAs: [0, 0, 254, 255], // not quite blue
// All cells that were "on" go into the "dying" state, which is not counted as an "on" cell in the neighbor count, and prevents any cell from being born there.
liveCell: ({ x, y }) => p.setPixel({ x, y, loop: false }, "Brian's Brain (dying)")
liveCell: loc => p.setPixel(loc, "Brian's Brain (dying)")
},
Seeds: {
...rules.lifelike(p, 'B2/S'),
Expand Down
34 changes: 25 additions & 9 deletions src/lib/pixelmanipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ import { Renderer, Location, location2Index } from './renderers'
// export * as neighborhoods from './neighborhoods'
import * as _neighborhoods from './neighborhoods'
export { _neighborhoods as neighborhoods }
function startAnimation (callback: () => void): number {
if (typeof requestAnimationFrame === 'undefined') {
return window.setInterval(callback, 1)
} else {
return requestAnimationFrame(callback)
}
}
function resumeAnimation (num: number, callback: () => void): number {
if (typeof requestAnimationFrame === 'undefined') {
return num
} else {
return requestAnimationFrame(callback)
}
}
function cancelAnimation (num: number): void {
if (typeof cancelAnimationFrame === 'undefined') {
clearInterval(num)
} else {
cancelAnimationFrame(num)
}
}
function boolToNumber (bool: boolean): number {
return bool ? 1 : 0
}
Expand Down Expand Up @@ -393,7 +414,7 @@ export class PixelManipulator<T> {
play (canvasSizes?: CanvasSizes): void {
if (this.mode === 'playing') this.reset(canvasSizes)
this.mode = 'playing'
this.loopint = window.requestAnimationFrame(() => {
this.loopint = startAnimation(() => {
this.iterate()
})
}
Expand Down Expand Up @@ -430,7 +451,7 @@ export class PixelManipulator<T> {
*/
pause (): void {
this.mode = 'paused'
window.cancelAnimationFrame(this.loopint)
cancelAnimation(this.loopint)
}

/**
Expand Down Expand Up @@ -630,7 +651,7 @@ export class PixelManipulator<T> {
this.update()
this.onAfterIterate()
if (this.mode === 'playing') {
this.loopint = window.requestAnimationFrame(() => {
this.loopint = resumeAnimation(this.loopint, () => {
this.iterate()
})
}
Expand All @@ -653,11 +674,6 @@ export const licence = 'PixelManipulator v' + version + ' Copyright (C) ' +
'WARRANTY\nThis is free software, and you are welcome to redistribute it\n' +
'under certain conditions, as according to the GNU GENERAL PUBLIC LICENSE ' +
'version 3 or later.'
if (typeof window === 'undefined') {
console.warn(
'This enviroment has not been tested, and is officially not supported.\n' +
'Good luck.'
)
} else console.log(licence)
if (typeof window !== 'undefined') console.log(licence)
// This is called a "modeline". It's a (n)vi(m)|ex thing.
// vi: tabstop=2 shiftwidth=2 expandtab
19 changes: 19 additions & 0 deletions src/node-demo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PixelManipulator, rules, StringRenderer } from '../lib/pixelmanipulator'

const renderer = new StringRenderer(console.log)
renderer.defaultRenderAs = '`'
const p = new PixelManipulator(renderer, process.stdout.columns, process.stdout.rows - 1)
const bbd = p.addElement({
name: "Brian's Brain (dying)",
renderAs: '#',
liveCell: loc => p.setPixel(loc, p.defaultId)
})
const bbo = p.addElement({
...rules.lifelike(p, 'B2/S'),
name: "Brian's Brain (dying)",
renderAs: '0',
liveCell: loc => p.setPixel(loc, bbd)
})
p.randomlyFill(bbo)
setInterval(() => p.iterate(), 1000 / 15)
// vim: tabstop=2 shiftwidth=2 expandtab

0 comments on commit cab9b6b

Please sign in to comment.