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');

A pattern is created out of this and this pattern is applied to a pie shape.

This shape is then rotated around its axis and this will form a kaleidoscope.
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.
Javascript Demos ▼
Here's a bunch of javascript... things I wrote over the years whenever time permitted.
Abhivadaye Generator
Game of Life
Anagram Finder
Deflection Demo
Break Out
Cycloids
Double Pendulum
Flocking
Fog fly through
Fractals
EV Savings
Target Finder
Bouncing Spheres
Horizontal Stars
Image Scanning
Pandemic Simulator
JSON Beautify
Julia Sets
Kaleidoscope
Kock Fractals
Lorenz Attractor
Mandlebrot Set
Meta Balls
Number Convert
Number Game
Forces on Objects
Particles & Nodes
Simple Pendulum
Perlin Noise
Poisson Disk
QuadTree Search
8 Queens Problem
Natural Flocking
Ripples
Set Demonstration
Sine Waves
Classic Snake
Starfield
Sierpinski Triangles
Super Shapes 2D
Tic Tac Toe
Voronoi Diagram
Who Moved My...
