Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nitaku
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitaku/1877405856af3a1c0a37 to your computer and use it in GitHub Desktop.
Save nitaku/1877405856af3a1c0a37 to your computer and use it in GitHub Desktop.
Canvas gradient
`// noprotect`
canvas = d3.select('canvas')
width = canvas.node().getBoundingClientRect().width
height = canvas.node().getBoundingClientRect().height
ctx = canvas.node().getContext('2d')
image = ctx.createImageData(width, height)
for x in [0...width]
for y in [0...height]
i = (y*width+x)*4
[r,g,b,a] = [i+0,i+1,i+2,i+3]
image.data[r] = x/width*255
image.data[g] = 0
image.data[b] = y/height*255
image.data[a] = 255
ctx.putImageData(image,0,0)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="Canvas gradient" />
<title>Canvas gradient</title>
<link rel="stylesheet" href="index.css">
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<canvas width="960" height="500"></canvas>
<script src="index.js"></script>
</body>
</html>
(function() {
// noprotect;
var a, b, canvas, ctx, g, height, i, image, r, width, x, y, _i, _j, _ref;
canvas = d3.select('canvas');
width = canvas.node().getBoundingClientRect().width;
height = canvas.node().getBoundingClientRect().height;
ctx = canvas.node().getContext('2d');
image = ctx.createImageData(width, height);
for (x = _i = 0; 0 <= width ? _i < width : _i > width; x = 0 <= width ? ++_i : --_i) {
for (y = _j = 0; 0 <= height ? _j < height : _j > height; y = 0 <= height ? ++_j : --_j) {
i = (y * width + x) * 4;
_ref = [i + 0, i + 1, i + 2, i + 3], r = _ref[0], g = _ref[1], b = _ref[2], a = _ref[3];
image.data[r] = x / width * 255;
image.data[g] = 0;
image.data[b] = y / height * 255;
image.data[a] = 255;
}
}
ctx.putImageData(image, 0, 0);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment