prerequisites.html 21 KB

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