diff --git a/src/components/CanvasBoard.vue b/src/components/CanvasBoard.vue
index 9291b1c..3b1ff82 100644
--- a/src/components/CanvasBoard.vue
+++ b/src/components/CanvasBoard.vue
@@ -1,7 +1,13 @@
+
@@ -18,12 +24,15 @@
evolve2d,
} from "../modules/automata.js";
import { getRandomInt, sleep } from "../modules/common.js";
+ import { picToBoard, picToBlackAndWhite } from "../modules/picture.js";
export default {
name: "CanvasBoard",
data() {
return {
canvas: null,
+ workCanvas: null,
+ workCtx: null,
ctx: null,
};
},
@@ -72,8 +81,11 @@
},
},
mounted() {
- this.canvas = Object.freeze(document.getElementById("canvas-board"));
+ this.canvas = Object.freeze(document.getElementById("board-canvas"));
+ this.workCanvas = Object.freeze(document.getElementById("work-canvas"));
this.ctx = this.canvas.getContext("2d");
+ this.workCtx = this.workCanvas.getContext("2d");
+ this.workCanvas.setAttribute("willReadFrequently", true);
this.canvasWidth = this.canvas.parentElement.clientWidth;
this.canvasHeight = this.canvas.parentElement.clientHeight;
this.setBoardWidth();
@@ -114,7 +126,7 @@
const initialState = this.compute1dInitialState();
const board = createBoard(initialState, this.ruleset.rules, this.max);
this.lastBoard = Object.freeze(board);
- this.drawCanvas(board);
+ this.drawCanvas(this.lastBoard);
this.toggleStop();
},
// draw 2D automaton on the canvas (currently only the game of life)
@@ -145,17 +157,55 @@
draw2dLast() {
if (this.lastBoard != undefined) this.draw2d(this.lastBoard);
},
- // draw 2d automaton from an uploaded picture
- draw2dPicture(e) {
- this.picture.width = this.canvas.width;
- this.picture.height = this.canvas.height;
- this.ctx.drawImage(
+ // draw 2d automaton from an uploaded picture.
+ // use the picture representation as an initial state
+ draw2dPicture() {
+ // get image data by drawing it on a work canvas
+ this.workCtx.drawImage(
this.picture,
0,
0,
- this.canvas.width,
- this.canvas.height
+ this.canvasWidth,
+ this.canvasHeight
);
+ const imgData = this.workCtx.getImageData(
+ 0,
+ 0,
+ this.canvasWidth,
+ this.canvasHeight
+ );
+
+ // convert the image to black and white
+ const black = picToBlackAndWhite(
+ imgData.data,
+ this.canvasWidth,
+ this.canvasHeight
+ );
+
+ // draw it back on the canvas
+ this.ctx.putImageData(black, 0, 0);
+
+ // draw the image back on the work canvas with the dimensions of the board
+ this.workCtx.drawImage(
+ this.picture,
+ 0,
+ 0,
+ this.boardWidth,
+ this.boardHeight
+ );
+
+ const resized = this.workCtx.getImageData(
+ 0,
+ 0,
+ this.boardWidth,
+ this.boardHeight
+ );
+
+ // convert the resized image into a 2D board of boolean based on pixel value
+ this.lastBoard = Object.freeze(
+ picToBoard(resized.data, this.boardWidth, this.boardHeight)
+ );
+
this.toggleStop();
},
// stop drawing routines and clear the canvas
diff --git a/src/components/MenuCellProperties.vue b/src/components/MenuCellProperties.vue
index 33a4786..b8e42cc 100644
--- a/src/components/MenuCellProperties.vue
+++ b/src/components/MenuCellProperties.vue
@@ -34,7 +34,7 @@