Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function shuffle(array) {
- var m = array.length, t, i;
- while (m) {
- i = Math.floor(Math.random() * m--);
- t = array[m];
- array[m] = array[i];
- array[i] = t;
- }
- return array;
- }
- var Grid = (function () {
- function Grid(width, height) {
- this.width = width;
- this.height = height;
- this.content = new Array(width);
- var i;
- for (i = 0; i < this.width; i++)
- this.content[i] = new Array(height);
- this.reset();
- this.traversal = 0;
- }
- Grid.prototype.reset = function () {
- var i;
- var j;
- for (i = 0; i < this.width; i++)
- for (j = 0; j < this.height; j++)
- this.content[i][j] = 0;
- };
- Grid.prototype.draw = function (ctx) {
- var i;
- var j;
- for (i = 0; i < this.width; i++)
- for (j = 0; j < this.height; j++) {
- ctx.fillStyle = "rgb(" + this.content[i][j] + ",0,0)";
- ctx.fillRect(i * GRID_STEP, j * GRID_STEP, GRID_STEP, GRID_STEP);
- }
- };
- Grid.prototype.adjacent = function (i, j) {
- var res = [];
- if (i > 0)
- res.push([i - 1, j]);
- if (i < this.width - 1)
- res.push([i + 1, j]);
- if (j > 0)
- res.push([i, j - 1]);
- if (j < this.height - 1)
- res.push([i, j + 1]);
- return res;
- };
- Grid.prototype.get = function (p) {
- return this.content[p[0]][p[1]];
- };
- Grid.prototype.set = function (p, n) {
- this.content[p[0]][p[1]] = n;
- };
- Grid.prototype.processCell = function (i, j) {
- var adj = this.adjacent(i, j);
- var min = 255;
- var p;
- for (p = 0; p < adj.length; p++) {
- if (this.get(adj[p]) < min)
- min = this.get(adj[p]);
- }
- var adjMin = adj.filter(function (p) { return this.get(p) == min; }, this);
- if (adjMin != [] && this.content[i][j] > 0 && min < 255) {
- var delta = Math.floor((this.content[i][j] - min) / 2);
- var r = shuffle(adjMin)[0];
- this.set(r, this.get(r) + delta);
- this.content[i][j] -= delta;
- }
- };
- Grid.prototype.dissipate = function () {
- var i;
- var j;
- if (this.traversal == 0) {
- for (i = this.width - 1; i >= 0; i--)
- for (j = this.height - 1; j >= 0; j--)
- this.processCell(i, j);
- }
- else if (this.traversal == 1) {
- for (i = 0; i < this.width; i++)
- for (j = 0; j < this.height; j++)
- this.processCell(i, j);
- }
- else if (this.traversal == 2) {
- for (i = 0; i < this.width; i++)
- for (j = this.height - 1; j >= 0; j--)
- this.processCell(i, j);
- }
- else {
- for (i = this.width - 1; i >= 0; i--)
- for (j = 0; j < this.height; j++)
- this.processCell(i, j);
- }
- this.traversal = (this.traversal + 1) % 4;
- };
- return Grid;
- }());
- var GRID_STEP = 10;
- var w = Math.floor(window.innerWidth / (GRID_STEP * 2));
- var h = Math.floor(window.innerHeight / (GRID_STEP + 1));
- function canvasInit() {
- var canvas = document.getElementById("canvas");
- canvas.width = w * GRID_STEP;
- canvas.height = h * GRID_STEP;
- return canvas;
- }
- var canvas = canvasInit();
- var ctx = canvas.getContext("2d");
- var grid = new Grid(w, h);
- var mouseDown = false;
- function onMouseMove(e) {
- if (mouseDown) {
- var x = e.pageX - canvas.offsetLeft;
- var y = e.pageY - canvas.offsetTop;
- var i = Math.floor(x / GRID_STEP);
- var j = Math.floor(y / GRID_STEP);
- grid.content[i][j] = 255;
- grid.draw(ctx);
- }
- }
- function onMouseDown(e) {
- mouseDown = true;
- }
- function onMouseUp(e) {
- mouseDown = false;
- }
- function onKeyDown(e) {
- if (e.keyCode == 32) {
- grid.dissipate();
- grid.draw(ctx);
- }
- }
- grid.draw(ctx);
- canvas.addEventListener("mousedown", onMouseDown);
- canvas.addEventListener("mouseup", onMouseUp);
- canvas.addEventListener("mousemove", onMouseMove);
- window.onkeydown = onKeyDown;
- <html>
- <head>
- <title>code</title>
- </head>
- <body>
- <p align=center>
- <canvas id=canvas></canvas>
- </p>
- <style type="text/css">
- canvas { display:block; }
- </style>
- <script type="text/javascript" src="main.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement