<canvas id="canvas" width="400" height="200">
お使いのブラウザはCanvasに対応していません。
</canvas>
<script src="script.js"></script>
canvas
タグ内には非対応ブラウザ用の説明を書く。
window.onload = function() {
draw();
};
function draw() {
var canvas = document.getElementById('canvas');
if (!canvas || !canvas.getContext) return false;
var ctx = canvas.getContext('2d');
// 描画処理
}
ctx.strokeRect(x, y, width, height);
ctx.strokeRect(10, 10, 50, 50);
ctx.fillRect(x, y, width, height);
ctx.fillRect(10, 10, 50, 50);
デフォルトの色は黒。
ctx.clearRect(x, y, width, height);
ctx.clearRect(10, 10, 50, 50);
ctx.strokeStyle = 色;
ctx.strokeStyle = "red";
ctx.strokeStyle = "#ff0000";
ctx.strokeStyle = "rgb(255, 0, 0)";
ctx.strokeStyle = "rgba(255, 0, 0, 0.5)";
ctx.fillStyle = 色;
ctx.fillStyle = "red";
ctx.fillStyle = "#ff0000";
ctx.fillStyle = "rgb(255, 0, 0)";
ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
ctx.lineWidth = ピクセル;
ctx.lineWidth = 5;
ctx.lineJoin = スタイル;
ctx.lineJoin = "round" // 角丸
ctx.lineJoin = "bevel" // 八角形(角を切る)
ctx.lineJoin = "miter" // 通常(デフォルト)
var g = ctx.createLinearGradient(始点x, 始点y, 終点x, 終点y);
var g = ctx.createLinearGradient(0, 0, 0, 100); // 縦方向
var g = ctx.createLinearGradient(0, 0, 100, 100); // 斜め方向
var g = ctx.createRadialGradient(始点x, 始点y, 始点半径, 終点x, 終点y, 終点半径);
var g = ctx.createRadialGradient(50, 50, 20, 50, 50, 60);
g.addColorStop(オフセット, 色);
g.addColorStop(0.0, "red");
g.addColorStop(0.5, "yellow");
g.addColorStop(1.0, "blue");
ctx.fillStyle = g;
ctx.fillStyle = g;
ctx.shadowColor = "#ccc" // 影の色(指定方法は何でも良い)
ctx.shadowOffsetX = 5; // 図形と影のずれ(x)
ctx.shadowOffsetY = 5; // 図形と影のずれ(y)
ctx.shadowBlur = 2; // ぼかし具合
ctx.globalAlpha = 透明度;
ctx.globalAlpha = 0.5;
描画後ではなく、描画前に図形を変形させる。
ctx.scale(x, y);
x, y
は倍率を指定
ctx.scale(0.8, 0.8); // 80%に縮小
ctx.rotate(度 / 180 * Math.PI);
ctx.rotate(30 / 180 * Math.PI); // 30度時計回りに回転
回転の起点は四角形の左上となっている。
ctx.translate(x, y);
ctx.translate(100, 10);
ctx.beignPath();
ctx.moveTo(x, y);
ctx.moveTo(20, 20);
ctx.lineTo(x, y);
ctx.lineTo(120, 20); // 20, 20から120, 20まで線を引く
ctx.lineTo(120, 120); // 120, 20から120, 120まで線を引く
ctx.stroke();
ctx.closePath();
ctx.fill();
ctx.arc(x, y, 半径, 開始角, 終了角);
ctx.beginPath();
ctx.arc(100, 100, 50, 0 / 180 * Math.PI, 360 / 180 * Math.PI); // 通常の円
ctx.stroke();
ctx.beginPath();
ctx.arc(100, 100, 50, 10 / 180 * Math.PI, 210 / 180 * Math.PI); // 欠けた円
ctx.stroke();
0度は円の右。塗りつぶす場合にはctx.fill();
を使う。その場合扇型にはならない。
ctx.lineCap = スタイル;
ctx.lineCap = 'butt'; // はみ出さない(デフォルト)
ctx.lineCap = 'round'; // 丸くなる(太い線でないとわからない)
ctx.lineCap = 'square'; // はみ出す
var img = new Image();
img.src = 'img.jpg';
img.onload = function() {
ctx.drawImage(img, 10, 10);
}
画像の変形もできる。
img.onload = function() { // 画像が読み込まれた時実行
var pattern = ctx.createPattern(img, 'repeat'); // no-repeat, repeat-x, repeat-yが指定可能
ctx.fillStyle = pattern; // パターンを適用
ctx.fillRect(20, 20, 100, 100); // 四角形を描画
}
ctx.save();
ctx.restore();
window.onload = function() {
draw();
};
function draw() {
var canvas = document.getElementById('canvas');
if (!canvas || !canvas.getContext) return false;
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
var y = 0;
(function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (y > canvas.height) y = -50;
y++;
ctx.fillRect(0, y, 50, 50);
setTimeout(loop, 10);
})();
}
window.onload = function() {
draw();
};
function draw() {
var canvas = document.getElementById('canvas');
if (!canvas || !canvas.getContext) return false;
var ctx = canvas.getContext('2d');
ctx.globalAlpha = 0.5;
for (var i = 0; i < 100; i++) {
var x = Math.floor(Math.random() * 400);
var y = Math.floor(Math.random() * 200);
var r = Math.floor(Math.random() * 200);
ctx.fillStyle = "rgb(" + rgb() + "," + rgb() + "," + rgb() + ")";
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill();
}
function rgb() {
return Math.floor(Math.random() * 255);
}
}
© 2019 shge.github.io 利用規約・プライバシー