working vue components for 2D automata

This commit is contained in:
Ali Gator 2022-01-10 13:50:02 +01:00
parent 59d821d084
commit 74a1b311b7
4 changed files with 47 additions and 4 deletions

View File

@ -9,8 +9,8 @@
</main> </main>
</template> </template>
<script> <script>
import { initialState1d, createBoard } from '../modules/automata.js' import { evolve2d, initialState1d, initialState2d, conwayRules, createBoard } from '../modules/automata.js'
import { getRandomInt } from '../modules/common.js' import { getRandomInt, sleep } from '../modules/common.js'
import {mapGetters} from 'vuex' import {mapGetters} from 'vuex'
export default { export default {
name: 'Canvas', name: 'Canvas',
@ -24,6 +24,7 @@ export default {
...mapGetters({ ...mapGetters({
cellProperties: 'getCellProperties', cellProperties: 'getCellProperties',
rules: 'getRuleSet1d', rules: 'getRuleSet1d',
drawing: 'isDrawing'
}) })
}, },
mounted() { mounted() {
@ -32,7 +33,9 @@ export default {
this.canvas.height = 720 this.canvas.height = 720
this.ctx = 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('draw2d', () => { this.draw2d() })
this.$root.$on('reset', () => { this.reset() }) this.$root.$on('reset', () => { this.reset() })
this.$root.$on('stop', () => { this.stop() })
}, },
methods: { methods: {
drawCanvas(board) { drawCanvas(board) {
@ -65,8 +68,30 @@ export default {
) )
this.drawCanvas(board) this.drawCanvas(board)
}, },
async draw2d() {
if (this.drawing === 0) return
const initialState = initialState2d(
Math.floor(this.canvas.width / this.cellProperties.size),
Math.floor(this.canvas.height / this.cellProperties.size),
getRandomInt,
[0, 2],
);
const board = evolve2d(initialState, conwayRules);
const draw2dNext = async (b) => {
if (this.drawing === 0) return
const newBoard = evolve2d(b, conwayRules)
this.drawCanvas(b, this.cellProperties)
await sleep(300)
draw2dNext(newBoard)
}
return draw2dNext(board)
},
stop() {
this.$root.$store.state.drawing = 0
},
reset() { reset() {
this.$root.$store.state.drawing = 0; this.stop()
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
} }
} }

View File

@ -2,22 +2,24 @@
<MenuRow row-title="2D Cellular Automata"> <MenuRow row-title="2D Cellular Automata">
<div class="form-field"> <div class="form-field">
<input <input
id="start2d"
type="button" type="button"
name="start2d" name="start2d"
value="start" value="start"
@click="draw2d"
> >
<input <input
type="button" type="button"
name="stop" name="stop"
class="stop" class="stop"
value="stop" value="stop"
@click="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>
@ -29,6 +31,18 @@ export default {
name: 'Menu2dCA', name: 'Menu2dCA',
components: { components: {
MenuRow MenuRow
},
methods: {
draw2d() {
this.$root.$store.state.drawing = 1
this.$root.$emit('draw2d')
},
reset() {
this.$root.$emit('reset')
},
stop() {
this.$root.$emit('stop')
},
} }
} }
</script> </script>

View File

@ -64,6 +64,7 @@ export default {
this.$store.commit('update1dRules', data) this.$store.commit('update1dRules', data)
}, },
draw1d() { draw1d() {
this.$root.$store.state.drawing = 1
this.$root.$emit('draw1d') this.$root.$emit('draw1d')
}, },
getRule(id) { getRule(id) {

View File

@ -39,6 +39,9 @@ export default new Vuex.Store({
}, },
getRule1d(state) { getRule1d(state) {
return (rule) => state.rules1d[rule] return (rule) => state.rules1d[rule]
},
isDrawing(state) {
return state.drawing
} }
}, },
actions: { actions: {