responsive.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Responsive Design</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 – Responsive Design">
  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>Responsive Design</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>This is the second article in a series of articles about three.js.
  29. The first article was <a href="fundamentals.html">about fundamentals</a>.
  30. If you haven't read that yet you might want to start there.</p>
  31. <p>This article is about how to make your three.js app be responsive
  32. to any situation. Making a webpage responsive generally refers
  33. to the page displaying well on different sized displays from
  34. desktops to tablets to phones.</p>
  35. <p>For three.js there are even more situations to consider. For
  36. example, a 3D editor with controls on the left, right, top, or
  37. bottom is something we might want to handle. A live diagram
  38. in the middle of a document is another example.</p>
  39. <p>The last sample we had used a plain canvas with no CSS and
  40. no size</p>
  41. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;canvas id="c"&gt;&lt;/canvas&gt;
  42. </pre>
  43. <p>That canvas defaults to 300x150 CSS pixels in size.</p>
  44. <p>In the web platform the recommended way to set the size
  45. of something is to use CSS.</p>
  46. <p>Let's make the canvas fill the page by adding CSS</p>
  47. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;style&gt;
  48. html, body {
  49. margin: 0;
  50. height: 100%;
  51. }
  52. #c {
  53. width: 100%;
  54. height: 100%;
  55. display: block;
  56. }
  57. &lt;/style&gt;
  58. </pre>
  59. <p>In HTML the body has a margin of 5 pixels by default so setting the
  60. margin to 0 removes the margin. Setting the html and body height to 100%
  61. makes them fill the window. Otherwise they are only as large
  62. as the content that fills them.</p>
  63. <p>Next we tell the <code class="notranslate" translate="no">id=c</code> element to be
  64. 100% the size of its container which in this case is the body of
  65. the document.</p>
  66. <p>Finally we set its <code class="notranslate" translate="no">display</code> mode to <code class="notranslate" translate="no">block</code>. A canvas's
  67. default display mode is <code class="notranslate" translate="no">inline</code>. Inline
  68. elements can end up adding whitespace to what is displayed. By
  69. setting the canvas to <code class="notranslate" translate="no">block</code> that issue goes away.</p>
  70. <p>Here's the result</p>
  71. <p></p><div translate="no" class="threejs_example_container notranslate">
  72. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/responsive-no-resize.html"></iframe></div>
  73. <a class="threejs_center" href="/manual/examples/responsive-no-resize.html" target="_blank">click here to open in a separate window</a>
  74. </div>
  75. <p></p>
  76. <p>You can see the canvas is now filling the page but there are 2
  77. problems. One our cubes are stretched. They are not cubes they
  78. are more like boxes. Too tall or too wide. Open the
  79. example in its own window and resize it. You'll see how
  80. the cubes get stretched wide and tall.</p>
  81. <p><img src="../resources/images/resize-incorrect-aspect.png" width="407" class="threejs_center nobg"></p>
  82. <p>The second problem is they look low resolution or blocky and
  83. blurry. Stretch the window really large and you'll really see
  84. the issue.</p>
  85. <p><img src="../resources/images/resize-low-res.png" class="threejs_center nobg"></p>
  86. <p>Let's fix the stretchy problem first. To do that we need
  87. to set the aspect of the camera to the aspect of the canvas's
  88. display size. We can do that by looking at the canvas's
  89. <code class="notranslate" translate="no">clientWidth</code> and <code class="notranslate" translate="no">clientHeight</code> properties.</p>
  90. <p>We'll update our render loop like this</p>
  91. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  92. time *= 0.001;
  93. + const canvas = renderer.domElement;
  94. + camera.aspect = canvas.clientWidth / canvas.clientHeight;
  95. + camera.updateProjectionMatrix();
  96. ...
  97. </pre>
  98. <p>Now the cubes should stop being distorted.</p>
  99. <p></p><div translate="no" class="threejs_example_container notranslate">
  100. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/responsive-update-camera.html"></iframe></div>
  101. <a class="threejs_center" href="/manual/examples/responsive-update-camera.html" target="_blank">click here to open in a separate window</a>
  102. </div>
  103. <p></p>
  104. <p>Open the example in a separate window and resize the window
  105. and you should see the cubes are no longer stretched tall or wide.
  106. They stay the correct aspect regardless of window size.</p>
  107. <p><img src="../resources/images/resize-correct-aspect.png" width="407" class="threejs_center nobg"></p>
  108. <p>Now let's fix the blockiness.</p>
  109. <p>Canvas elements have 2 sizes. One size is the size the canvas is displayed
  110. on the page. That's what we set with CSS. The other size is the
  111. number of pixels in the canvas itself. This is no different than an image.
  112. For example we might have a 128x64 pixel image and using
  113. CSS we might display as 400x200 pixels.</p>
  114. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;img src="some128x64image.jpg" style="width:400px; height:200px"&gt;
  115. </pre>
  116. <p>A canvas's internal size, its resolution, is often called its drawingbuffer size.
  117. In three.js we can set the canvas's drawingbuffer size by calling <code class="notranslate" translate="no">renderer.setSize</code>.
  118. What size should we pick? The most obvious answer is "the same size the canvas is displayed".
  119. Again, to do that we can look at the canvas's <code class="notranslate" translate="no">clientWidth</code> and <code class="notranslate" translate="no">clientHeight</code>
  120. properties.</p>
  121. <p>Let's write a function that checks if the renderer's canvas is not
  122. already the size it is being displayed as and if so set its size.</p>
  123. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function resizeRendererToDisplaySize(renderer) {
  124. const canvas = renderer.domElement;
  125. const width = canvas.clientWidth;
  126. const height = canvas.clientHeight;
  127. const needResize = canvas.width !== width || canvas.height !== height;
  128. if (needResize) {
  129. renderer.setSize(width, height, false);
  130. }
  131. return needResize;
  132. }
  133. </pre>
  134. <p>Notice we check if the canvas actually needs to be resized. Resizing the canvas
  135. is an interesting part of the canvas spec and it's best not to set the same
  136. size if it's already the size we want.</p>
  137. <p>Once we know if we need to resize or not we then call <code class="notranslate" translate="no">renderer.setSize</code> and
  138. pass in the new width and height. It's important to pass <code class="notranslate" translate="no">false</code> at the end.
  139. <code class="notranslate" translate="no">render.setSize</code> by default sets the canvas's CSS size but doing so is not
  140. what we want. We want the browser to continue to work how it does for all other
  141. elements which is to use CSS to determine the display size of the element. We don't
  142. want canvases used by three to be different than other elements.</p>
  143. <p>Note that our function returns true if the canvas was resized. We can use
  144. this to check if there are other things we should update. Let's modify
  145. our render loop to use the new function</p>
  146. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  147. time *= 0.001;
  148. + if (resizeRendererToDisplaySize(renderer)) {
  149. + const canvas = renderer.domElement;
  150. + camera.aspect = canvas.clientWidth / canvas.clientHeight;
  151. + camera.updateProjectionMatrix();
  152. + }
  153. ...
  154. </pre>
  155. <p>Since the aspect is only going to change if the canvas's display size
  156. changed we only set the camera's aspect if <code class="notranslate" translate="no">resizeRendererToDisplaySize</code>
  157. returns <code class="notranslate" translate="no">true</code>.</p>
  158. <p></p><div translate="no" class="threejs_example_container notranslate">
  159. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/responsive.html"></iframe></div>
  160. <a class="threejs_center" href="/manual/examples/responsive.html" target="_blank">click here to open in a separate window</a>
  161. </div>
  162. <p></p>
  163. <p>It should now render with a resolution that matches the display
  164. size of the canvas.</p>
  165. <p>To make the point about letting CSS handle the resizing let's take
  166. our code and put it in a <a href="../examples/threejs-responsive.js">separate <code class="notranslate" translate="no">.js</code> file</a>.
  167. Here then are a few more examples where we let CSS choose the size and notice we had
  168. to change zero code for them to work.</p>
  169. <p>Let's put our cubes in the middle of a paragraph of text.</p>
  170. <p></p><div translate="no" class="threejs_example_container notranslate">
  171. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/responsive-paragraph.html&amp;startPane=html"></iframe></div>
  172. <a class="threejs_center" href="/manual/examples/responsive-paragraph.html" target="_blank">click here to open in a separate window</a>
  173. </div>
  174. <p></p>
  175. <p>and here's our same code used in an editor style layout
  176. where the control area on the right can be resized.</p>
  177. <p></p><div translate="no" class="threejs_example_container notranslate">
  178. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/responsive-editor.html&amp;startPane=html"></iframe></div>
  179. <a class="threejs_center" href="/manual/examples/responsive-editor.html" target="_blank">click here to open in a separate window</a>
  180. </div>
  181. <p></p>
  182. <p>The important part to notice is no code changed. Only our HTML and CSS
  183. changed.</p>
  184. <h2 id="handling-hd-dpi-displays">Handling HD-DPI displays</h2>
  185. <p>HD-DPI stands for high-density dot per inch displays.
  186. That's most Macs nowadays and many Windows machines
  187. as well as pretty much all smartphones.</p>
  188. <p>The way this works in the browser is they use
  189. CSS pixels to set the sizes which are supposed to be the same
  190. regardless of how high res the display is. The browser
  191. will just render text with more detail but the
  192. same physical size.</p>
  193. <p>There are various ways to handle HD-DPI with three.js.</p>
  194. <p>The first one is just not to do anything special. This
  195. is arguably the most common. Rendering 3D graphics
  196. takes a lot of GPU processing power. Mobile GPUs have
  197. less power than desktops, at least as of 2018, and yet
  198. mobile phones often have very high resolution displays.
  199. The current top of the line phones have an HD-DPI ratio
  200. of 3x meaning for every one pixel from a non-HD-DPI display
  201. those phones have 9 pixels. That means they have to do 9x
  202. the rendering.</p>
  203. <p>Computing 9x the pixels is a lot of work so if we just
  204. leave the code as it is we'll compute 1x the pixels and the
  205. browser will just draw it at 3x the size (3x by 3x = 9x pixels).</p>
  206. <p>For any heavy three.js app that's probably what you want
  207. otherwise you're likely to get a slow framerate.</p>
  208. <p>That said if you actually do want to render at the resolution
  209. of the device there are a couple of ways to do this in three.js.</p>
  210. <p>One is to tell three.js a resolution multiplier using <code class="notranslate" translate="no">renderer.setPixelRatio</code>.
  211. You ask the browser what the multiplier is from CSS pixels to device pixels
  212. and pass that to three.js</p>
  213. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> renderer.setPixelRatio(window.devicePixelRatio);
  214. </pre><p>After that any calls to <code class="notranslate" translate="no">renderer.setSize</code> will magically
  215. use the size you request multiplied by whatever pixel ratio
  216. you passed in. <strong>This is strongly NOT RECOMMENDED</strong>. See below</p>
  217. <p>The other way is to do it yourself when you resize the canvas.</p>
  218. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> function resizeRendererToDisplaySize(renderer) {
  219. const canvas = renderer.domElement;
  220. const pixelRatio = window.devicePixelRatio;
  221. const width = Math.floor( canvas.clientWidth * pixelRatio );
  222. const height = Math.floor( canvas.clientHeight * pixelRatio );
  223. const needResize = canvas.width !== width || canvas.height !== height;
  224. if (needResize) {
  225. renderer.setSize(width, height, false);
  226. }
  227. return needResize;
  228. }
  229. </pre>
  230. <p>This second way is objectively better. Why? Because it means I get what I ask for.
  231. There are many cases when using three.js where we need to know the actual
  232. size of the canvas's drawingBuffer. For example when making a post processing filter,
  233. or if we are making a shader that accesses <code class="notranslate" translate="no">gl_FragCoord</code>, if we are making
  234. a screenshot, or reading pixels for GPU picking, for drawing into a 2D canvas,
  235. etc... There are many cases where if we use <code class="notranslate" translate="no">setPixelRatio</code> then our actual size will be different
  236. than the size we requested and we'll have to guess when to use the size
  237. we asked for and when to use the size three.js is actually using.
  238. By doing it ourselves we always know the size being used is the size we requested.
  239. There is no special case where magic is happening behind the scenes.</p>
  240. <p>Here's an example using the code above.</p>
  241. <p></p><div translate="no" class="threejs_example_container notranslate">
  242. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/responsive-hd-dpi.html"></iframe></div>
  243. <a class="threejs_center" href="/manual/examples/responsive-hd-dpi.html" target="_blank">click here to open in a separate window</a>
  244. </div>
  245. <p></p>
  246. <p>It might be hard to see the difference but if you have an HD-DPI
  247. display and you compare this sample to those above you should
  248. notice the edges are more crisp.</p>
  249. <p>This article covered a very basic but fundamental topic. Next up lets quickly
  250. <a href="primitives.html">go over the basic primitives that three.js provides</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>