コンテンツにスキップ

使えない席を指定する

「テーブル 1 の左前の椅子」のように場所の言葉で伝えると、その椅子が灰色に × で描き分けられることを表した図

前の節で描いた部屋に、使えない席を足します。

出入口の前、柱のそば、壊れている椅子。実際の部屋には、椅子はあっても人を座らせたくない場所があります。 座席表を配ってから「そこ、座れないんですけど」と言われないように、図の上で先に塞いでおきます。

前の節を終えていない人は、ここから始められます。

この節を始めるときのファイル

前の節の続きで、同じ Codex のやりとりに次の文を送ってください。

ブラウザを再読み込みします。

「PNG で保存」で保存した画像が、次のようになれば OK です。 色や線の太さ・文字の大きさは違っていて構いません。下の確認ポイントを満たしているかで判断してください。

テーブル 1 の左前・テーブル 3 の右前・テーブル 5 の後ろの椅子が灰色に × になり、図の下に凡例がある

  • テーブル 1 の左前、テーブル 3 の右前、テーブル 5 のいちばん後ろの椅子が灰色で × が付いている
  • ほかの 33 脚は白いまま
  • 図の下に凡例が出ている
  • 「PNG で保存」で保存した画像にも、灰色の椅子と凡例が入っている

使えない席も、言葉で足したり減らしたりできます。

場所を言葉で指すのは、けっこう大変

Section titled “場所を言葉で指すのは、けっこう大変”

場所で指すと「テーブル 5 の、いちばん後ろ側の椅子」と長くなるが、番号なら「17 番」で済むことを表した図

やってみて気づいたと思います。「テーブル 5 の、いちばん後ろ側の椅子」は、言うのも聞くのも長い。 時計回りに数えるのか、どこが「前」なのか。Codex が読み違えて、別の椅子を塞いでしまうこともあります。

人に伝えるときも同じです。「テーブル 4 の、いちばん前側の椅子に座ってください」より、 「17 番に座ってください」のほうがずっと楽です。

そこで次の節では、席に番号を振ります。

この節を終えたときの完成例のファイルです。前の節から増えたファイルに「作成」、変わったファイルに「変更」の印が付き、変わった行に色が付きます。Codex が作ったファイルと、書き方や細かいところが違っていて構いません。

seating-chart
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>座席表</title>
<link rel="stylesheet" href="style.css" />
<script src="main.js" defer></script>
</head>
<body>
<h1>座席表</h1>
<section class="panel">
<canvas id="room"></canvas>
<button id="save" type="button">PNG で保存</button>
</section>
</body>
</html>
// 座席表アプリ - ステップ 2: 使えない席を描き分ける
//
// ステップ 1 との違いは、UNAVAILABLE(使えない席の一覧)が増えたこと。
// 一覧に入っている椅子は灰色に塗って × を付け、図の下に凡例を出す。
// ---- 部屋とテーブル(単位は m) ----
// 部屋: 横 10m × 奥行き 8m。前(図の上側)の壁にホワイトボードがある
const ROOM_WIDTH = 10;
const ROOM_DEPTH = 8;
// テーブル: 直径 1.2m の丸テーブル。まわりに椅子を 6 脚、等間隔に置く
const TABLE_DIAMETER = 1.2;
const CHAIRS_PER_TABLE = 6;
// 並べ方: 横に 3 卓 × 前から 2 列。テーブルの中心どうしの間隔は、横 3.2m・縦 3m
const TABLE_COLUMNS = 3;
const TABLE_ROWS = 2;
const TABLE_GAP_X = 3.2;
const TABLE_GAP_Y = 3;
// 前の壁から、1 列目のテーブルの中心までの距離。ホワイトボードの前はあけておく
const FRONT_SPACE = 2.4;
// 椅子(=1 人分の席)の大きさと、テーブルの中心から椅子の中心までの距離
const SEAT_WIDTH = 0.95;
const SEAT_DEPTH = 0.4;
const SEAT_DISTANCE = 1.15;
// 使えない席。table はテーブルの番号、chair は椅子の番号(前側から時計回りに 1〜6)
const UNAVAILABLE = [
{ table: 1, chair: 6 }, // テーブル 1 の左前: 出入口の前
{ table: 3, chair: 2 }, // テーブル 3 の右前: 出入口の前
{ table: 5, chair: 4 }, // テーブル 5 のいちばん後ろ: 柱がある
];
// ---- 描き方 ----
const SCALE = 80; // 1m を何ピクセルで描くか
const MARGIN = 48; // 部屋のまわりの余白(px)。「前」の文字を置く場所
const LEGEND_SPACE = 32; // 図の下に凡例を書くためにあける高さ(px)
const PIXEL_RATIO = 2; // 保存する PNG を何倍の細かさで描くか。印刷してもぼやけないように 2 倍
const canvas = document.querySelector('#room');
const saveButton = document.querySelector('#save');
// テーブルと椅子の位置(部屋の左上の角からの距離, m)を、並べ方の数字から計算する。
// テーブルは前の列の左から順に 1, 2, 3…。椅子はテーブルの前側から時計回りに 1〜6。
function layoutTables() {
const tables = [];
// テーブルのかたまり全体を、部屋の左右の真ん中に置く
const left = ROOM_WIDTH / 2 - (TABLE_COLUMNS - 1) * TABLE_GAP_X / 2;
for (let row = 0; row < TABLE_ROWS; row++) {
for (let col = 0; col < TABLE_COLUMNS; col++) {
const table = {
number: tables.length + 1,
x: left + col * TABLE_GAP_X,
y: FRONT_SPACE + row * TABLE_GAP_Y,
seats: [],
};
for (let i = 0; i < CHAIRS_PER_TABLE; i++) {
// 真上(前側)から始めて、時計回りに等間隔。canvas は下向きが +y なので角度が増えると時計回り
const angle = -Math.PI / 2 + (i * 2 * Math.PI) / CHAIRS_PER_TABLE;
table.seats.push({
table: table.number,
chair: i + 1,
x: table.x + Math.cos(angle) * SEAT_DISTANCE,
y: table.y + Math.sin(angle) * SEAT_DISTANCE,
});
}
tables.push(table);
}
}
return tables;
}
// m で表した位置を、canvas 上のピクセルに直す
function toX(m) {
return MARGIN + m * SCALE;
}
function toY(m) {
return MARGIN + m * SCALE;
}
function draw() {
const width = ROOM_WIDTH * SCALE + MARGIN * 2;
const height = ROOM_DEPTH * SCALE + MARGIN * 2 + LEGEND_SPACE;
// canvas の中身は PIXEL_RATIO 倍の細かさで持ち、画面には元の大きさで見せる
canvas.width = width * PIXEL_RATIO;
canvas.height = height * PIXEL_RATIO;
canvas.style.width = width + 'px';
const ctx = canvas.getContext('2d');
ctx.scale(PIXEL_RATIO, PIXEL_RATIO);
// 背景を白で塗る。塗らないと PNG の背景が透明になり、開くソフトによっては真っ黒に見える
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, width, height);
// 部屋の壁
ctx.strokeStyle = '#334e68';
ctx.lineWidth = 3;
ctx.strokeRect(toX(0), toY(0), ROOM_WIDTH * SCALE, ROOM_DEPTH * SCALE);
// ホワイトボード(前の壁の真ん中、幅 3m)
ctx.fillStyle = '#334e68';
ctx.fillRect(toX(ROOM_WIDTH / 2 - 1.5), toY(0) + 4, 3 * SCALE, 6);
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.font = '12px sans-serif';
ctx.fillText('ホワイトボード', toX(ROOM_WIDTH / 2), toY(0) + 16);
// どちらが前かを部屋の外に書いておく
ctx.textBaseline = 'bottom';
ctx.font = 'bold 14px sans-serif';
ctx.fillText('前', toX(ROOM_WIDTH / 2), toY(0) - 10);
for (const table of layoutTables()) {
drawTable(ctx, table);
for (const seat of table.seats) {
drawSeat(ctx, toX(seat.x), toY(seat.y), isUnavailable(seat));
}
}
drawLegend(ctx, toY(ROOM_DEPTH) + 28);
}
function isUnavailable(seat) {
return UNAVAILABLE.some(function (u) {
return u.table === seat.table && u.chair === seat.chair;
});
}
// 丸テーブルを 1 卓描く。真ん中にテーブルの番号を書く
function drawTable(ctx, table) {
ctx.fillStyle = '#fdf6e3';
ctx.strokeStyle = '#52606d';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.arc(toX(table.x), toY(table.y), (TABLE_DIAMETER / 2) * SCALE, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
ctx.fillStyle = '#7b8794';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = '12px sans-serif';
ctx.fillText('テーブル ' + table.number, toX(table.x), toY(table.y));
}
// 椅子(席)を 1 脚描く。cx, cy は椅子の中心(px)。あとで番号や名前を書き込めるよう横長の札の形にする
function drawSeat(ctx, cx, cy, unavailable) {
const w = SEAT_WIDTH * SCALE;
const h = SEAT_DEPTH * SCALE;
ctx.fillStyle = unavailable ? '#e4e7eb' : '#ffffff';
ctx.strokeStyle = '#52606d';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.roundRect(cx - w / 2, cy - h / 2, w, h, 6);
ctx.fill();
ctx.stroke();
// 使えない席は × を付ける。白黒で印刷しても灰色と見分けられるように
if (unavailable) {
drawCross(ctx, cx, cy, h * 0.28);
}
}
function drawCross(ctx, cx, cy, r) {
ctx.strokeStyle = '#9aa5b1';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(cx - r, cy - r);
ctx.lineTo(cx + r, cy + r);
ctx.moveTo(cx + r, cy - r);
ctx.lineTo(cx - r, cy + r);
ctx.stroke();
}
// 図の下に「どの印が何か」を書く。y は凡例の縦の真ん中(px)
function drawLegend(ctx, y) {
const x = toX(0);
ctx.fillStyle = '#e4e7eb';
ctx.strokeStyle = '#52606d';
ctx.lineWidth = 1.5;
ctx.fillRect(x, y - 9, 30, 18);
ctx.strokeRect(x, y - 9, 30, 18);
drawCross(ctx, x + 15, y, 5);
ctx.fillStyle = '#334e68';
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
ctx.font = '13px sans-serif';
ctx.fillText('使えない席', x + 40, y);
}
// canvas の絵を PNG にして、リンクをクリックしたことにしてダウンロードさせる
function savePng(targetCanvas, fileName) {
const link = document.createElement('a');
link.href = targetCanvas.toDataURL('image/png');
link.download = fileName;
link.click();
}
saveButton.addEventListener('click', function () {
savePng(canvas, '座席表.png');
});
draw();
/* 座席表アプリの見た目。
ダークモードに引っぱられて読めなくならないよう light に固定している。 */
:root {
color-scheme: light;
}
body {
margin: 0;
padding: 24px 16px 48px;
font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
line-height: 1.7;
color: #1f2933;
background: #f7f8fa;
}
h1 {
margin: 0 0 24px;
font-size: 1.5rem;
}
.panel {
width: fit-content;
max-width: 100%;
box-sizing: border-box;
padding: 16px;
background: #fff;
border: 1px solid #d8dee6;
border-radius: 10px;
}
/* 図は画面の幅に合わせて縮める。保存される PNG の大きさは変わらない */
canvas {
display: block;
max-width: 100%;
height: auto;
border: 1px solid #e4e7eb;
}
button {
margin-top: 12px;
padding: 8px 20px;
font-family: inherit;
font-size: 0.9rem;
color: #fff;
background: #2b6cb0;
border: none;
border-radius: 6px;
cursor: pointer;
}
button:hover {
background: #245a93;
}
完成例をダウンロード