1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<title>Couper un canevas en trois</title>
<style>
body {
background: #DDD;
}
canvas {
border: solid thin #AAA;
background: white;
}
</style>
</head>
<body>
<p>
<canvas id="cvs-source" width="400" height="750">Bleh</canvas>
</p>
<p>
<canvas id="cvs-dest-1" width="400" height="250">Blih</canvas>
<canvas id="cvs-dest-2" width="400" height="250">Blah</canvas>
<canvas id="cvs-dest-3" width="400" height="250">Bluh</canvas>
</p>
<script> "use strict";
const $source = document.getElementById("cvs-source");
const $dest1 = document.getElementById("cvs-dest-1");
const $dest2 = document.getElementById("cvs-dest-2");
const $dest3 = document.getElementById("cvs-dest-3");
// dessin dans le canevas source
const cx = $source.getContext("2d");
cx.fillStyle = "#FC5";
cx.fillRect(10, 10, 200, 300);
cx.fillStyle = "#CF6";
cx.fillRect(100, 225, 200, 300);
cx.fillStyle = "#8CF";
cx.fillRect(190, 440, 200, 300);
// copie dans les 3 canevas séparés
const cx1 = $dest1.getContext("2d");
cx1.drawImage($source, 0, 0);
const cx2 = $dest2.getContext("2d");
cx2.drawImage($source, 0, 250, 400, 250, 0, 0, 400, 250);
const cx3 = $dest3.getContext("2d");
cx3.drawImage($source, 0, 500, 400, 250, 0, 0, 400, 250);
</script>
</body>
</html> |
Partager