"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
Your browser doesn't support canvas.

A
s an experiment I decided to create a couple of gears that would appear as an overlay on a web page.

My thinking was that I might use HTML5 to create UI controls that could be incorporated into a web form as slider bars, dials or charts, where there was a need for functionality that the basic HTML controls couldn’t provide.

I ran into a few issues:

First, I wanted the gear to have the appearance of rotating. I did this by using the translate function to set the origin to the center axis of the gear. The rotate function could then be used to rotate the canvas around that origin. 2*Math.PI is a complete circle. I was using a hidden canvas as a buffer to draw the gear, then drawing the completed gear on the visible canvas using drawImage. The axis was causing me issues, and I had some trouble getting the transparency to work correctly, so I decided to add another hidden buffer canvas. From my reading it seems like there is an issue with the alpha byte in canvas that controls transparency and some html5 functions (IE: clearRect), so I decided to use get getImageData and putImageData functions to copy the gear image from the first buffer canvas to the second. I used this copyImage function to do so.

Note that the copyImage function accepts a transparentColor value. If present, this value determines the color in the copied image that will become transparent.

Finally, I use the drawImage function to copy the image from the buffer to the final canvas. I use drawImage here rather than copyImage as copyImage considers the entire canvas when determining transparency. IE: the transparent part of an image will show the background behind the canvas, regardless of whether graphics were previously written to that part of the canvas.

    
    var transparentColor = {
       r : 255,
       g : 255,
       b : 255
    }

    function copyImage(
            srcCtx,
            srcX,
            srcY,
            srcWidth,
            srcHeight,
            destCtx,
            destX,
            destY,
            transparentColor)
        {
            // read pixels from source
            var pixels = srcCtx.getImageData(srcX, srcY, srcWidth, srcHeight);

            // iterate through pixel data (1 pixels consists of 4 ints in the array)
            for(var i = 0, len = pixels.data.length; i < len; i += 4)
            {
                var r = pixels.data[i];
                var g = pixels.data[i+1];
                var b = pixels.data[i+2];

                // if the pixel matches our transparent color, set alpha to 0
                if(transparentColor!=undefined)
                    if(r == transparentColor.r && g == transparentColor.g && b == transparentColor.b)
                    {
                        pixels.data[i+3] = 0;
                    }
            }

            // write pixel data to destination context
            destCtx.putImageData(pixels,destX,destY);
        }
    
    
    
}