2022-01-08 13:12:15 +01:00
|
|
|
<template>
|
|
|
|
<main id="main">
|
2022-01-09 11:47:18 +01:00
|
|
|
<canvas
|
|
|
|
id="canvas"
|
|
|
|
ref="canvas"
|
|
|
|
width="500"
|
|
|
|
height="500"
|
|
|
|
/>
|
2022-01-08 13:12:15 +01:00
|
|
|
</main>
|
|
|
|
</template>
|
2022-01-09 11:47:18 +01:00
|
|
|
<script>
|
2022-01-10 13:50:02 +01:00
|
|
|
import { evolve2d, initialState1d, initialState2d, conwayRules, createBoard } from '../modules/automata.js'
|
|
|
|
import { getRandomInt, sleep } from '../modules/common.js'
|
2022-01-10 11:13:44 +01:00
|
|
|
import {mapGetters} from 'vuex'
|
2022-01-09 11:47:18 +01:00
|
|
|
export default {
|
|
|
|
name: 'Canvas',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
canvas: null,
|
2022-01-10 11:13:44 +01:00
|
|
|
ctx: null,
|
2022-01-09 11:47:18 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-01-10 11:13:44 +01:00
|
|
|
...mapGetters({
|
|
|
|
cellProperties: 'getCellProperties',
|
|
|
|
rules: 'getRuleSet1d',
|
2022-01-10 13:50:02 +01:00
|
|
|
drawing: 'isDrawing'
|
2022-01-10 11:13:44 +01:00
|
|
|
})
|
2022-01-09 11:47:18 +01:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.canvas = this.$refs['canvas']
|
|
|
|
this.canvas.width = 1280
|
|
|
|
this.canvas.height = 720
|
2022-01-10 11:13:44 +01:00
|
|
|
this.ctx = this.canvas.getContext('2d')
|
2022-01-09 11:47:18 +01:00
|
|
|
this.$root.$on('draw1d', () => { this.draw1d() })
|
2022-01-10 13:50:02 +01:00
|
|
|
this.$root.$on('draw2d', () => { this.draw2d() })
|
2022-01-10 11:13:44 +01:00
|
|
|
this.$root.$on('reset', () => { this.reset() })
|
2022-01-10 13:50:02 +01:00
|
|
|
this.$root.$on('stop', () => { this.stop() })
|
2022-01-09 11:47:18 +01:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
drawCanvas(board) {
|
2022-01-10 11:13:44 +01:00
|
|
|
const props = this.cellProperties
|
2022-01-09 11:47:18 +01:00
|
|
|
board.map((row, y) => {
|
2022-01-10 11:13:44 +01:00
|
|
|
const d = props.size
|
2022-01-09 11:47:18 +01:00
|
|
|
return row.map(
|
|
|
|
(cell, x) => {
|
|
|
|
this.ctx.fillStyle = (
|
|
|
|
() => {
|
2022-01-10 11:13:44 +01:00
|
|
|
if (cell === 1) return props.liveColor
|
|
|
|
return props.deadColor
|
|
|
|
})()
|
|
|
|
this.ctx.fillRect(x * d, y * d, d, d)
|
|
|
|
return cell
|
2022-01-09 11:47:18 +01:00
|
|
|
},
|
2022-01-10 11:13:44 +01:00
|
|
|
)
|
|
|
|
})
|
2022-01-09 11:47:18 +01:00
|
|
|
},
|
2022-01-10 11:13:44 +01:00
|
|
|
async draw1d() {
|
2022-01-09 11:47:18 +01:00
|
|
|
const initialState = initialState1d(
|
2022-01-10 11:13:44 +01:00
|
|
|
Math.floor(this.canvas.width / this.cellProperties.size),
|
2022-01-09 11:47:18 +01:00
|
|
|
getRandomInt,
|
|
|
|
[0, 2],
|
2022-01-10 11:13:44 +01:00
|
|
|
)
|
2022-01-09 11:47:18 +01:00
|
|
|
const board = createBoard(
|
|
|
|
initialState,
|
2022-01-10 11:13:44 +01:00
|
|
|
this.rules,
|
|
|
|
Math.floor(this.canvas.height / this.cellProperties.size)
|
|
|
|
)
|
|
|
|
this.drawCanvas(board)
|
|
|
|
},
|
2022-01-10 13:50:02 +01:00
|
|
|
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
|
|
|
|
},
|
2022-01-10 11:13:44 +01:00
|
|
|
reset() {
|
2022-01-10 13:50:02 +01:00
|
|
|
this.stop()
|
2022-01-10 11:13:44 +01:00
|
|
|
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
|
2022-01-09 11:47:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|