explorata/src/components/Canvas.vue

129 lines
3.4 KiB
Vue
Raw Normal View History

2022-01-08 13:12:15 +01:00
<template>
<main id="main">
<canvas
id="canvas"
ref="canvas"
2022-01-10 14:16:34 +01:00
:width="canvasWidth"
:height="canvasHeight"
/>
2022-01-08 13:12:15 +01:00
</main>
</template>
<script>
2022-01-11 13:16:42 +01:00
import { create1dState, create1dStateOneCell, create2dState, createBoard, conwayRules, evolve2d } from '../modules/automata.js'
2022-01-10 13:50:02 +01:00
import { getRandomInt, sleep } from '../modules/common.js'
2022-01-10 11:13:44 +01:00
import {mapGetters} from 'vuex'
export default {
name: 'Canvas',
data() {
return {
canvas: null,
2022-01-10 11:13:44 +01:00
ctx: null,
}
},
computed: {
2022-01-10 11:13:44 +01:00
...mapGetters({
cellProperties: 'getCellProperties',
rules: 'get1dRules',
2022-01-10 14:16:34 +01:00
drawing: 'isDrawing',
canvasWidth: 'getCanvasWidth',
2022-01-10 20:49:09 +01:00
canvasHeight: 'getCanvasHeight',
2022-01-11 13:16:42 +01:00
refreshRate: 'getRefreshRate',
2022-01-13 18:07:41 +01:00
initial1dState: 'getInitial1dState',
drawingDirection: 'getDrawingDirection',
lastBoard: 'getLastBoard'
2022-01-11 13:16:42 +01:00
}),
boardWidth: function() {
return Math.floor(
this.canvasWidth /
2022-01-11 13:16:42 +01:00
this.cellProperties.size)
},
boardHeight: function() {
return Math.floor(
this.canvasHeight /
2022-01-11 13:16:42 +01:00
this.cellProperties.size)
}
},
mounted() {
this.canvas = this.$refs['canvas']
2022-01-10 11:13:44 +01:00
this.ctx = this.canvas.getContext('2d')
this.$root.$on('draw1d', () => { this.draw1d() })
this.$root.$on('draw2dNew', () => { this.draw2dNew() })
this.$root.$on('draw2dLast', () => { this.draw2dLast() })
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() })
},
methods: {
drawCanvas(board) {
2022-01-10 11:13:44 +01:00
const props = this.cellProperties
board.map((row, y) => {
2022-01-10 11:13:44 +01:00
const d = props.size
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
})()
2022-01-13 18:07:41 +01:00
if (this.drawingDirection === "x")
this.ctx.fillRect(y * d, x * d, d, d)
else
this.ctx.fillRect(x * d, y * d, d, d)
2022-01-10 11:13:44 +01:00
return cell
},
2022-01-10 11:13:44 +01:00
)
})
},
2022-01-11 13:16:42 +01:00
compute1dInitialState() {
if(this.initial1dState === "onecell")
return create1dStateOneCell(this.boardWidth)
return create1dState(this.boardWidth, getRandomInt, [0, 2])
},
2022-01-10 11:13:44 +01:00
async draw1d() {
2022-01-11 13:16:42 +01:00
const initialState = this.compute1dInitialState()
const board = createBoard(
initialState,
2022-01-10 11:13:44 +01:00
this.rules,
2022-01-13 18:07:41 +01:00
this.boardWidth
2022-01-10 11:13:44 +01:00
)
this.$store.commit('setLastBoard', board)
2022-01-10 11:13:44 +01:00
this.drawCanvas(board)
},
async draw2d(board) {
2022-01-10 13:50:02 +01:00
if (this.drawing === 0) return
const draw2dNext = async (b) => {
if (this.drawing === 0) return
const newBoard = evolve2d(b, conwayRules)
this.drawCanvas(b, this.cellProperties)
2022-01-10 20:49:09 +01:00
await sleep(this.refreshRate)
2022-01-10 13:50:02 +01:00
draw2dNext(newBoard)
}
return draw2dNext(board)
},
async draw2dNew() {
const initialState = create2dState(
this.boardWidth,
this.boardHeight,
getRandomInt,
[0, 2],
);
const board = evolve2d(initialState, conwayRules);
this.$store.commit('setLastBoard', board)
this.draw2d(board)
},
async draw2dLast() {
console.log(this.lastBoard)
this.draw2d(this.lastBoard)
},
2022-01-10 13:50:02 +01:00
stop() {
2022-01-10 14:16:34 +01:00
this.$store.commit('setDrawingStatus', 0)
2022-01-10 13:50:02 +01:00
},
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)
}
}
}
</script>