1 /**
  2  * Hilo
  3  * Copyright 2015 alibaba.com
  4  * Licensed under the MIT License
  5  */
  6 
  7 /**
  8  * <iframe src='../../../examples/Bitmap.html?noHeader' width = '300' height = '200' scrolling='no'></iframe>
  9  * <br/>
 10  * 使用示例:
 11  * <pre>
 12  * var bmp = new Hilo.Bitmap({image:imgElem, rect:[0, 0, 100, 100]});
 13  * stage.addChild(bmp);
 14  * </pre>
 15  * @class Bitmap类表示位图图像类。
 16  * @augments View
 17  * @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。此外还包括:
 18  * <ul>
 19  * <li><b>image</b> - 位图所在的图像image。必需。</li>
 20  * <li><b>rect</b> - 位图在图像image中矩形区域。可选。</li>
 21  * <li><b>crossOrigin</b> - 是否设置crossOrigin。默认否。</li>
 22  * </ul>
 23  * @module hilo/view/Bitmap
 24  * @requires hilo/core/Hilo
 25  * @requires hilo/core/Class
 26  * @requires hilo/view/View
 27  * @requires hilo/view/Drawable
 28  */
 29  var Bitmap = Class.create(/** @lends Bitmap.prototype */{
 30     Extends: View,
 31     constructor: function(properties){
 32         properties = properties || {};
 33         this.id = this.id || properties.id || Hilo.getUid("Bitmap");
 34         Bitmap.superclass.constructor.call(this, properties);
 35 
 36         this.drawable = new Drawable(properties);
 37 
 38         //init width and height
 39         if(!this.width || !this.height){
 40             var rect = this.drawable.rect;
 41             if(rect){
 42                 this.width = rect[2];
 43                 this.height = rect[3];
 44             }
 45         }
 46     },
 47 
 48     /**
 49      * 设置位图的图片。
 50      * @param {Image|String} image 图片对象或地址。
 51      * @param {Array} rect 指定位图在图片image的矩形区域。可选。
 52      * @param {Boolean} crossOrigin 是否设置 crossOrigin,默认否。
 53      * @returns {Bitmap} 位图本身。
 54      */
 55     setImage: function(image, rect, crossOrigin){
 56         this.drawable.init({image:image, rect:rect, crossOrigin:crossOrigin});
 57         if(rect){
 58             this.width = rect[2];
 59             this.height = rect[3];
 60         }
 61         else if(!this.width && !this.height){
 62             rect = this.drawable.rect;
 63             if(rect){
 64                 this.width = rect[2];
 65                 this.height = rect[3];
 66             }
 67         }
 68         return this;
 69     }
 70  });
 71