File: libs/FacePhoto.js
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* Created by anlun on 16/8/14.
*/
/**
* @module annieUI
*/
var annieUI;
(function (annieUI) {
var Sprite = annie.Sprite;
/**
* 有时我们需要从外部获取一张个人头像,将它变成方形或者圆形展示出来。
* 又希望他能按照我们的尺寸展示,这个时候你就需要用到FacePhoto类啦。
* @class annieUI.FacePhoto
* @public
* @extends annie.Sprite
* @since 1.0.0
*/
var FacePhoto = (function (_super) {
__extends(FacePhoto, _super);
/**
* 构造函数
* @method FacePhoto
* @since 1.0.0
* @public
* @example
* var circleface = new annieUI.FacePhoto(),
* rectFace=new annieUI.FacePhoto();
* //圆形头像
* circleface.init('http://test.annie2x.com/biglong/logo.jpg', 100, 0);
* circleface.x = 260;
* circleface.y = 100;
* s.addChild(circleface);
* //方形头像
* rectFace.init('http://test.annie2x.com/biglong/logo.jpg', 200, 1);
* rectFace.x = 260;
* rectFace.y = 400;
* s.addChild(rectFace);
*/
function FacePhoto() {
var _this = _super.call(this) || this;
_this.maskType = 0;
var s = _this;
s._instanceType = "annieUI.FacePhoto";
s.photo = new Image();
s.bitmap = new annie.Bitmap();
s.maskObj = new annie.Shape();
s.photo.onload = function (e) {
s.bitmap.bitmapData = s.photo;
s.maskObj.clear();
s.maskObj.beginFill("#000000");
var scale = s.radio / (s.photo.width < s.photo.height ? s.photo.width : s.photo.height);
s.bitmap.scaleX = s.bitmap.scaleY = scale;
s.bitmap.x = (s.radio - s.photo.width * scale) >> 1;
s.bitmap.y = (s.radio - s.photo.height * scale) >> 1;
if (s.maskType == 0) {
s.maskObj.drawCircle(s.radio >> 1, s.radio >> 1, s.radio >> 1);
}
else {
s.maskObj.drawRect(0, 0, s.radio, s.radio);
}
s.maskObj.endFill();
s.dispatchEvent("onComplete");
};
s.addChild(s.bitmap);
s.bitmap.mask = s.maskObj;
return _this;
}
/**
* 被始化头像,可反复调用设置不同的遮罩类型或者不同的头像地址
* @method init
* @param {string} src 头像的地址
* @param {number} radio 指定头像的长宽或者直径
* @param {number} maskType 遮罩类型,是圆形遮罩还是方形遮罩 0 圆形 1方形 默认是0
*/
FacePhoto.prototype.init = function (src, radio, maskType) {
if (radio === void 0) { radio = 0; }
if (maskType === void 0) { maskType = 0; }
var s = this;
s.radio = radio;
if (s.photo.src != src)
s.photo.src = src;
if (s.maskType != maskType)
s.maskType = maskType;
};
return FacePhoto;
}(Sprite));
annieUI.FacePhoto = FacePhoto;
})(annieUI || (annieUI = {}));