What I've been trying to do is to assign pixels colours based on their coordinates. I've done something but I'm quite new to this and I'm not sure why it doesn't work.
var canvas = document.getElementById("plane");
var context = canvas.getContext("2d");
// parameters of the plance - should be editable
var width = canvas.width;
var height = canvas.heigth;
var x;
var y;
var color;
var imagedata = context.createImageData(width, height);
function createImage(offset)
{
for (x = 0; x < width; x += 1)
{
for (y = 0; y < height; y += 1)
{
if (x < width / 3)
color = {r: 256, g: 0, b: 0};
else if (x < width / 2)
color = {r: 0, g: 256, b: 0};
else if (x < width)
color = {r: 0, g: 0, b: 256};
var pixelindex = (y * width + x) * 4;
imagedata.data[pixelindex] = color.r;
imagedata.data[pixelindex + 1] = color.g;
imagedata.data[pixelindex + 2] = color.b;
imagedata.data[pixelindex + 3] = 255;
}
}
}
context.putImageData(imagedata, 0, 0);
I created the HTML separately.
<html>
<head>
<meta charset="utf-8">
<title>
Something
</title>
<script type="text/javascript" src="something.js"></script>
</head>
<body>
<canvas id="plane" width="640" height="480"></canvas>
</body>
</html>
Thank you for your help.