国旗アプリを改良しようと考えた。1ステップ前の国旗に戻れるようにするものだ。最初は,左下の1/4×1/4領域に,前回表示した国旗の小さなイメージを表示させてそこをクリックすると前の表示に戻るようにするものだ。国名は通し番号をつけて右下に移動した。
ChatGPT o3-mini-highにお願いしたところ,何度かのやりとりの後に目的のものはできたにはできたが,国旗が2つも表示されているのは子どもにはわかりにくそうだ。やり直し。
Q(koshix):
国旗表示アプリ
以下のウェブアプリに修正を加えてください。
(1) 各国旗には次の3つの番号を持ちます。(a) アルファベット順の通し番号,(b) アイウエオ順の通し番号,(c) アプリ起動後にその国旗が最初に表示されたときの通し番号
(2) カタカナの国名表示部分を,通し番号:カタカナ国名 の形に修正してください。このときの通し番号は,表示モードに対応したものとします。
(3) 右下に半透明グレーのボタン領域を設定してください。その機能は後で説明します。まずは,(1) (2) を実装した全体コードをお願いします。
なお,修正前のコードは以下のとおりです。
Q(koshix):
ボタン領域を「もどる」というなまえにして,ここをクリックすると選択された領域で表示されている国旗より通し番号がワンステップ小さいものに戻って表示するようにしてください。
これで目的のものはできた。361行のウェブアプリである。やはりどういうロジックにするかを想定しながらプロンプトを考えるのが重要だ。前回は単なる仕様だけで依頼したので,うまく機能しなかったので結構モチャついた371行。
図:kokki.htmlのiPhoneでの実行画面
以下が,修正版のkokki.htmlのソースコードだ。108行目あたりのkazaha.jpgを自分のイメージファイルに変えて,kokki.htmlと同じフォルダに設置して,ブラウザで閲覧すればOKだ。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>かざは の こっき</title>
<style>
html, body {
margin: 0;
padding: 0;
background-color: #f0f0f0;
height: 100%;
font-family: sans-serif;
}
/* トップページ(スタート前) */
#top-page {
text-align: center;
padding: 20px;
}
#top-page h1 {
font-size: 2em;
margin-bottom: 20px;
}
#top-page img {
width: 437px;
height: 377px;
margin-bottom: 20px;
}
/* スタートボタン */
#start-button {
padding: 12px 24px;
font-size: 1.5em;
border: none;
border-radius: 8px;
background-color: #007aff;
color: #fff;
cursor: pointer;
}
/* 国旗表示ボックス */
#flag-container {
width: 90vw;
height: 70vh;
margin: 5vh auto;
position: relative;
background-color: #000;
border: 5px solid #fff;
box-shadow: 0 4px 8px rgba(0,0,0,0.5);
cursor: pointer;
display: none; /* スタート前は非表示 */
touch-action: manipulation;
}
#flag-container img {
width: 100%;
height: 100%;
object-fit: contain;
opacity: 0;
transition: opacity 0.8s ease-in-out;
position: absolute;
top: 0;
left: 0;
}
#country-name {
position: absolute;
bottom: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.7);
padding: 4px 8px;
border-radius: 8px;
font-size: 1.2em;
z-index: 10;
}
/* 設定パネル */
#control-panel {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
background: rgba(255,255,255,0.9);
padding: 8px;
border-radius: 8px;
z-index: 10;
display: flex;
gap: 10px;
align-items: center;
}
#control-panel select {
font-size: 1em;
padding: 4px;
}
/* 右下の半透明グレーのボタン領域(もどるボタン) */
#button-area {
position: absolute;
bottom: 10px;
right: 10px;
background: rgba(128, 128, 128, 0.5);
padding: 10px;
border-radius: 8px;
z-index: 10;
cursor: pointer;
user-select: none;
}
</style>
</head>
<body>
<!-- トップページ(初回表示) -->
<div id="top-page">
<h1>かざは の こっき</h1>
<img src="kazaha.jpg" alt="かざはの顔">
<br>
<button id="start-button">スタート</button>
</div>
<!-- 国旗表示エリア -->
<div id="flag-container">
<img id="flag-image" src="" alt="国旗">
<div id="country-name"></div>
<div id="control-panel">
<div>
<label for="order-select">表示順序:</label>
<select id="order-select">
<option value="alphabetical">アルファベット順</option>
<option value="japanese">アイウエオ順</option>
<option value="random">ランダム</option>
</select>
</div>
<div>
<label for="region-select">表示領域:</label>
<select id="region-select">
<option value="all">全世界</option>
<option value="eastAsiaOceania">東アジア〜オセアニア</option>
<option value="southWestAsia">南〜西アジア</option>
<option value="europe">ヨーロッパ</option>
<option value="africa">アフリカ大陸</option>
<option value="americas">アメリカ大陸</option>
</select>
</div>
</div>
<!-- 右下の「もどる」ボタン領域 -->
<div id="button-area">もどる</div>
</div>
<!-- Kuroshiro と Kuromoji の読み込み -->
<script src="https://unpkg.com/kuroshiro@1.1.3/dist/kuroshiro.min.js"></script>
<script src="https://unpkg.com/kuroshiro-analyzer-kuromoji@1.1.0/dist/kuroshiro-analyzer-kuromoji.min.js"></script>
<script>
// グローバル変数
let allCountries = [];
let countries = [];
let currentIndex = 0;
let kuroshiro;
let randomDisplayCounter = 1; // ランダムモードで初回表示されたときの番号用
// 初期化処理全体をまとめる
async function initializeApp() {
// トップページを非表示、国旗エリアを表示
document.getElementById("top-page").style.display = "none";
document.getElementById("flag-container").style.display = "block";
// Kuroshiro の初期化
try {
kuroshiro = new Kuroshiro();
await kuroshiro.init(new KuroshiroAnalyzerKuromoji({
dictPath: 'https://unpkg.com/kuromoji@0.1.2/dict/'
}));
console.log("Kuroshiro initialized");
} catch (error) {
console.error("Kuroshiro initialization failed:", error);
}
// 国データの取得
await fetchCountries();
// イベントリスナーの設定
setupEventListeners();
}
// 国データ取得
async function fetchCountries() {
try {
const response = await fetch("https://restcountries.com/v3.1/all");
const data = await response.json();
allCountries = await Promise.all(data.map(async country => {
const flagUrl = country.flags && country.flags.png ? country.flags.png : "";
const nameEn = country.name && country.name.common ? country.name.common : "";
const jpName = (country.translations &&
country.translations.jpn &&
country.translations.jpn.common) ? country.translations.jpn.common : nameEn;
let nameKatakana = "";
try {
nameKatakana = await kuroshiro.convert(jpName, { to: "katakana", mode: "normal" });
} catch (err) {
console.error("Conversion error:", err);
nameKatakana = jpName;
}
return {
flag: flagUrl,
name_en: nameEn,
name_katakana: nameKatakana,
region: country.region || "",
subregion: country.subregion || ""
};
}));
// 不要なデータの除外
allCountries = allCountries.filter(c => c.flag && c.name_en && c.name_katakana);
// 各国旗に対してグローバルな番号を付与
// (a) アルファベット順の通し番号
let sortedAlpha = [...allCountries].sort((a, b) => a.name_en.localeCompare(b.name_en));
sortedAlpha.forEach((country, index) => {
country.numAlpha = index + 1;
});
// (b) アイウエオ順(カタカナ順)の通し番号
let sortedKatakana = [...allCountries].sort((a, b) => a.name_katakana.localeCompare(b.name_katakana, "ja"));
sortedKatakana.forEach((country, index) => {
country.numKatakana = index + 1;
});
// (c) ランダムモード用(初回表示時に設定)の番号は未設定状態に
allCountries.forEach(country => {
country.numDisplay = null;
});
// 初期のフィルタ・ソート処理
filterCountries();
sortCountries();
displayCountry();
} catch (error) {
console.error("Error fetching countries:", error);
}
}
// 表示領域でフィルタ
function filterCountries() {
const regionValue = document.getElementById("region-select").value;
if (regionValue === "all") {
countries = allCountries.slice();
} else if (regionValue === "eastAsiaOceania") {
countries = allCountries.filter(c =>
(c.region === "Oceania") ||
(c.region === "Asia" && (c.subregion === "Eastern Asia" || c.subregion === "South-Eastern Asia"))
);
} else if (regionValue === "southWestAsia") {
countries = allCountries.filter(c =>
c.region === "Asia" && !(c.subregion === "Eastern Asia" || c.subregion === "South-Eastern Asia")
);
} else if (regionValue === "europe") {
countries = allCountries.filter(c => c.region === "Europe");
} else if (regionValue === "africa") {
countries = allCountries.filter(c => c.region === "Africa");
} else if (regionValue === "americas") {
countries = allCountries.filter(c => c.region === "Americas");
}
currentIndex = 0;
}
// 表示順序でソート
function sortCountries() {
const order = document.getElementById("order-select").value;
if (order === "alphabetical") {
countries.sort((a, b) => a.name_en.localeCompare(b.name_en));
} else if (order === "japanese") {
countries.sort((a, b) => a.name_katakana.localeCompare(b.name_katakana, "ja"));
} else if (order === "random") {
// ランダムモードでは Fisher-Yates シャッフルを実施
for (let i = countries.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[countries[i], countries[j]] = [countries[j], countries[i]];
}
}
currentIndex = 0;
}
// 現在の国を表示
function displayCountry() {
if (countries.length === 0) {
console.error("No countries available for the selected region.");
document.getElementById("country-name").textContent = "データなし";
document.getElementById("flag-image").src = "";
return;
}
const currentCountry = countries[currentIndex];
// 表示モードに応じた番号を取得
const orderMode = document.getElementById("order-select").value;
let displayNumber;
if (orderMode === "alphabetical") {
displayNumber = currentCountry.numAlpha;
} else if (orderMode === "japanese") {
displayNumber = currentCountry.numKatakana;
} else if (orderMode === "random") {
if (!currentCountry.numDisplay) { // 初回表示時のみ番号を付与
currentCountry.numDisplay = randomDisplayCounter++;
}
displayNumber = currentCountry.numDisplay;
}
// 国旗画像の切替演出
const flagImage = document.getElementById("flag-image");
flagImage.style.opacity = 0;
setTimeout(() => {
flagImage.src = currentCountry.flag;
// 「通し番号:カタカナ国名」の形式で表示
document.getElementById("country-name").textContent = displayNumber + ":" + currentCountry.name_katakana;
flagImage.onload = () => {
flagImage.style.opacity = 1;
};
}, 800);
}
// イベントリスナーの設定(タッチ対応の場合は touchend を優先)
function setupEventListeners() {
const flagContainer = document.getElementById("flag-container");
function nextCountry() {
currentIndex = (currentIndex + 1) % countries.length;
displayCountry();
}
// タッチまたはクリックで次の国を表示
if ('ontouchstart' in window || navigator.maxTouchPoints) {
flagContainer.addEventListener("touchend", function(e) {
nextCountry();
}, false);
} else {
flagContainer.addEventListener("click", function(e) {
nextCountry();
}, false);
}
// 表示順序・表示領域の変更時の再ソート/再表示
document.getElementById("order-select").addEventListener("change", () => {
sortCountries();
displayCountry();
});
document.getElementById("region-select").addEventListener("change", () => {
filterCountries();
sortCountries();
displayCountry();
});
// 「もどる」ボタンのイベント設定
const buttonArea = document.getElementById("button-area");
// タッチイベントに対応
buttonArea.addEventListener("touchend", function(e) {
e.stopPropagation();
e.preventDefault(); // タッチ操作時のデフォルト動作を防止
if (countries.length > 0) {
currentIndex = (currentIndex - 1 + countries.length) % countries.length;
displayCountry();
}
}, false);
// PCなど、タッチ以外のデバイス用のクリックイベントも残す
buttonArea.addEventListener("click", function(e) {
e.stopPropagation();
if (countries.length > 0) {
currentIndex = (currentIndex - 1 + countries.length) % countries.length;
displayCountry();
}
}, false);
}
// スタートボタンのイベント設定(pointerup イベントを使用)
document.getElementById("start-button").addEventListener("pointerup", () => {
initializeApp();
});
</script>
</body>
</html>
0 件のコメント:
コメントを投稿