今日の課題:Canvasのクリッピング
<!DOCTYPE html>
<html>
<head>
<title>HTML5の練習@ヒッキープログラミングスレ</title>
<style type="text/css">
body { background-color: silver; }
canvas { background-color: white; }
</style>
</head>
<body>
<h4>クリッピング</h4>
<canvas id="mycanvas" width="640" height="480"></canvas>
<script language="javascript" type="text/javascript">
function mydraw() {
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, 150, 150);
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(200, 100);
ctx.lineTo(150, 200);
ctx.closePath();
ctx.clip();
ctx.beginPath();
ctx.fillStyle = 'red';
ctx.rect(110, 100, 50, 100);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = 'blue';
ctx.fillRect(400, 100, 100, 100); // クリップ領域外なので描かれない
// クリッピングの解除方法わからない件
}
mydraw();
</script>
</body>
</html>
- stroke()やfill()と同じ感じでclip() を使う。
- clip()を使うとクリップした領域にしか絵を描けなくなる。
最終更新:2016年05月18日 01:06