HTML5 Canvas HTML Canvas
John Samuel
CPE Lyon
Année: 2017-2018
Courriel: john(dot)samuel(at)cpe(dot)fr
<body>...
<canvas id="moncanvas" width="300" height="400">
</canvas>
...
</body>
<canvas id="moncanvas" width="300" height="400">
</canvas>
<body>...
<canvas id="moncanvas" width="300" height="400">
</canvas>
<script>
var id = document.getElementById("moncanvas");
var context = id.getContext("2d");
</script>...
</body>
<body>...
<canvas id="ligne" width="300" height="400">
</canvas>
<script>
var id = document.getElementById("ligne");
var context = id.getContext("2d");
context.strokeStyle = "#00b33c";
context.lineWidth = 10;
context.moveTo(0,0);
context.lineTo(300,400);
context.stroke();
</script>...
</body>
<body>...
<canvas id="ligne" width="300" height="400">
</canvas>
<script>
var id = document.getElementById("ligne");
var context = id.getContext("2d");
context.strokeStyle = "#00b33c";
context.lineWidth = 10;
context.moveTo(300,0);
context.lineTo(0,400);
context.stroke();
</script>...
</body>
context.beginPath();
context.strokeStyle = "#00b33c";
context.moveTo(0,0);
context.lineTo(300,400);
context.stroke();
context.beginPath();
context.strokeStyle = "#00b33c";
context.moveTo(0,0);
context.lineTo(300,400);
context.strokeStyle = "red";
context.moveTo(0,400);
context.lineTo(300,0);
context.stroke();
context.beginPath();
context.strokeStyle = "#00b33c";
context.moveTo(0,0);
context.lineTo(300,400);
context.stroke();
context.beginPath();
context.strokeStyle = "red";
context.moveTo(0,400);
context.lineTo(300,0);
context.stroke();
context.beginPath();
context.arc(200, 150, 100, Math.PI, 1.5 * Math.PI);
context.stroke();
context.beginPath();
context.arc(200, 150, 100, Math.PI, 1.5 * Math.PI, true);
context.stroke();
context.beginPath();
context.arc(x, y, rayon, angleInitial, angleFinal, antihoraire);
context.stroke();
context.beginPath();
context.moveTo(0,70);
context.lineTo(140,70);
context.arcTo(200, 70, 200, 270, 50);
context.lineTo(200, 270);
context.stroke();
context.beginPath();
context.fillStyle = "#00b33c";
context.moveTo(0,70);
context.lineTo(140,70);
context.arcTo(200, 70, 200, 270, 50);
context.lineTo(200, 270);
context.fill();
context.beginPath();
context.arcTo(x1, y1, x2, y2, rayon);
context.stroke();
context.beginPath();
quadraticCurveTo(cp1x, cp1y, x, y);
context.stroke();
context.beginPath();
context.moveTo(0,70);
context.lineTo(120,70);
context.quadraticCurveTo(280, 120, 200, 270);
context.stroke();
context.beginPath();
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
context.stroke();
context.beginPath();
context.moveTo(0,70);
context.lineTo(120,70);
context.bezierCurveTo(280, 120, 240, 220, 150, 270);
context.stroke();
context.beginPath();
context.arc(150, 150, 100, 0, 2 * Math.PI);
context.stroke();
context.lineWidth = 10;
context.rect(10, 10, 280, 280);
context.stroke();
context.strokeStyle = "#00b33c";
context.rect(x, y, largeur, hauteur);
context.stroke();
context.strokeStyle = "#00b33c";
context.strokeRect(x, y, largeur, hauteur);
context.fillStyle = "#00b33c";
context.fillRect(x, y, largeur, hauteur);
context.fillStyle = "#00b33c";
context.fillRect(10, 10, 280, 280);
context.beginPath();
context.strokeStyle = "#00b33c";
context.moveTo(10, 10);
context.lineTo(150, 380);
context.lineTo(280, 10);
context.closePath();
context.stroke();
context.beginPath();
context.fillStyle = "#00b33c";
context.moveTo(10, 10);
context.lineTo(150, 380);
context.lineTo(280, 10);
context.closePath();
context.fill();
context.beginPath();
context.strokeStyle = "#00b33c";
context.moveTo(10, 200);
context.lineTo(80, 10);
context.lineTo(200, 10);
context.lineTo(270, 200);
context.lineTo(200, 380);
context.lineTo(80, 380);
context.closePath();
context.stroke();
context.beginPath();
context.fillStyle = "#00b33c";
context.moveTo(10, 200);
context.lineTo(80, 10);
context.lineTo(200, 10);
context.lineTo(270, 200);
context.lineTo(200, 380);
context.lineTo(80, 380);
context.lineTo(80, 380);
context.closePath();
context.fill();
context.clearRect(90, 140, 100, 100);
context.font = "40px Arial";
context.strokeText("Bonjour!", 100, 100);
context.font = "40px Arial";
context.fillText("Bonjour!", 100, 100);
context.font = "40px Arial";
context.strokeText("Bonjour!", 100, 100);
context.font = "30px Arial";
context.textAlign = "start";
context.fillText("début", 250, 20);
context.textAlign = "end";
context.fillText("fin1 fin2 fin3 fin4 fin5 fin6", 250, 100);
context.textAlign = "left";
context.fillText("gauche1 gauche2 gauche3 gauche4", 250, 180);
context.textAlign = "right";
context.fillText("droite1 droite2 droite3 droite4", 250, 260);
context.textAlign = "center";
context.fillText("centre", 250, 340);
context.font = "20px Arial";
context.textBaseline = "top";
context.fillText("haut", 0, 200);
context.textBaseline = "middle";
context.fillText("moyen", 150, 200);
context.textBaseline = "alphabetic";
context.fillText("alphabetique", 250, 200);
context.textBaseline = "bottom";
context.fillText("en bas", 420, 200);
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.drawImage(img, 0, 0);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.drawImage(img, 0, 0, 500, 400);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.drawImage(img, 0, 0, 250, 200);
context.drawImage(img, 250, 0, 250, 200);
context.drawImage(img, 0, 200, 250, 200);
context.drawImage(img, 250, 200, 250, 200);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.drawImage(img, 100, 100, 500, 400, 250, 250, 150, 150);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.rotate(10*Math.PI/180);
context.drawImage(img, 200, 0, 200, 200);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.scale(0.5, 0.5);
context.drawImage(img, 200, 0, 200, 200);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.drawImage(img, 0, 0, 200, 200);
context.translate(200, 200);
context.drawImage(img, 0, 0, 400, 400);
}
var img = new Image();
img.src = "img.png";
img.onload = function() {
context.drawImage(img, 0, 0, 200, 200);
context.transform(0.5, 0.5, -0.5, 0.5, 300,10);
context.drawImage(img, 0, 0, 400, 400);
}
document.onkeydown = function(event) {
event.preventDefault();
event = event || window.event;
if (event.keyCode == '37') {
// gauche
}
else if (event.keyCode == '38') {
// haut
}
else if (event.keyCode == '39') {
// droite
}
else if (event.keyCode == '40') {
// bas
}
}
document.getElementById("moncanvas").onmousedown = function(event) {
event = event || window.event;
event.preventDefault();
var posX = event.pageX - canvas.offsetLeft;
var posY = event.pageY - canvas.offsetTop;
}
document.body.onmouseup = function(event) {
event = event || window.event;
event.preventDefault();
}