113 lines
2.2 KiB
JavaScript
113 lines
2.2 KiB
JavaScript
const width = 8 ** 3;
|
|
const height = 8 ** 3;
|
|
|
|
function validXY(x, y) {
|
|
if (x < 0 || x >= width) return false;
|
|
if (y < 0 || y >= height) return false;
|
|
return true;
|
|
}
|
|
|
|
function getRGBA(data, i) {
|
|
let out = new Uint8Array(4);
|
|
for (let j = i * 4; j < i * 4 + 4; j++) {
|
|
out[j - i*4] = data[j];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function setRGBA(data, i, input) {
|
|
for (let j = i * 4; j < i * 4 + 4; j++) {
|
|
data[j] = input[j - i * 4];
|
|
}
|
|
}
|
|
|
|
function Game(inSize) {
|
|
this.img = new Image();
|
|
this.inSize = inSize;
|
|
this.base = new Float32Array(inSize * inSize);
|
|
}
|
|
|
|
Game.prototype.main = function() {
|
|
var {base, inSize, ctx} = this;
|
|
|
|
var dat = ctx.getImageData(0,0,width,height);
|
|
|
|
for (let i = 0; i < base.length; i++) {
|
|
base[i] = Math.max(base[i] * 0.99999,0);
|
|
}
|
|
|
|
let exp = Math.ceil(Math.log(width) - Math.log(inSize));
|
|
|
|
for (let i = 0; i < dat.data.length / 4; i++) {
|
|
let x = i % width;
|
|
let y = Math.floor(i / width);
|
|
|
|
let x2 = x;
|
|
let y2 = y;
|
|
|
|
let isMask = 1;
|
|
|
|
for (let j = 0; j < exp; j++) {
|
|
isMask *= base[(y2 % inSize) * inSize + (x2 % inSize)];
|
|
|
|
x2 /= inSize;
|
|
y2 /= inSize;
|
|
|
|
x2 = Math.floor(x2);
|
|
y2 = Math.floor(y2);
|
|
}
|
|
|
|
isMask = (isMask - 0.5) * 100 + 0.5;
|
|
isMask = Math.max(isMask,0);
|
|
isMask = Math.min(isMask,1);
|
|
isMask *= 255;
|
|
|
|
setRGBA(dat.data,i,[isMask,isMask,isMask,255])
|
|
}
|
|
|
|
ctx.putImageData(dat,0,0,0,0,width,height);
|
|
}
|
|
|
|
Game.prototype.map = function() {
|
|
var {base, inSize} = this;
|
|
|
|
//noise.seed(Math.random() * 1000);
|
|
|
|
for (let i in base) {
|
|
let x = i % inSize;
|
|
let y = Math.floor(i / inSize);
|
|
|
|
base[i] = Math.random();
|
|
|
|
base[i] = Math.max(base[i],0);
|
|
base[i] = Math.min(base[i],1);
|
|
base[i] = Math.pow(base[i],0.3);
|
|
base[i] = base[i] * 0.4 + 0.6;
|
|
//base[i] = (Math.random() > 0.5) ? 0 : 1;
|
|
}
|
|
}
|
|
|
|
Game.prototype.loop = function() {
|
|
let that = this;
|
|
setInterval(function() { that.main() } ,1000 / 60);
|
|
}
|
|
|
|
Game.prototype.startGame = function() {
|
|
var canvas = document.querySelector("#canvas");
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
|
|
var ctx = this.ctx = canvas.getContext("2d",{ willReadFrequently: true });
|
|
|
|
ctx.imageSmoothingEnabled = false;
|
|
|
|
this.map();
|
|
this.loop();
|
|
}
|
|
|
|
Game.prototype.init = function() {
|
|
let that = this;
|
|
that.startGame();
|
|
}
|
|
|
|
new Game(8).init();
|