prerequisites.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!DOCTYPE html><html lang="ja"><head>
  2. <meta charset="utf-8">
  3. <title>の前提条件</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – の前提条件">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>の前提条件</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>これらの記事は、three.jsの使用方法を学習するためものです。
  29. JavaScriptのプログラミング方法を把握してる事を前提とします。
  30. DOMとは何か、HTMLの記述方法、JavaScriptでDOMの作成方法を把握してる事を前提とします。
  31. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import">es6 modules</a> のimportや <code class="notranslate" translate="no">&lt;script type="module"&gt;</code> タグを把握してる事を前提とします。
  32. CSSや <a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors">CSSセレクター</a> が何か把握してる事を前提とします。
  33. ES5、ES6、およびES7を把握してる事を前提とします。
  34. イベントとコールバックでJavaScripが実行されるのを把握してる事を前提とします。
  35. クロージャとは何かを知っている事を前提とします。</p>
  36. <p>また、以下に簡単な復習と注意事項があります。</p>
  37. <h2 id="es6-">es6モジュール</h2>
  38. <p>es6モジュールはスクリプトの中で <code class="notranslate" translate="no">import</code> キーワード、または <code class="notranslate" translate="no">&lt;script type="module"&gt;</code> タグを使用してインラインでロードできます。
  39. 以下に両方の使用​​例があります。</p>
  40. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;script type="module"&gt;
  41. import * as THREE from 'three';
  42. ...
  43. &lt;/script&gt;
  44. </pre>
  45. <p>パスは絶対パス、または相対パスでなければなりません。相対パスは常に <code class="notranslate" translate="no">./</code> または <code class="notranslate" translate="no">../</code> で始まり <code class="notranslate" translate="no">&lt;img&gt;</code> や <code class="notranslate" translate="no">&lt;a&gt;</code> などの他のタグやcss参照とは異なります。</p>
  46. <p>詳細は<a href="fundamentals.html">この記事</a>の最後に記載しています。</p>
  47. <h2 id="-document-queryselector-document-queryselectorall-"><code class="notranslate" translate="no">document.querySelector</code> と <code class="notranslate" translate="no">document.querySelectorAll</code></h2>
  48. <p><code class="notranslate" translate="no">document.querySelector</code> を使用し、CSSセレクターに一致する最初の要素を選択できます。
  49. <code class="notranslate" translate="no">document.querySelectorAll</code> は、CSSセレクターに一致するすべての要素を返します。</p>
  50. <h2 id="-onbody-"><code class="notranslate" translate="no">onbody</code> は必要ありません</h2>
  51. <p>20年前の古いWebページでは、以下のようなHTMLが多く使われてました。</p>
  52. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">&lt;body onload="somefunction()"&gt;
  53. </pre><p>このスタイルは非推奨です。スクリプトをページの下部に配置します。</p>
  54. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;html&gt;
  55. &lt;head&gt;
  56. ...
  57. &lt;/head&gt;
  58. &lt;body&gt;
  59. ...
  60. &lt;/body&gt;
  61. &lt;script&gt;
  62. // inline javascript
  63. &lt;/script&gt;
  64. &lt;/html&gt;
  65. </pre>
  66. <p>または、<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script">deferプロパティを使用します</a>。</p>
  67. <h2 id="-">クロージャーの仕組みを知る</h2>
  68. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function a(v) {
  69. const foo = v;
  70. return function() {
  71. return foo;
  72. };
  73. }
  74. const f = a(123);
  75. const g = a(456);
  76. console.log(f()); // prints 123
  77. console.log(g()); // prints 456
  78. </pre>
  79. <p>上記のコードでは、新しい関数が作成される度に関数 <code class="notranslate" translate="no">a</code> を呼び出します。
  80. その関数は変数 <code class="notranslate" translate="no">foo</code> を <em>閉じ込めます</em> 。
  81. ここに<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures">詳細な情報</a> があります。</p>
  82. <h2 id="-this-"><code class="notranslate" translate="no">this</code> がどのように機能するか知る</h2>
  83. <p><code class="notranslate" translate="no">this</code> は魔法ではありません。
  84. 引数が関数に渡されるのと同じように、関数に自動的に渡される実質的な変数です。
  85. 簡単に説明すると、次のような関数を直接呼び出す場合です。</p>
  86. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">somefunction(a, b, c);
  87. </pre><p><code class="notranslate" translate="no">this</code> は <code class="notranslate" translate="no">null</code>(strictモードまたはモジュールの場合)になりますが、ドット演算子を使用して関数を呼び出す場合と同様です。</p>
  88. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">someobject.somefunction(a, b, c);
  89. </pre><p><code class="notranslate" translate="no">this</code> は <code class="notranslate" translate="no">someobject</code> がセットされます。</p>
  90. <p>この部分はみなさんが混乱するコールバックです。</p>
  91. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const callback = someobject.somefunction;
  92. loader.load(callback);
  93. </pre><p>これは経験の浅い人が期待するように動作しません。
  94. なぜなら <code class="notranslate" translate="no">loader.load</code> がコールバックを呼び出す時、<code class="notranslate" translate="no">.</code> 演算子で <code class="notranslate" translate="no">this</code> を呼び出していないため、デフォルトではnullになります(ローダーが明示的に何かを設定しない限り)
  95. コールバックが発生した時に <code class="notranslate" translate="no">this</code> を <code class="notranslate" translate="no">someobject</code> したい場合は、関数をバインドする必要があります。</p>
  96. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const callback = someobject.somefunction.bind(someobject);
  97. loader.load(callback);
  98. </pre><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this">この記事は <code class="notranslate" translate="no">this</code> を理解するのに役立つかもしれません</a>.</p>
  99. <h2 id="es5-es6-es7-stuff">ES5/ES6/ES7 stuff</h2>
  100. <h3 id="-var-const-let-"><code class="notranslate" translate="no">var</code> は非推奨です。 <code class="notranslate" translate="no">const</code> か <code class="notranslate" translate="no">let</code> を使って下さい</h3>
  101. <p><code class="notranslate" translate="no">var</code> は使用する理由がありません。varを使用するのは悪い習慣と見なされます。ほとんどの場合、変数の値を変えない場合は <code class="notranslate" translate="no">const</code> を使用します。
  102. 値が変更される場合は <code class="notranslate" translate="no">let</code> を使用します。これにより大量のバグを回避できます。</p>
  103. <h3 id="-for-of-for-in-"><code class="notranslate" translate="no">for of</code> を使用し <code class="notranslate" translate="no">for in</code> は使用しない</h3>
  104. <p><code class="notranslate" translate="no">for of</code> は新しい書き方で、 <code class="notranslate" translate="no">for in</code> は古い書き方です。 <code class="notranslate" translate="no">for in</code> で解決できない問題を <code class="notranslate" translate="no">for of</code> が解決しています。
  105. 解決した一例として、オブジェクトのすべてのkey/valueのペアを反復処理ができます。</p>
  106. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">for (const [key, value] of Object.entries(someObject)) {
  107. console.log(key, value);
  108. }
  109. </pre>
  110. <h3 id="-foreach-map-filter-"><code class="notranslate" translate="no">forEach</code> 、<code class="notranslate" translate="no">map</code> 、 <code class="notranslate" translate="no">filter</code> は役に立ちます</h3>
  111. <p>配列の関数である <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach"><code class="notranslate" translate="no">forEach</code></a> や
  112. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"><code class="notranslate" translate="no">map</code></a> 、
  113. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"><code class="notranslate" translate="no">filter</code></a>
  114. はモダンなJavaScriptで広く使われています。</p>
  115. <h3 id="-">分割代入を使う</h3>
  116. <p><code class="notranslate" translate="no">const dims = {width: 300, height: 150}</code> のObjectがあるとします。</p>
  117. <p>古いコードの場合</p>
  118. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const width = dims.width;
  119. const height = dims.height;
  120. </pre><p>新しいコードの場合</p>
  121. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const {width, height} = dims;
  122. </pre><h3 id="-">オブジェクト宣言のショートカットを使う</h3>
  123. <p>古いコードの場合</p>
  124. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const width = 300;
  125. const height = 150;
  126. const obj = {
  127. width: width,
  128. height: height,
  129. area: function() {
  130. return this.width * this.height
  131. },
  132. };
  133. </pre>
  134. <p>新しいコードの場合</p>
  135. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const width = 300;
  136. const height = 150;
  137. const obj = {
  138. width,
  139. height,
  140. area() {
  141. return this.width * this.height;
  142. },
  143. };
  144. </pre>
  145. <h3 id="-">スプレット演算子 <code class="notranslate" translate="no">...</code> を使う</h3>
  146. <p>スプレット演算子にはたくさんの使い方があります。例えば</p>
  147. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> function log(className, ...args) {
  148. const elem = document.createElement('div');
  149. elem.className = className;
  150. elem.textContent = [...args].join(' ');
  151. document.body.appendChild(elem);
  152. }
  153. </pre>
  154. <p>もう1つの例</p>
  155. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const position = [1, 2, 3];
  156. somemesh.position.set(...position);
  157. </pre>
  158. <h3 id="-class-"><code class="notranslate" translate="no">class</code> を使う</h3>
  159. <p>ES5より以前のオブジェクトのようなクラス構文は、ほとんどのプログラマーにはなじみがありませんでした。
  160. ES5以降では、C ++やC#、Javaのスタイルに近い<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code class="notranslate" translate="no">class</code> キーワードを使用</a>
  161. できるようになりました。</p>
  162. <h3 id="getters-setters-">gettersとsettersを理解する</h3>
  163. <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get">Getters</a> と
  164. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set">setters</a> は
  165. ほとんどのモダンなプログラミン言語でよく使われます。
  166. ES5のクラス構文により、ES5以前よりもはるかに簡単に使えます。</p>
  167. <h3 id="-">必要に応じてアロー関数を使います</h3>
  168. <p>アロー関数はcallbackとPromiseで特に役立ちます。</p>
  169. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">loader.load((texture) =&gt; {
  170. // use texture
  171. });
  172. </pre>
  173. <p>アロー関数は <code class="notranslate" translate="no">this</code> をバインドします。</p>
  174. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = (args) =&gt; {/* code */};
  175. </pre>
  176. <p>ショートカットで書くなら</p>
  177. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = (function(args) {/* code */}).bind(this));
  178. </pre>
  179. <h3 id="promise-async-await-">Promiseはasync/awaitと同様です</h3>
  180. <p>Promisesは非同期な処理を助ます。Async/awaitはpromiseを助けます。</p>
  181. <p>ここで扱うには大きな話題になるため、<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">promiseのドキュメントを読んで下さい</a>。
  182. また、<a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await">async/awaitもドキュメントを読んで下さい</a>。</p>
  183. <h3 id="-">テンプレートリテラルを使用する</h3>
  184. <p>テンプレートリテラルは、引用符("", '')の代わりにバックティック文字(<code class="notranslate" translate="no"> </code>)を使います。</p>
  185. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">const foo = `this is a template literal`;
  186. </pre><p>テンプレートリテラルには基本的に2つの機能があります。1つは複数行にかけます。</p>
  187. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = `this
  188. is
  189. a
  190. template
  191. literal`;
  192. const bar = "this\nis\na\ntemplate\nliteral";
  193. </pre>
  194. <p>上記の <code class="notranslate" translate="no">foo</code> と <code class="notranslate" translate="no">bar</code> は同様の意味になります.</p>
  195. <p>もう1つは、文字モードの中に <code class="notranslate" translate="no">${javascript-expression}</code> のようにJavaScriptのスニペッドを挿入できます。
  196. これはテンプレートの一部です。例えば</p>
  197. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const r = 192;
  198. const g = 255;
  199. const b = 64;
  200. const rgbCSSColor = `rgb(${r},${g},${b})`;
  201. </pre>
  202. <p>または</p>
  203. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = [192, 255, 64];
  204. const rgbCSSColor = `rgb(${color.join(',')})`;
  205. </pre>
  206. <p>または</p>
  207. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const aWidth = 10;
  208. const bWidth = 20;
  209. someElement.style.width = `${aWidth + bWidth}px`;
  210. </pre>
  211. <h1 id="javascript-">JavaScriptのコーディング規則を学びましょう</h1>
  212. <p>自由にコードフォーマットする事ができますが、少なくとも1つの規則に注意する必要があります。
  213. JavaScriptの変数名や関数名、メソッド名はすべてローワーキャメルケースです。
  214. コンストラクターやクラスの名前はアッパーキャメルケースです。
  215. このルールに従うなら、他のほとんどのJavaScriptコードと一致します。</p>
  216. <p>多くの <a href="https://eslint.org">リンター]</a> やコード内の明らかなエラーをチェックするプログラムは、間違ったケースを使用するとエラーを指摘します。
  217. 上記の規則に従うことで、エラーが間違っている事がわかるからです。</p>
  218. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const v = new vector(); // clearly an error if all classes start with a capital letter
  219. const v = Vector(); // clearly an error if all functions start with a lowercase latter.
  220. </pre>
  221. <h1 id="visual-studio-code-">Visual Studio Codeの使用を検討する</h1>
  222. <p>もちろんあなたが望むエディタが良いですが、もし望むエディタがなければ <a href="https://code.visualstudio.com/">Visual Studio Code</a> を使う事を検討してみて下さい。
  223. インストールし <a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">eslintをセットアップ</a> します。
  224. セットアップには数分かかる場合がありますが、バグを見つけるのに非常に役に立ちます。</p>
  225. <p>いくつかの例</p>
  226. <p><a href="https://eslint.org/docs/rules/no-undef"><code class="notranslate" translate="no">no-undef</code> ルール</a> を有効にすると、VSCodeのEsLintで多くの未定義の変数について警告します。</p>
  227. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-not-defined.png"></div>
  228. <p>上記は <code class="notranslate" translate="no">doTheThing</code> のスペルを <code class="notranslate" translate="no">doThing</code> と間違えている事がわかります。
  229. <code class="notranslate" translate="no">doThing</code> の下に赤い波線があり、その上をホバリングすると定義されていない事がわかります。
  230. 1つのエラーが回避されました。
  231. <code class="notranslate" translate="no">THREE</code> を使用して警告が表示された場合、eslintに <code class="notranslate" translate="no">THREE</code> が存在する事を伝えるため、JavaScriptファイルの先頭に <code class="notranslate" translate="no">/* global THREE */</code> を追加します。</p>
  232. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-not-a-constructor.png"></div>
  233. <p>上記では、eslintは <code class="notranslate" translate="no">アッパーキャメルケース</code> がコンストラクターであるというルールを知っているため、 <code class="notranslate" translate="no">new</code> を使用する必要があります。
  234. 他のエラーをキャッチして避けます。これは<a href="https://eslint.org/docs/rules/new-cap"><code class="notranslate" translate="no">new-cap</code> ルール</a> です。</p>
  235. <p><a href="https://eslint.org/docs/rules/">数百のEslintルールをオン・オフにカスタム</a> できます。
  236. 上記の例では <code class="notranslate" translate="no">var</code> でなく <code class="notranslate" translate="no">const</code> と <code class="notranslate" translate="no">let</code> を使用するルールを適用しました。
  237. コードでは <code class="notranslate" translate="no">var</code> を使用しましたが、<code class="notranslate" translate="no">let</code> または <code class="notranslate" translate="no">const</code> を使用する必要があると警告されました。</p>
  238. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-var.png"></div>
  239. <p>ここでは <code class="notranslate" translate="no">let</code> を使用しましたが、値を変更しない事がわかったため、 <code class="notranslate" translate="no">const</code> を使用することが提案されました。</p>
  240. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-let.png"></div>
  241. <p>もちろん、 <code class="notranslate" translate="no">var</code> を使い続けたい場合は、そのルールをオフにすることができます。
  242. 上記で記述したように <code class="notranslate" translate="no">var</code> よりも <code class="notranslate" translate="no">const</code> と <code class="notranslate" translate="no">let</code> を使用することを好みます。
  243. それらはうまく機能し、バグを防ぎます。</p>
  244. <p>ルールをオーバーライドする必要がある場合、1行のコードまたはコードセクションに<a href="https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments">無効にするコメントを追加できます</a>。</p>
  245. <h1 id="-">レガシーブラウザをサポートする必要がある場合は、トランスパイラーを使用して下さい</h1>
  246. <p>ほとんどのモダンなブラウザは自動更新されるため、これらすべての機能を使用すると便利です。生産性を高め、バグを回避できます。
  247. あなたのプロジェクトで古いブラウザをサポートする必要があれば、<a href="https://babeljs.io">ES5/ES6/ES7コードをES5のJavascriptにトランスパイラーするツール</a> を使用して下さい。</p>
  248. </div>
  249. </div>
  250. </div>
  251. <script src="../resources/prettify.js"></script>
  252. <script src="../resources/lesson.js"></script>
  253. </body></html>