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

2015年3月19日木曜日

2 - ol3.3ex 86b - Bind HTML input example 2

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

「286-ol3ex.js」
function checkWebGL(evt) {
 document.getElementById('no-webgl').style.display =
  ol.has.WEBGL ? 'none' : '';
  /** ol.has.WEBGL{boolean}
   * True if browser supports WebGL.
   * ブラウザが WebGL をサポートしていたら true。(ol3 API)
   */
  /** 条件演算子 condition ? expr1 : expr2 
   * condition: true か false かを評価する条件文です。
   * expr1, expr2: 各々の値の場合に実行する式です。
   * condition が true の場合、演算子は expr1 の値を選択します。
   * そうでない場合は expr2 の値を選択します。
   * (MDN[https://developer.mozilla.org/ja/docs/Web/
   * JavaScript/Guide/Expressions_and_Operators])
   */
 document.getElementById('has-webgl').style.display =
  ol.has.WEBGL && !evt.glContext ? '' : 'none';
  /** glContext()
   * WebGL context. Only available when a WebGL renderer 
   * is used, null otherwise.
   * WebGLのコンテキスト。WebGL レンダラを使用する場合にだけ
   * 利用でき、そうでない場合は null。
   * (ol3 API[説明は Stable Only のチェックを外すと表示])
   */
 document.getElementById('webgl').style.display =
  evt.glContext ? '' : 'none';
}
var layer = new ol.layer.Tile({
 source: new ol.source.OSM()
 /** ol.source.OSM 
  * Layer source for the OpenStreetMap tile server.
  * OpenStreetMap タイルサーバのレイヤソース。(ol3 API)
  */
});
layer.once('precompose', checkWebGL);
/** once()
 * Listen once for a certain type of event.
 * あるタイプのイヴェントを1回リッスンします。
 * Return: Unigue key for the listener.(ol3 API)
 */
/** precompose イベント
 * レイヤを描画する前に発生するイベント。
 * (「Layer spy example」参照)
 */
var view = new ol.View({
 center: [0, 0],
 zoom: 2
});
var map = new ol.Map({
 layers: [layer],
 renderer: exampleNS.getRendererFromQueryString(),
 /** 'example-behavior.js' により URL にある renderer を返します */
 target: 'map',
 controls: ol.control.defaults({
 /** controls
  * Controls initially added to the map. 
  * If not specified, ol.control.defaults() is used.
  * 初期設定で、マップに追加されたコントロール。
  * 明示されていなければ、ol.control.defaults() が使用されます。
  * (ol3 API)
  */
 /** ol.control.defaults()
  * デフォルトでは、マップに含まコントロールのセット。
  * 特に設定しない限り、これは、以下の各コントロールの
  * インスタンスを含むコレクションを返します。(ol3 API)
  * ol.control.Zoom, ol.control.Rotate, ol.control.Attribution
  */
  attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
  /** @type 
   * 値のタイプ(型)の説明 - 式などで表示
   * (@use JSDoc[http://usejsdoc.org/]より)
   */
   collapsible: false // 折りたたみ
  })
 }),
 view: view
});
var visible = new ol.dom.Input(document.getElementById('visible'));
/** ol.dom.Input
 * Helper class for binding HTML input to an ol.Object.
 * ol.Object に HTML input要素を接続するためのヘルパークラス。
 * (ol3 API)
 */
visible.bindTo('checked', layer, 'visible');
/** bindTo 
 * The bindTo method allows you to set up a two-way 
 * binding between a `source` and `target` object. 
 * The method returns an object with a `transform` 
 * method that you can use to provide `from` and 
 * `to` functions to transform values on the way 
 * from the source to the target and on the way back.
 * bindTo メソッドは、`source` と ` target` オブジェク
 * ト間の結合を双方向に設定することができます。メソッドは、
 * ソースからターゲットに、および、その逆方向に値を変換
 * する、 `from` と ` to` ファンクションを提供するため
 * に使用することがでる `transform` メソッドをともなっ
 * た、オブジェクトを返します。
 * (ol3 API[説明は Stable Only のチェックを外すと表示])
 */
var opacity = new ol.dom.Input(document.getElementById('opacity'));
opacity.bindTo('value', layer, 'opacity')
 .transform(parseFloat, String);
 /** parseFloat()
  * 引数として与えられた文字列を解析し、浮動小数点数を返します。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * JavaScript/Reference/Global_Objects/parseFloat])
  */
var hue = new ol.dom.Input(document.getElementById('hue'));
hue.bindTo('value', layer, 'hue')
 .transform(parseFloat, String);
var saturation = new ol.dom.Input(document.getElementById('saturation'));
saturation.bindTo('value', layer, 'saturation')
 .transform(parseFloat, String);
var contrast = new ol.dom.Input(document.getElementById('contrast'));
contrast.bindTo('value', layer, 'contrast')
 .transform(parseFloat, String);
var brightness = new ol.dom.Input(document.getElementById('brightness'));
brightness.bindTo('value', layer, 'brightness')
 .transform(parseFloat, String);

var rotation = new ol.dom.Input(document.getElementById('rotation'));
rotation.bindTo('value', view, 'rotation').transform(parseFloat, String);
var resolution = new ol.dom.Input(document.getElementById('resolution'));
resolution.bindTo('value', view, 'resolution').transform(parseFloat, String);

2 - ol3.3ex 86a - Bind HTML input example 1

「Bind HTML input example (bind-input.html)」を参考に地図を表示してみます。

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





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





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



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








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











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


「286-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.3.0/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.3.0/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.3.0/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.3.0/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Bind HTML input example</title>
 </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.3.0/examples/"><img src="v3.3.0/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="span4">
    <h4 id="title">Bind HTML input example</h4>
     <p id="shortdesc">Demonstrates two-way binding of HTML 
     input elements to OpenLayers objects. </p>
     <div id="docs">
      <!--
      <p>See the <a href="bind-input.js" target="_blank">bind-input.js source</a> 
       to see how this is done.</p>
      -->
      <!-- ファイル修正 -->
      <p>See the <a href="286-ol3ex.js" target="_blank">286-ol3ex.js source</a> 
       to see how this is done.</p>
      <p id="has-webgl" style="display: none">With the 
       <a href="?renderer=webgl">WebGL renderer</a>,
       <strong>hue</strong>, 
       <strong>saturation</strong>, 
       <strong>contrast</strong> and 
       <strong>brightness</strong> can also be controlled.
       </p>
      <div id="no-webgl" class="alert alert-warning" style="display: none">
       <h4>Warning!</h4>
       A browser that supports 
       <a href="http://get.webgl.org/">WebGL</a> 
       is required to change the <strong>hue</strong>, 
       <strong>saturation</strong>, <strong>contrast</strong> 
       and <strong>brightness</strong>.
     </div>
     <div id="tags">input, bind, openstreetmap</div>
    </div>
    <div class="span4">
     <form class="">
      <fieldset>
       <legend>Layer</legend>
       <label class="checkbox" for="visible">
        <input id="visible" type="checkbox"/>visibility
       </label>
       <label>opacity</label>
       <input id="opacity" type="range" min="0" max="1" step="0.01"/>
      </fieldset>
      <fieldset id="webgl" style="display: none">
       <label>hue</label>
       <input id="hue" type="range" min="-3.141592653589793" max="3.141592653589793" step="0.01"/>
       <label>saturation</label>
       <input id="saturation" type="range" min="0" max="5" step="0.01"/>
       <label>contrast</label>
       <input id="contrast" type="range" min="0" max="2" step="0.01"/>
       <label>brightness</label>
       <input id="brightness" type="range" min="-1" max="1" step="0.01"/>
      </fieldset>
     </form>
    </div>
    <div class="span4">
     <form class="">
      <fieldset>
       <legend>View</legend>
       <label>rotation</label>
       <input id="rotation" type="range" min="-3.141592653589793" max="3.141592653589793" step="0.01"/>
       <label>resolution</label>
       <input id="resolution" type="number" min="0" step="250"/>
      </fieldset>
     </form>
    </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.3.0/resources/jquery.min.js" type="text/javascript"></script>
  <script src="v3.3.0/resources/example-behaviour.js" type="text/javascript"></script>
  <!--
  <script src="loader.js?id=bind-input" type="text/javascript"></script>
  -->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=286-ol3ex" type="text/javascript"></script>
 </body>
</html>