Skip to content

Instantly share code, notes, and snippets.

@vim13
Created September 15, 2013 18:46
Show Gist options
  • Save vim13/6573347 to your computer and use it in GitHub Desktop.
Save vim13/6573347 to your computer and use it in GitHub Desktop.
チカチカ
var SCREEN_SIZE = 500; //x
var SIDE_CELLS = 200;
var CELL_SIZE = SCREEN_SIZE / SIDE_CELLS;
var FPS = 50;
var canvas;
var context;
window.onload = function() {
var field = new Array(SIDE_CELLS*SIDE_CELLS);
canvas = document.getElementById('noise');
canvas.width = canvas.height = SCREEN_SIZE;
var scaleRate = Math.min(window.innerWidth/SCREEN_SIZE, window.innerWidth/SCREEN_SIZE);
canvas.style.height = canvas.style.width = SCREEN_SIZE*scaleRate+'px';
context = canvas.getContext('2d');
context.fillStyle = 'rgb(256,256,256)';
update(field);
}
function update(field) {
for (var s=0; s<field.length; s++) field[s] = Math.floor(Math.random()*2);
drow(field);
setTimeout(update, 1000/FPS, field);
}
function drow(field) {
context.clearRect(0, 0, SCREEN_SIZE, SCREEN_SIZE);
for (var i=0; i<field.length; i++) {
if (field[i]) context.fillRect((i%SIDE_CELLS)*CELL_SIZE, Math.floor((i/SIDE_CELLS))*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment