ラベル fractal の投稿を表示しています。 すべての投稿を表示
ラベル fractal の投稿を表示しています。 すべての投稿を表示

2015年3月4日水曜日

2 - ol3.2ex 77b - Fractal example 2

「fractal.js(277-ol3ex.js)」は、マップを表示するための JavaScript ファイルです。

「277-ol3ex.js」
var radius = 10e6;
var cos30 = Math.cos(Math.PI / 6);
/** Math.cos()
 * 引数として与えた数のコサインを返します。
 * (MDN[https://developer.mozilla.org/ja/docs/Web/
 * JavaScript/Reference/Global_Objects/Math/cos])
 */
/** Math.PI()
 * 円周率。約 3.14159 です。
 * (MDN[https://developer.mozilla.org/ja/docs/Web
 * /JavaScript/Reference/Global_Objects/Math/PI])
 */
var sin30 = Math.sin(Math.PI / 6);
/** Math.sin()
 * 引数として与えた数のサイン(正弦)を返します。
 * (MDN[https://developer.mozilla.org/ja/docs/Web/
 * JavaScript/Reference/Global_Objects/Math/sin])
 */
var rise = radius * sin30;
var run = radius * cos30;
var triangle = new ol.geom.LineString([
/** ol.geom.LineString
 * Linestring geometry.(ol3 API)
 */
 [0, radius], [run, -rise], [-run, -rise], [0, radius]
]);
var feature = new ol.Feature(triangle);
/** ol.Feature
 * A vector object for geographic features with a geometry 
 * and other attribute properties, similar to the features 
 * in vector file formats like GeoJSON.
 * GeoJSONのようなベクトルファイル形式のフィーチャに類似した、
 * ジオメトリとその他の属性プロパティを持つ地物フィーチャのため
 * のベクトルオブジェクト。(ol3 API)
 */
var layer = new ol.layer.Vector({
/** ol.layer.Vector
 * Vector data that is rendered client-side.
 * クライアント側で描画されたベクタデータ。(ol3 API)
 */
 source: new ol.source.Vector({
 /*: ol.source.Vector 
  * Provides a source of features for vector layers.
  * ベクタレイヤのフィーチャのソースを提供します。(ol3 API)
  */
  features: [feature]
 })
});
var map = new ol.Map({
 layers: [layer],
 target: 'map',
 view: new ol.View({
  center: [0, 0],
  zoom: 1
 })
});
function makeFractal(depth) {
 var geometry = /** @type {ol.geom.LineString} */ (triangle.clone());
 /** @type 
  * 値のタイプ(型)の説明 - 式などで表示
  * (@use JSDoc[http://usejsdoc.org/]より)
  */
 /** clone()
  * Clone this feature. If the original feature has 
  * a geometry it is also cloned. The feature id is 
  * not set in the clone.
  * このフィーチャを複製します。元のフィーチャは、ジオメトリ
  * を有する場合にも複製します。フィーチャ ID は、クローンに
  * 設定されていません。(ol3 API)
  */
 var graph = coordsToGraph(geometry.getCoordinates());
 /** getCoordinates()
  * Returns: Coordinates.(ol3 API)
  */
 for (var i = 0; i < depth; ++i) {
  var node = graph;
  while (node.next) {
   var next = node.next;
   injectNodes(node);
   node = next;
  }
 }
 var coordinates = graphToCoords(graph);
 document.getElementById('count').innerHTML = coordinates.length;
 /** element.innerHTML
  * innerHTML は、与えられた要素に含まれる全てのマークアップ
  * と内容を設定または取得します。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * API/element.innerHTML])
  */
 geometry.setCoordinates(coordinates);
 /** setCoordinates()
  * setCoordinates(coordinates) [Type: ol.Coordinate, 
  * Description: Coordinates](ol3 API)
  */
 feature.setGeometry(geometry);
 /** setGeometry()
  * Set the geometry for this feature. This will 
  * update the property associated with the current 
  * geometry property name. By default, this is 
  * geometry but it can be changed by calling 
  * setGeometryName.
  * このフィーチャのジオメトリを設定します。これは、現在のジ
  * オメトリプロパティ名に関連付けられているプロパティを更新
  * します。デフォルトでは、ジオメトリですがそれは 
  * setGeometryName を呼び出すことによって変更できます。
  * (ol3 API)
  */
}
function injectNodes(startNode) {
 var endNode = startNode.next;

 var start = startNode.point;
 var end = startNode.next.point;
 var dx = end[0] - start[0];
 var dy = end[1] - start[1];

 // first point at 1/3 along the segment
 var firstNode = {
  point: [start[0] + dx / 3, start[1] + dy / 3]
 };

 // second point at peak of _/\_
 var r = Math.sqrt(dx * dx + dy * dy) / (2 * cos30);
 /** Math.sqrt()
  * 引数として与えた数の平方根を返します。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * JavaScript/Reference/Global_Objects/Math/sqrt])
  */
 var a = Math.atan2(dy, dx) + Math.PI / 6;
 /** Math.atan()
  * 引数として与えた数のアークタンジェントをラジアン単位で返
  * します。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * JavaScript/Reference/Global_Objects/Math/atan])
  */
 var secondNode = {
  point: [start[0] + r * Math.cos(a), start[1] + r * Math.sin(a)]
 };
 // third point at 2/3 along the segment
 var thirdNode = {
  point: [end[0] - dx / 3, end[1] - dy / 3]
 };

 startNode.next = firstNode;
 firstNode.next = secondNode;
 secondNode.next = thirdNode;
 thirdNode.next = endNode;
}

function coordsToGraph(coordinates) {
 var graph = {
  point: coordinates[0]
 };
 var length = coordinates.length;
 for (var level = 0, node = graph; level < length - 1; ++level) {
  node.next = {
   point: coordinates[level + 1]
  };
  node = node.next;
 }
 return graph;
}
function graphToCoords(graph) {
 var coordinates = [graph.point];
 for (var node = graph, i = 1; node.next; node = node.next, ++i) {
  coordinates[i] = node.next.point;
 }
 return coordinates;
}
var depthInput = document.getElementById('depth');
function update() {
 makeFractal(Number(depthInput.value));
}
var updateTimer;

/**
 * Regenerate fractal on depth change.  Change 
 * events are debounced so updates only occur 
 * every 200ms.
 * デプスチェンジによってフラクタルを再生します。アップデート
 * が200ミリ秒毎に生じるだけで、チェンジイベントがデバウンス
 * されます。
 */
depthInput.onchange = function() {
/** GlobalEventHandlers.onchange()
 * The onchange property sets and returns the event 
 * handler for the change event.
 * onchange プロパティは、change イベントに対してイベント
 * ハンドラを設定、および、返します。
 * (MDN[https://developer.mozilla.org/en-US/
 * docs/Web/API/GlobalEventHandlers/onchange])
 */
 window.clearTimeout(updateTimer);
 /** Window.clearTimeout(timeoutID)
  * window.setTimeout() によって設定された遅延を解除します。
  * timeoutID は、解除したいタイマの ID です。ID は、
  * window.setTimeout() の戻り値によって取得できます。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * API/WindowTimers/clearTimeout])
  */
 updateTimer = window.setTimeout(update, 200);
 /** setTimeout(func, dylay)
  * 指定された遅延の後に、コードの断片または関数を実行します。
  * func : delay ミリ秒後に実行したい関数。
  * delay : 関数呼び出しを遅延させるミリ秒(1/1000 秒)。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * API/Window/setTimeout])
  */
};

update();

2 - ol3.2ex 77a - Fractal example 1

「Fractal example (fractal.html)」を参考に地図を表示してみます。

HTML ファイルの作成
a Eclipse のメニューの「ファイル」->「ファイルを開く」をクリックします。





b 「ファイルを開く」ウィンドウで、「user」->「mapsite」->「ol3proj」->「v3.2.1」->「examples」->「fractal.html」をクリックして選択し、「OK」ボタンをクリックします。
同じように「fractal.js」を開きます。





c メニューの「ファイル」->「新規」 -> 「ファイル」をクリックします。



d 「ファイル」ウィンドウで「ol3proj」をクリックして選択し、「ファイル名」を「277-ol3ex.html」と入力し、「次へ」ボタンをクリックします。








e 「File Template」ウィンドウで「HTML 5 Template」をクリックして選択し、「OK」ボタンをクリックします。











f 「fractal.html」の内容をコピーして「277-ol3ex.html」に貼り付け、修正します。
g 同じように、新規に「277-ol3ex.js」ファイルを作成し、「File Template」ウィンドウで「JavaScript Template」をクリックして選択し、「完了」ボタンをクリックして、「fractal.js」の内容をコピーして貼り付け、修正します。「fractal-require.js」も「277-ol3ex-require.js」に貼り付けます。


「277-ol3ex.html」
<!doctype html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<!--
  <link rel="stylesheet" href="../css/ol.css" type="text/css">
  <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="../resources/layout.css" type="text/css">
  <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
-->
  <!-- ディレクトリ修正 -->
  <link rel="stylesheet" href="v3.2.1/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.2.1/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.2.1/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.2.1/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Fractal example</title>
  <style>
   .map {
    background: whitesmoke;
   }
   #depth {
    width: 100px;
   }
  </style>
 </head>
 <body>
  <!-- 
  bootstrap.min.css, bootstrap-responsive.min.css で設定されたセレクタを使用。
  -->
  <div class="navbar navbar-inverse navbar-fixed-top">
   <div class="navbar-inner">
    <div class="container">
     <!--
     <a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
     -->
     <!-- ディレクトリ修正 -->
     <a class="brand" href="v3.2.1/examples/"><img src="v3.2.1/resources/logo.png"> OpenLayers 3 Examples</a>
    </div>
   </div>
  </div>
  <div class="container-fluid">
   <div class="row-fluid">
    <div class="span12">
     <div id="map" class="map"></div>
    </div>
   </div>
   <div class="row-fluid">
    <div class="span12">
    <h4 id="title">Fractal example</h4>
     <p id="shortdesc">Example of a fractal.</p>
     <label for="depth">
      depth:&nbsp;
      <input id="depth" type="range" min="0" max="9" step="1" value="5">
      &nbsp;(<span id="count">#</span> points)
     </label>
     <div id="docs">
      <!--
      <p>See the <a href="fractal.js" target="_blank">fractal.js source</a> to see how this is done.</p>
      -->
      <!-- ファイル修正 -->
      <p>See the <a href="277-ol3ex.js" target="_blank">277-ol3ex.js source</a> to see how this is done.</p>
     </div>
     <div id="tags">fractal, vector</div>
    </div>
   </div>
  </div>
  <!--
  <script src="../resources/jquery.min.js" type="text/javascript"></script>
  <script src="../resources/example-behaviour.js" type="text/javascript"></script>
  -->
  <!-- ディレクトリ修正
   jQuery Minified版と
   example-behaviour.js(Examples用 JSコード[文字コードなど])
  -->
  <script src="v3.2.1/resources/jquery.min.js" type="text/javascript"></script>
  <script src="v3.2.1/resources/example-behaviour.js" type="text/javascript"></script>
  <!--
  <script src="loader.js?id=fractal" type="text/javascript"></script>
  -->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=277-ol3ex" type="text/javascript"></script>
 </body>
</html>