Created
January 27, 2016 16:02
-
-
Save nickgartmann/1b65ea8d734bb6e1c604 to your computer and use it in GitHub Desktop.
Function which draws the image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function draw(width, height) { | |
canvas = $element.find("canvas"); | |
canvas.attr("width", width); | |
canvas.attr("height", height); | |
// Browser API which allows us to manipulate canvas pixels | |
context = canvas[0].getContext("2d"); | |
image_data = context.createImageData(width, height); | |
data = image_data.data | |
// Keep track of the actual position in the bitmap | |
var i = 0; | |
for(var y = 0; y < height; ++y) { | |
for(var x = 0; x < width; ++x) { | |
// Calculate rgb value based on % height and width | |
var rgb = xy2rgb(x/width, y/height); | |
data[i + 0] = rgb[0]; // red | |
data[i + 1] = rgb[1]; // green | |
data[i + 2] = rgb[2]; // blue | |
data[i + 3] = 255; // opacity | |
i += 4; | |
} | |
} | |
context.putImageData(image_data, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment