working vue components for 1D automata

This commit is contained in:
Ali Gator 2022-01-10 11:13:44 +01:00
parent 5fe9243560
commit 59d821d084
7 changed files with 123 additions and 123 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ pnpm-debug.log*
*.njsproj *.njsproj
*.sln *.sln
*.sw? *.sw?
*.#*

View File

@ -11,61 +11,63 @@
<script> <script>
import { initialState1d, createBoard } from '../modules/automata.js' import { initialState1d, createBoard } from '../modules/automata.js'
import { getRandomInt } from '../modules/common.js' import { getRandomInt } from '../modules/common.js'
import {mapGetters} from 'vuex'
export default { export default {
name: 'Canvas', name: 'Canvas',
data() { data() {
return { return {
canvas: null, canvas: null,
ctx: null ctx: null,
} }
}, },
computed: { computed: {
cellProperties() { ...mapGetters({
return this.$store.state.cellProperties cellProperties: 'getCellProperties',
}, rules: 'getRuleSet1d',
rules() { })
return this.$store.state.rules1d
}
}, },
mounted() { mounted() {
this.canvas = this.$refs['canvas'] this.canvas = this.$refs['canvas']
this.canvas.width = 1280 this.canvas.width = 1280
this.canvas.height = 720 this.canvas.height = 720
this.context = this.canvas.getContext('2d') this.ctx = this.canvas.getContext('2d')
this.$root.$on('draw1d', () => { this.draw1d() }) this.$root.$on('draw1d', () => { this.draw1d() })
this.$root.$on('reset', () => { this.reset() })
}, },
methods: { methods: {
drawCanvas(board) { drawCanvas(board) {
const props = this.CellProperties; const props = this.cellProperties
board.map((row, y) => { board.map((row, y) => {
const d = props.size; const d = props.size
return row.map( return row.map(
(cell, x) => { (cell, x) => {
this.ctx.fillStyle = ( this.ctx.fillStyle = (
() => { () => {
if (cell === 1) return props.liveColor; if (cell === 1) return props.liveColor
return props.deadColor; return props.deadColor
})(); })()
this.ctx.fillRect(x * d, y * d, d, d); this.ctx.fillRect(x * d, y * d, d, d)
return cell; return cell
}, },
); )
}); })
}, },
draw1d() { async draw1d() {
const rules = this.rules;
const props = this.CellProperties;
console.log(props.size)
const initialState = initialState1d( const initialState = initialState1d(
Math.floor(this.canvas.width / props.size), Math.floor(this.canvas.width / this.cellProperties.size),
getRandomInt, getRandomInt,
[0, 2], [0, 2],
); )
const board = createBoard( const board = createBoard(
initialState, initialState,
rules, this.rules,
Math.floor(this.canvas.height / props.size)); Math.floor(this.canvas.height / this.cellProperties.size)
this.drawCanvas(board); )
this.drawCanvas(board)
},
reset() {
this.$root.$store.state.drawing = 0;
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
} }
} }
} }

View File

@ -0,0 +1,9 @@
<template>
<div class="form-field" v-if="type === 'checkbox'">
<label>{{ name }}
<input type="checkbox" name="{{ name }}">
</label>
</div>
<div class="form-field" v-else-if="type === ''">
</div>
</template>

View File

@ -0,0 +1,16 @@
<template>
<div class="menu-row">
<div class="form-field">
<label for="live">Canvas Resolution</label>
</div>
<div class="form-field">
<input name="canvasWidth" type="number" id="canvasWidth" value="1280"/>
</div>
<div class="form-field">
<input name="canvasHeight" type="number" id="canvasHeight" value="1024"/>
</div>
<div class="form-field">
<input type="button" name="canvasRefresh" id="canvasRefresh" value="refresh"/>
</div>
</div>
</template>

View File

@ -4,31 +4,28 @@
<div class="form-field"> <div class="form-field">
<label for="live">Living cell color</label> <label for="live">Living cell color</label>
<input <input
id="live" name="liveColor"
name="live"
type="color" type="color"
:value="liveColor" @value="cellProperties.liveColor"
@click="updateCellProperties" @input="updateCellProperties"
> >
</div> </div>
<div class="form-field"> <div class="form-field">
<label for="dead">Dead cell color</label> <label for="dead">Dead cell color</label>
<input <input
id="dead" name="deadColor"
name="dead"
type="color" type="color"
:value="deadColor" :value="cellProperties.deadColor"
@click="updateCellProperties" @input="updateCellProperties"
> >
</div> </div>
<div class="form-field"> <div class="form-field">
<label>Cell size</label> <label>Cell size</label>
<input <input
id="cellSize" name="size"
name="cellSize"
type="number" type="number"
:value="size" :value="cellProperties.size"
@click="updateCellProperties" @input="updateCellProperties"
> >
</div> </div>
</form> </form>
@ -44,15 +41,19 @@ export default {
}, },
data() { data() {
return { return {
size : this.$store.state.cellProperties['size'], cellProperties: this.$store.state.cellProperties
liveColor : this.$store.state.cellProperties['liveColor'],
deadColor : this.$store.state.cellProperties['deadColor'],
} }
}, },
methods: { methods: {
getCellProperties(event) {
const elem = event.target
const prop = this.$store.state.cellProperties
return prop[elem.name]
},
updateCellProperties(event) { updateCellProperties(event) {
const elem = event.target const elem = event.target
const prop = {'name' : elem.name, 'value' : elem.value} const prop = {'name' : elem.name, 'value' : elem.value}
//console.log(prop)
this.$store.commit('setCellProperties', prop) this.$store.commit('setCellProperties', prop)
} }
}, },

View File

@ -4,79 +4,18 @@
<label>Rules</label> <label>Rules</label>
</div> </div>
<form> <form>
<div class="form-field"> <div
<label>111 v-for="rule in rules"
<input :key="rule"
type="checkbox" class="form-field"
name="111"
checked
@change="update1dRules"
> >
</label> <label>{{ rule }}
</div>
<div class="form-field">
<label>110
<input <input
:value="getRule(rule)"
type="checkbox" type="checkbox"
name="110" :name="rule"
@click="update1dRules" :checked="getRule(rule) == 1"
> @input="update1dRules"
</label>
</div>
<div class="form-field">
<label>101
<input
type="checkbox"
name="101"
checked
@click="update1dRules"
>
</label>
</div>
<div class="form-field">
<label>100
<input
type="checkbox"
name="100"
@click="update1dRules"
>
</label>
</div>
<div class="form-field">
<label>011
<input
type="checkbox"
name="011"
checked
@click="update1dRules"
>
</label>
</div>
<div class="form-field">
<label>010
<input
type="checkbox"
name="010"
@click="update1dRules"
>
</label>
</div>
<div class="form-field">
<label>001
<input
type="checkbox"
name="001"
@click="update1dRules"
>
</label>
</div>
<div class="form-field">
<label>000
<input
type="checkbox"
name="000"
checked
@click="update1dRules"
> >
</label> </label>
</div> </div>
@ -88,17 +27,18 @@
value="start" value="start"
@click="draw1d" @click="draw1d"
> >
<input <!-- <input -->
type="button" <!-- type="button" -->
name="stop" <!-- name="stop" -->
class="stop" <!-- class="stop" -->
value="stop" <!-- value="stop" -->
> <!-- > -->
<input <input
type="button" type="button"
name="reset" name="reset"
class="reset" class="reset"
value="reset" value="reset"
@click="reset()"
> >
</div> </div>
</MenuRow> </MenuRow>
@ -111,18 +51,28 @@ export default {
components: { components: {
MenuRow MenuRow
}, },
data() {
return {
rules: ["111", "110", "101", "100", "011", "010", "001", "000"]
}
},
methods: { methods: {
update1dRules(event) { update1dRules(event) {
const elem = event.target const elem = event.target
const value = elem.value == 'on' ? '1' : '0' const value = elem.checked ? 1 : 0
const data = { 'rule' : elem.name, 'value' : value} const data = { 'rule' : elem.name, 'value' : value}
this.$store.commit('update1dRules', data) this.$store.commit('update1dRules', data)
}, },
draw1d() { draw1d() {
this.$root.$emit('draw1d') this.$root.$emit('draw1d')
}, },
mounted() { getRule(id) {
} const rules = this.$store.state.rules1d
return rules[id]
},
reset() {
this.$root.$emit('reset')
},
} }
} }
</script> </script>

View File

@ -5,9 +5,19 @@ Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
state: { state: {
rules1d : {}, drawing: 0,
rules1d : {
"111" : 0,
"110" : 1,
"101" : 0,
"100" : 0,
"011" : 1,
"010" : 0,
"001" : 0,
"000" : 1
},
cellProperties: { cellProperties: {
size: 5, size: 3,
liveColor: '#000000', liveColor: '#000000',
deadColor: '#AA78E8', deadColor: '#AA78E8',
}, },
@ -20,6 +30,17 @@ export default new Vuex.Store({
state.cellProperties[data.name] = data.value state.cellProperties[data.name] = data.value
}, },
}, },
getters: {
getCellProperties(state) {
return state.cellProperties
},
getRuleSet1d(state) {
return state.rules1d
},
getRule1d(state) {
return (rule) => state.rules1d[rule]
}
},
actions: { actions: {
}, },
modules: { modules: {