PageSourceSearch

https://simplerouteprinter.pages.dev/src/core/utils.js

js simplerouteprinter.pages.dev collected 2026-10-03 05:56:14 UTC 2,459 bytes, 85 lines download raw bytes

1/**
2 * src/core/utils.js
3 * 共通ユーティリティ
4 */
5
6/**
7 * 📐 用紙サイズ → ピクセル計算
8 * @param {string} paperSize - 'A4', 'A3', 'A5'
9 * @param {'portrait'|'landscape'} orientation
10 * @param {number} dpi - default 96
11 * @returns {{width:number,height:number}}
12 */
13export function getPixelSizeFromPaper(paperSize, orientation='portrait', dpi=96){
14  const mmPerInch = 25.4;
15  const sizeTable = {
16    A4:{width:210,height:297},
17    A3:{width:297,height:420},
18    A5:{width:148,height:210}
19  };
20  const mm = sizeTable[paperSize];
21  if(!mm){
22    console.warn('未対応の用紙サイズ:',paperSize);
23    return {width:800,height:600};
24  }
25  const wmm = orientation==='landscape'?mm.height:mm.width;
26  const hmm = orientation==='landscape'?mm.width:mm.height;
27  return {
28    width: Math.round((wmm/mmPerInch)*dpi),
29    height: Math.round((hmm/mmPerInch)*dpi)
30  };
31}
32
33/**
34 * 🌍 Haversine公式による2点間距離計算
35 * @param {number} lat1 - 地点1の緯度
36 * @param {number} lng1 - 地点1の経度
37 * @param {number} lat2 - 地点2の緯度
38 * @param {number} lng2 - 地点2の経度
39 * @returns {number} 距離(メートル)
40 */
41export function calculateHaversineDistance(lat1, lng1, lat2, lng2) {
42  const R = 6371000; // 地球の半径(メートル)
43  const toRad = (deg) => deg * Math.PI / 180;
44
45  const dLat = toRad(lat2 - lat1);
46  const dLng = toRad(lng2 - lng1);
47
48  const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
49            Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
50            Math.sin(dLng / 2) * Math.sin(dLng / 2);
51
52  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
53
54  return R * c;
55}
56
57/**
58 * 📏 座標リストの合計距離を計算
59 * @param {Array<{lat: number, lng: number}>} coords - 座標リスト
60 * @returns {number} 合計距離(メートル)
61 */
62export function calculateTotalDistance(coords) {
63  if (coords.length < 2) return 0;
64
65  let total = 0;
66  for (let i = 0; i < coords.length - 1; i++) {
67    total += calculateHaversineDistance(
68      coords[i].lat, coords[i].lng,
69      coords[i + 1].lat, coords[i + 1].lng
70    );
71  }
72  return total;
73}
74
75/**
76 * 📝 距離の表示フォーマット
77 * @param {number} meters - 距離(メートル)
78 * @returns {string} フォーマットされた距離文字列
79 */
80export function formatDistance(meters) {
81  if (meters < 1000) {
82    return `${Math.round(meters)} m`;
83  }
84  return `${(meters / 1000).toFixed(2)} km`;
85}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.