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

2015年4月1日水曜日

2 - ol3.4ex 102b - Image load events example 2

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

「2102-ol3ex.js」
/**
 * Renders a progress bar.
 * @param {Element} el The target element.
 * @constructor
 */
/** 「@param」
 * The @param tag provides the name, type, and 
 * description of a function parameter.
 * The @param tag requires you to specify the name of 
 * the parameter you are documenting. You can also 
 * include the parameter's type, enclosed in curly 
 * brackets, and a description of the parameter.
 * @paramタグは、関数パラメータの名前と型、説明を提供します。
 * @paramタグを使用すると、文書化されたパラメータの名前を
 * 指定する必要があります。また、パラメータのタイプと、中括弧
 * で囲まれたおよびパラメータの説明を含めることができます。
 * (@use JSDoc [http://usejsdoc.org/tags-param.html])
 */
/** 「@constructor(@class と同義)」
 * The @class tag marks a function as being a 
 * constructor, meant to be called with the new 
 * keyword to return an instance.
 * @classタグは、インスタンスを返すために、新しいキーワードで
 * 呼び出されることを意図し、コンストラクタとして function を
 * マークします。
 * (@use JSDoc [http://usejsdoc.org/tags-class.html])
 */
function Progress(el) {
 this.el = el;
 this.loading = 0;
 this.loaded = 0;
}

/**
 * Increment the count of loading tiles.
 * ローディングするタイルのカウントをインクリメント(増や)
 * します。
 */
Progress.prototype.addLoading = function() {
/** Object.prototype
 * The Object.prototype property represents the 
 * Object prototype object.
 * Object.prototype プロパティは、Object プロパティ
 * オブジェクトを表します。
 * (MDN[https://developer.mozilla.org/en-US/docs/Web/
 * JavaScript/Reference/Global_Objects/Object/prototype])
 */
 if (this.loading === 0) {
  this.show();
 }
 ++this.loading;
 this.update();
};

/**
 * Increment the count of loaded tiles.
 * ローディングしたタイルのカウントをインクリメント(増や)
 * します。
 */
Progress.prototype.addLoaded = function() {
 setTimeout(function() {
 /** setTimeout(func, dylay)
  * 指定された遅延の後に、コードの断片または関数を実行します。
  * func : delay ミリ秒後に実行したい関数。
  * delay : 関数呼び出しを遅延させるミリ秒(1/1000 秒)。
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * API/Window/setTimeout])
  */
  ++this.loaded;
  this.update();
 }.bind(this), 100);
 /** Function.prototype.bind()
  * bind() メソッドは、呼び出された時に新しい関数を生成しま
  * す。最初の引数  thisArg は新しい関数の this キーワード
  * にセットされます。2 個目以降の引数は、新しい関数より前に、
  * ターゲット関数の引数として与えられます。
  * (下記ページ内「setTimeout とともに」も参照してください。)
  * (MDN[https://developer.mozilla.org/ja/docs/Web/
  * JavaScript/Reference/Global_Objects/Function/bind])
  */
};

/**
 * Update the progress bar.
 * プログレスバーを更新します。
 */
Progress.prototype.update = function() {
 var width = (this.loaded / this.loading * 100).toFixed(1) + '%';
 /** Number.prototype.toFixed()
  * The toFixed() method formats a number using 
  * fixed-point notation.
  * toFixed() メソッドは、固定小数点表記を使用して数値を
  * フォーマットします。
  * (MDN[https://developer.mozilla.org/en-US/docs/Web/
  * JavaScript/Reference/Global_Objects/Number/toFixed])
  */
 this.el.style.width = width;
 if (this.loading === this.loaded) {
  this.loading = 0;
  this.loaded = 0;
  setTimeout(this.hide.bind(this), 500);
 }
};

/**
 * Show the progress bar.
 * プログレスバーを表示します。
 */
Progress.prototype.show = function() {
 this.el.style.visibility = 'visible';
};

/**
 * Hide the progress bar.
 * プログレスバーを非表示にします。
 */
Progress.prototype.hide = function() {
 if (this.loading === this.loaded) {
  this.el.style.visibility = 'hidden';
  this.el.style.width = 0;
 }
};
var progress = new Progress(document.getElementById('progress'));
var source = new ol.source.ImageWMS({
/** ol.source.ImageWMS
 * Source for WMS servers providing single, untiled 
 * images.
 * 単一の、タイル状でない画像を提供する WMS サーバソース。
 * (ol3 API)
 */
 url: 'http://demo.boundlessgeo.com/geoserver/wms',
 params: {'LAYERS': 'topp:states'},
 serverType: 'geoserver'
});

source.on('imageloadstart', function(event) {
 progress.addLoading();
});
source.on('imageloadend', function(event) {
/** on()
 * Listen for a certain type of event.
 * Return:     Unique key for the listener.
 * (ol3 API)
 */
 progress.addLoaded();
});
source.on('imageloaderror', function(event) {
 progress.addLoaded();
});
var map = new ol.Map({
 logo: false,
 layers: [
  new ol.layer.Image({source: source})
 ],
 renderer: exampleNS.getRendererFromQueryString(),
 // 'example-behavior.js' により URL にある renderer を返します
 target: 'map',
 view: new ol.View({
  center: [-10997148, 4569099],
  zoom: 4
 })
});

2 - ol3.4ex 102a - Image load events example 1

「Image load events example (image-load-events.html)」を参考に地図を表示してみます。
説明に次のようにあります。

Example using image load events.
Image sources fire events related to image loading. You can listen for imageloadstart, imageloadend, and imageloaderror type events to monitor image loading progress. This example registers listeners for these events and renders an image loading progress bar at the bottom of the map.

画像のロードイベントを使用する例。
画像ソースは、画像の読み込みに関連するイベントを発生します。画像の読み込みの進行状況を監視する imageloadstart、および、imageloadend、imageloaderrorタイプのイベントをリッスンすることができます。この例では、これらのイベントのリスナを登録し、マップの下部にあるイメージロードプログレスバーをレンダリングします。

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





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





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



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








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











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

「2102-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.4.0/css/ol.css" type="text/css">
  <link rel="stylesheet" href="v3.4.0/resources/bootstrap/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="v3.4.0/resources/layout.css" type="text/css">
  <link rel="stylesheet" href="v3.4.0/resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
  <title>Image load events example</title>
  <style>
   .map {
    background: #E0ECED;
   }
   .wrapper {
    position: relative;
   }
   #progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 2px;
    background: rgba(0, 60, 136, 0.4);
    width: 0;
    transition: width 250ms;
   }
  </style>
 </head>
 <body>
  <!-- 
  bootstrap.min.css, bootstrap-responsive.min.css 
   Examples用 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.4.0/examples/"><img src="v3.4.0/resources/logo.png"> OpenLayers 3 Examples</a>
    </div>
   </div>
  </div>
  <div class="container-fluid">
   <div class="row-fluid">
    <div class="span12 wrapper">
     <div id="map" class="map"></div>
     <div id="progress"></div>
    </div>
   </div>
   <div class="row-fluid">
    <div class="span12">
     <h4 id="title">Image load events example</h4>
     <p id="shortdesc">Example using image load events. </p>
     <div id="docs">
      <p>Image sources fire events related to image loading. 
       You can listen for <code>imageloadstart</code>, 
       <code>imageloadend</code>,and 
       <code>imageloaderror</code> type events to 
       monitor image loading progress.  This example 
       registers listeners for these events and renders an 
       image loading progress bar at the bottom of the map.
      </p>
      <!--
      <p>See the <a href="image-load-events.js" target="_blank">image-load-events.js source</a> for more detail on how this is done.</p>
      -->
      <!-- ファイル修正 -->
      <p>See the <a href="2102-ol3ex.js" target="_blank">2102-ol3ex.js source</a> for more detail on how this is done.</p>
     </div>
     <div id="tags">image, events, loading</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.4.0/resources/jquery.min.js" type="text/javascript"></script>
  <script src="v3.4.0/resources/example-behaviour.js" type="text/javascript"></script>
  <!--
  <script src="loader.js?id=image-load-events" type="text/javascript"></script>
  -->
  <!-- ファイル修正 -->  <!-- ディレクトリ修正 -->
  <script src="loader.js?id=2102-ol3ex" type="text/javascript"></script>
  </body>
</html>