An adjacent cell is colored each iteration, thus The Mighty PixelWorm leaves its trail of rainbow poo.
Moved from arrays to objects.
click on canvas - add worm;
double click on canvas - clear;
var canvas = document.getElementById('canvas');
var c = canvas.getContext("2d");
c.fillStyle = "#000000";
c.fillRect(0, 0, canvas.width, canvas.height);
canvas.addEventListener('click', startWorm, false);
canvas.addEventListener('dblclick', clear, false);
///// CONTROLS ///////////////////////////////////////////////
/** //
/** total time for a full cell color transition */
var interval = 100; //
/** number of columns/rows */
var gridSize = 30; //
/** side of a cell in pixels */
var cellSize = canvas.width / gridSize; //
/** number of iterations for a full cell color transition */
var iterations = 10; //
/** 0-255; ~90 and below keeps the adjacent colors relative */
var colorRange = 80; //
/** max number of active worms */
var maxWorms = 10; //
var worms = [];
pushWorm([0, 0, 0], getRandomInt(gridSize), getRandomInt(gridSize));
/** worm object */
function Worm(pixel, x, y) {
this.index = iterations;
this.pixel = [];
this.pixel[0] = pixel[0];
this.pixel[1] = pixel[1];
this.pixel[2] = pixel[2];
this.pixelStep = [0, 0, 0];
this.x = x;
this.y = y;
this.start = function () {
//TODO: there must be a more civilized way to achieve this...
this.timer = setInterval(redirect, (interval / iterations), this);
function redirect(w) {
w.draw();
}
};
this.stop = function () {
clearInterval(this.timer);
};
/** paint next iteration of color to the current cell */
this.draw = function () {
if (this.index == iterations) {
this.colorize();
this.index = 0;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.