Aide-mémoire pour la balise Canvas
Définition par:
<canvas id="c1" width="500" height="375">
Balise Canvas non reconnue
</canvas>
Appel du contexte par les deux lignes:
var canvas = document.getElementById('c1');
var ctx = canvas.getContext('2d');
C'est cet objet ctx dont on appelera propriétés et méthodes.
ctx.beginPath(); ctx.lineWidth="5"; ctx.strokeStyle="green"; // Green path ctx.moveTo(0,75); ctx.lineTo(250,75); ctx.stroke(); // Draw it ctx.beginPath(); ctx.strokeStyle="purple"; // Purple path ctx.moveTo(50,0); ctx.lineTo(150,130); ctx.stroke(); // Draw it
canvas2 = document.createElement('canvas');
var container = canvas1.parentNode;
canvas2.id = 'canvas2';
canvas2.width = canvas1.width;
canvas2.height = canvas1.height;
container.appendChild(canvas2);
ctx2 = canvas2.getContext('2d');
function img_update () {
ctx1.drawImage(canvas2, 0, 0);
ctx2.clearRect(0, 0, canvas2.width, canvas2.height);
}
Ne pas oublier de préciser:
<style>
#container { position: relative; }
#canvas1 { border: 1px solid #000; }
#canvas2 { position: absolute; top: 1px; left: 1px; }
</style>
// Store the current transformation matrix ctx.save(); // Use the identity matrix while clearing the canvas ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0,0,monCanvas.width,monCanvas.height); ctx.beginPath(); //évite traits incontrôlés // Restore the transform ctx.restore();