es6 class syntax
This commit is contained in:
parent
0f31f2556c
commit
745c197f09
@ -2,7 +2,8 @@ import { create2dState, create1dStateOneCell, create1dState } from "./core.js";
|
|||||||
|
|
||||||
import { getRandomInt } from "./common.js";
|
import { getRandomInt } from "./common.js";
|
||||||
|
|
||||||
function Board(width, height, grid = []) {
|
class Board {
|
||||||
|
constructor(width, height, grid = []) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
(this.cellProperties = {
|
(this.cellProperties = {
|
||||||
@ -11,6 +12,7 @@ function Board(width, height, grid = []) {
|
|||||||
deadColor: "#F5F5F5",
|
deadColor: "#F5F5F5",
|
||||||
}),
|
}),
|
||||||
(this.grid = grid);
|
(this.grid = grid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a first state, either a single living cell
|
// create a first state, either a single living cell
|
||||||
|
@ -1,17 +1,18 @@
|
|||||||
import { boardToPic, scaleToTargetSize } from "./picture.js";
|
import { boardToPic, scaleToTargetSize } from "./picture.js";
|
||||||
|
|
||||||
function scaleAndApply(context, ratio, callback) {
|
class Renderer {
|
||||||
context.save();
|
constructor() {
|
||||||
// rescale
|
this.canvas = null;
|
||||||
context.imageSmoothingEnabled = false;
|
this.workCanvas = null;
|
||||||
context.scale(ratio, ratio);
|
this.workCtx = null;
|
||||||
// apply
|
this.width = null;
|
||||||
callback();
|
this.height = null;
|
||||||
context.restore();
|
this.ctx = null;
|
||||||
}
|
this.refreshRate = 300;
|
||||||
|
}
|
||||||
|
|
||||||
// draws the board representation on the canvas
|
// draws the board representation on the canvas
|
||||||
function render(board) {
|
render(board) {
|
||||||
const d = board.cellProperties.size;
|
const d = board.cellProperties.size;
|
||||||
// bool to RGBA colors
|
// bool to RGBA colors
|
||||||
const img = boardToPic(board);
|
const img = boardToPic(board);
|
||||||
@ -20,10 +21,10 @@ function render(board) {
|
|||||||
this.workCtx.putImageData(img, 0, 0);
|
this.workCtx.putImageData(img, 0, 0);
|
||||||
this.ctx.drawImage(this.workCanvas, 0, 0, this.width, this.height);
|
this.ctx.drawImage(this.workCanvas, 0, 0, this.width, this.height);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw image on canvas
|
// draw image on canvas
|
||||||
function renderImage(image, ctx, tw, th) {
|
renderImage(image, ctx, tw, th) {
|
||||||
ctx.fillStyle = "black";
|
ctx.fillStyle = "black";
|
||||||
ctx.fillRect(0, 0, tw, th);
|
ctx.fillRect(0, 0, tw, th);
|
||||||
const dimensions = scaleToTargetSize(image.width, image.height, tw, th);
|
const dimensions = scaleToTargetSize(image.width, image.height, tw, th);
|
||||||
@ -35,33 +36,32 @@ function renderImage(image, ctx, tw, th) {
|
|||||||
dimensions[1]
|
dimensions[1]
|
||||||
);
|
);
|
||||||
return ctx.getImageData(0, 0, tw, th);
|
return ctx.getImageData(0, 0, tw, th);
|
||||||
}
|
}
|
||||||
|
|
||||||
function resize(width, height) {
|
// resize canvas and workcavas
|
||||||
|
resize(width, height) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.canvas.height = height;
|
this.canvas.height = height;
|
||||||
this.canvas.width = width;
|
this.canvas.width = width;
|
||||||
this.workCanvas.height = height;
|
this.workCanvas.height = height;
|
||||||
this.workCanvas.width = width;
|
this.workCanvas.width = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset() {
|
// clear any drawing from canvas
|
||||||
|
reset() {
|
||||||
this.ctx.clearRect(0, 0, this.width, this.height);
|
this.ctx.clearRect(0, 0, this.width, this.height);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Renderer() {
|
function scaleAndApply(context, ratio, callback) {
|
||||||
this.canvas = null;
|
context.save();
|
||||||
this.workCanvas = null;
|
// rescale
|
||||||
this.workCtx = null;
|
context.imageSmoothingEnabled = false;
|
||||||
this.width = null;
|
context.scale(ratio, ratio);
|
||||||
this.height = null;
|
// apply
|
||||||
this.ctx = null;
|
callback();
|
||||||
this.refreshRate = 300;
|
context.restore();
|
||||||
this.render = render;
|
|
||||||
this.renderImage = renderImage;
|
|
||||||
this.reset = reset;
|
|
||||||
this.resize = resize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Renderer };
|
export { Renderer };
|
||||||
|
Loading…
Reference in New Issue
Block a user