prerequisites.html 21 KB

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