télécharger ou tester sur place (bouton précédent
du navigateur pour revenir)
<html>
<head>
<title>Canvas test</title>
</head>
<body>
<canvas id="canvas-cible" width="256" height="256" ></canvas>
<script type="text/javascript">
var testCanvas = document.createElement('canvas');
if (testCanvas.getContext === undefined) {
document.writeln('Browser too old!.');
} else {
var canvas = document.getElementById("canvas-cible");
var context = canvas.getContext("2d");
var imgData = context.createImageData(canvas.width, canvas.height);
var pixels = imgData.data;
for (var i = 0; i < pixels.length; i += 4) {
pixels[i ] = (i/4) % 256; // rouge
pixels[i+1] = Math.floor((i/4)/256); // vert
pixels[i+2] = Math.floor((Math.random()*256)+1); // bleu
pixels[i+3] = 255; // canal alpha (transparence)
}
context.putImageData(imgData, 0, 0);
}
</script>
</body>
</html>