8 Queens Problem
Abhivadaye
Anagram Finder
Bouncing Spheres
Break Out
Classic Snake
Cycloids
Deflection Demo
Double Pendulum
EV Savings
Flocking
Fog fly through
Forces on Objects
Fractals
Game of Life
Horizontal Stars
Image Scanning
JSON Beautify
Julia Sets
Kaleidoscope
Kock Fractals
Lorenz Attractor
Mandlebrot Set
Meta Balls
Natural Flocking
Number Convert
Number Game
Pandemic Simulator
Particles & Nodes
Perlin Noise
Poisson Disk
QuadTree Search
Ripples
Set Demonstration
Sierpiński Triangles
Simple Pendulum
Sine Waves
Starfield
Super Shapes 2D
Target Finder
Tic Tac Toe
Voronoi Diagram
Who Moved My...
Kaleidoscope
01 Jan 2007
This is a kaleidoscope implemented in canvas.

Patterns can be created in HTML5 canvas using context object’s function “createPattern”. Patterns can be created with images. Fortunately for us, HTML5 treats Canvas objects itself as images. With this in mind I thought it would be easy to implement an animated kaleidoscope.
//Pattern using an image
var pattern = aContext.createPattern(anImage, 'repeat');

//Pattern using another canvas.
var pattern = aContext.createPattern(orAnotherCanvas, 'repeat');
The steps to do that that is very simple. We simply take an off-screen canvas on which we perform an animation. Create a pattern made up of the animation function and use it to fill a pie shape. This pie shape can then be rotated around a central axis to give an illusion of a kaleidoscope. Like so.


While this worked wonderfully and was very easy to implement, I found out that the createPattern function eats up a lot of memory and the longer the kaleidoscope runs, the slower the animation gets. And I am talking about slowness on an 8GB, Intel i7 processor. That is bad.

This was obviously abandoned.

The below implementation is a much simpler one. One that does not use createPattern and thus is not processor or memory intensive. In this solution I have taken an animating canvas and rotate it 45 degrees each time. Here is what one slice looks like.



With two slices it becomes clear where we are going with this.



We can do this until the penultimate slice, because the last slice is going to overlap the first one and thus will ruin the illusion.

For the last slice alone, I create another canvas that is clipped to the exact dimensions that is required to fill the gap.


The final effect is very nice. But I’ll let you be the judge.
Select from one of the below animating patterns.

Sorry, your browser does not support Canvas.
Click here to see/hide the off-screen canvas.

1. kaleidoscope.js - Download, index.html
//Code goes here