Hilo/docs/api-en/code/view/Drawable.js
2019-04-17 15:53:46 +08:00

71 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Hilo
* Copyright 2015 alibaba.com
* Licensed under the MIT License
*/
/**
* @class Drawable is a wrapper of drawable images.
* @param {Object} properties create Objects properties, contains all writable properties.
* @module hilo/view/Drawable
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Object} image Image to be drawed, can used by CanvasRenderingContext2D.drawImagelike HTMLImageElement、HTMLCanvasElement、HTMLVideoElement。
* @property {array} rect The retangle area that image will be drawed.
*/
var Drawable = Class.create(/** @lends Drawable.prototype */{
constructor: function(properties){
this.init(properties);
},
image: null,
rect: null,
/**
* Initialize drawable elements.
* @param {Object} properties Properties need to be initialized.
*/
init: function(properties){
var me = this, oldImage = me.image;
if(Drawable.isDrawable(properties)){
me.image = properties;
}else{
util.copy(me, properties, true);
}
var image = me.image;
if(typeof image === 'string'){
if(oldImage && image === oldImage.getAttribute('src')){
image = me.image = oldImage;
}else{
me.image = null;
//load image dynamically
var img = new Image();
if(properties.crossOrigin){
img.crossOrigin = "Anonymous";
}
img.onload = function(){
img.onload = null;
me.init(img);
};
img.src = image;
return;
}
}
if(image && !me.rect) me.rect = [0, 0, image.width, image.height];
},
Statics: /** @lends Drawable */{
/**
* Check whether the given 'elem' and be wrapped into Drawable object.
* @param {Object} elem Element to be tested.
* @return {Boolean} Return true if element can be wrapped into Drawable element, otherwises return false.
*/
isDrawable: function(elem){
if(!elem || !elem.tagName) return false;
var tagName = elem.tagName.toLowerCase();
return tagName === "img" || tagName === "canvas" || tagName === "video";
}
}
});