ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
Canvas API とは
HTML5 で導入された <canvas> 要素は、JavaScript からピクセル単位で描画できるビットマップキャンバスです。ゲーム、グラフ、画像処理、データビジュアライゼーションなど幅広く使われます。
最小サンプル: 四角形を描く
<!DOCTYPE html>
<html>
<body>
<canvas id="c" width="500" height="300" style="border:1px solid #ccc"></canvas>
<script>
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
// 塗りつぶし四角形
ctx.fillStyle = '#3498db';
ctx.fillRect(20, 20, 100, 60);
// 枠線のみの四角形
ctx.strokeStyle = '#e74c3c';
ctx.lineWidth = 3;
ctx.strokeRect(150, 20, 100, 60);
// クリア
// ctx.clearRect(0, 0, canvas.width, canvas.height);
</script>
</body>
</html>
width / height は CSS でなく属性で指定するのが鉄則。CSS で指定すると拡大表示になりピクセルがぼやけます。
基本図形の描画
// 直線
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(200, 100);
ctx.lineTo(50, 150);
ctx.closePath(); // パスを閉じる
ctx.stroke(); // 線として描画
// ctx.fill(); // 塗りつぶし
// 円 / 円弧
ctx.beginPath();
ctx.arc(300, 100, 50, 0, Math.PI * 2); // x, y, r, 開始角, 終了角(ラジアン)
ctx.fillStyle = '#2ecc71';
ctx.fill();
// 半円
ctx.beginPath();
ctx.arc(400, 100, 50, 0, Math.PI);
ctx.stroke();
// 二次ベジェ曲線
ctx.beginPath();
ctx.moveTo(50, 200);
ctx.quadraticCurveTo(150, 100, 250, 200); // 制御点, 終点
ctx.stroke();
テキスト描画
ctx.font = 'bold 32px sans-serif';
ctx.fillStyle = '#2c3e50';
ctx.textAlign = 'center'; // start | end | left | right | center
ctx.textBaseline = 'middle'; // top | hanging | middle | alphabetic | ideographic | bottom
ctx.fillText('Hello Canvas', 250, 50);
// 枠線のみ
ctx.strokeStyle = '#e74c3c';
ctx.lineWidth = 2;
ctx.strokeText('Outline', 250, 100);
// 文字幅を測る
const m = ctx.measureText('Hello');
console.log(m.width); // pixel
画像の描画
const img = new Image();
img.src = '/path/to/photo.jpg';
img.onload = () => {
// 原寸
ctx.drawImage(img, 50, 50);
// サイズ指定
ctx.drawImage(img, 50, 50, 200, 150);
// 切り抜き + 配置(sx, sy, sw, sh, dx, dy, dw, dh)
ctx.drawImage(img, 0, 0, 100, 100, 300, 50, 200, 200);
};
変形(回転・拡大・移動)
ctx.save(); // 状態を保存
ctx.translate(250, 150); // 原点を移動
ctx.rotate(Math.PI / 4); // 45 度回転
ctx.scale(1.5, 1.5); // 1.5 倍拡大
ctx.fillStyle = '#9b59b6';
ctx.fillRect(-50, -25, 100, 50); // 移動後の原点が中心
ctx.restore(); // 元の状態に戻す
アニメーション: requestAnimationFrame
毎フレーム描き直すには requestAnimationFrame を使います。setInterval ではブラウザの再描画タイミングと同期しないのでカクつきます。
let x = 0;
let vx = 2;
function tick() {
// 1. 画面クリア
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 2. 状態更新
x += vx;
if (x > canvas.width - 50 || x < 0) vx = -vx;
// 3. 描画
ctx.fillStyle = '#3498db';
ctx.fillRect(x, 100, 50, 50);
// 4. 次フレームを予約
requestAnimationFrame(tick);
}
tick();
マウス入力でお絵描き
let drawing = false;
canvas.addEventListener('mousedown', e => {
drawing = true;
ctx.beginPath();
ctx.moveTo(e.offsetX, e.offsetY);
});
canvas.addEventListener('mousemove', e => {
if (!drawing) return;
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
});
canvas.addEventListener('mouseup', () => drawing = false);
Canvas API でできることの限界
| 用途 | 素の Canvas | 推奨ライブラリ |
|---|---|---|
| 簡単な図形・グラフ | OK | Chart.js |
| 2D ゲーム(スプライト多数) | 遅い | PixiJS(WebGL 加速) |
| 3D 描画 | 不可(getContext("webgl") 必要) | Three.js / Babylon.js |
| SVG 的なベクタ | パスは描けるが座標管理が大変 | SVG + D3.js |
| 動画フィルタ | getImageData / putImageData で可能 | WebGL + シェーダ |
WebGL コンテキストへの拡張
// 2D → 3D の入り口
const gl = canvas.getContext('webgl2');
if (!gl) {
alert('WebGL2 がサポートされていません');
}
// 実際は Three.js を使うのが現実的
// npm install three
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, w/h, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ canvas });
const cube = new THREE.Mesh(
new THREE.BoxGeometry(),
new THREE.MeshNormalMaterial()
);
scene.add(cube);
camera.position.z = 3;
renderer.setAnimationLoop(() => {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
});
FAQ
Q: Canvas で描いた絵を画像保存したい
A: canvas.toDataURL('image/png') で base64 取得、canvas.toBlob(cb) で Blob 取得が可能です。
Q: 高 DPI(Retina)でぼやける
A: canvas.width = w * devicePixelRatio にして ctx.scale(devicePixelRatio, devicePixelRatio)、CSS 側は w px に。
Q: SVG とどちらが良い?
A: 拡大に強く要素単位でイベント取りたいなら SVG、ピクセル制御や多数オブジェクトのアニメは Canvas が高速です。
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
同階層のページ
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- IPv6とは|128bitアドレス・コロン16進表記/::省略・リンクローカル・SLAAC・デュアルスタック NEW 2026-06-22 12:34:44
- MAC アドレスフィルタリングの仕組みと限界 | ネットワーク入門 NEW 2026-06-22 12:19:10
- VPNとは|暗号トンネル・サイト間/リモートアクセス・IPsec/SSL-VPN/WireGuardを解説 NEW 2026-06-22 12:19:10
- WebRTC とは ブラウザ間 P2P の音声・映像・データ通信 | ネットワーク入門 NEW 2026-06-22 12:17:25
- HTTP/2 とは 多重化・HPACK・バイナリフレーム | ネットワーク入門 NEW 2026-06-22 12:17:25
- Web通信プロトコル入門 HTTP/2・HTTP/3・WebSocket・gRPC・WebRTC | ネットワーク入門 NEW 2026-06-22 12:17:25
- gRPC とは HTTP/2 + Protocol Buffers の高速 RPC | ネットワーク入門 NEW 2026-06-22 12:17:25
- HTTP/3 (QUIC) とは UDP ベースの低遅延 Web 通信 | ネットワーク入門 NEW 2026-06-22 12:17:25
- WebSocket とは 全二重リアルタイム通信 ws/wss | ネットワーク入門 NEW 2026-06-22 12:17:25
- 証明書と認証局(CA)とは|X.509・信頼チェーン・DV/OV/EV・失効(CRL/OCSP)を解説 NEW 2026-06-22 12:17:24
- ファイアウォールとは|パケットフィルタ・ステートフル・DMZ・次世代FW(L4/L7)を解説 NEW 2026-06-22 12:17:24
- iptables/nftablesとは|テーブル・チェーン・ルール例・永続化をLinux視点で解説 NEW 2026-06-22 12:17:24
- HAProxy とは frontend/backend と設定例 | ネットワーク入門 NEW 2026-06-22 12:17:24
- CDN とは エッジキャッシュ・TTL・Cloudflare/CloudFront | ネットワーク入門 NEW 2026-06-22 12:17:24
- TLS/SSLの仕組み|ハンドシェイク・暗号スイート・前方秘匿性・証明書検証をわかりやすく解説 NEW 2026-06-22 12:17:24
コメントを削除してもよろしいでしょうか?