-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.js
94 lines (72 loc) · 2.04 KB
/
image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(function(CL){
var RenderLayer = CL.RenderLayer;
var CanvasUtil = CL.CanvasUtil;
//图片对象
function CanvasImage(opt){
if(!(this instanceof CanvasImage)){
return new CanvasImage(opt);
}
this.init(opt);
};
CanvasImage.prototype = Object.create(RenderLayer.prototype);
CanvasImage.prototype.constructor = CanvasImage;
$.extend(CanvasImage.prototype,{
init:function(opt){
RenderLayer.prototype.init.apply(this,arguments);
this.url = opt.url;
//初始化图片对象
this.imageElement = $('<img>');
this.bind();
this.setUrl(this.url);
},
bind:function(){
var self = this;
this.imageElement.on('load',function(e){
var element = e.target;
var finalStyle = self.finalStyle;
//图片是否已加载
self.isLoaded = true;
self.naturalWidth = element.naturalWidth;
self.naturalHeight = element.naturalHeight;
if(typeof self.width == 'undefined'){
finalStyle.width = self.naturalWidth;
}
if(typeof self.height == 'undefined'){
finalStyle.height = self.naturalHeight;
}
//删除根元素的缓存canvas,以便更新缓存
self.updateParentCacheableLayer();
//初始化样式
self.initializeStyle();
//触发缓存canvas更新
var root = self.getRoot();
//让根元素重新绘制
if(root){
root.run();
}
});
},
setUrl:function(url){
this.url = url;
this.isLoaded = false;
this.imageElement.prop('src',url);
},
drawSelf:function(ctx){
//已加载图片才允许绘制
if(this.isLoaded){
var width = this.drawWidth;
var height = this.drawHeight;
var borderRadius = this.finalStyle.borderRadius;
//圆角图片裁剪
if(borderRadius){
CanvasUtil.setBorderRadiusContext(ctx,0,0,width,height,borderRadius);
ctx.clip();
}
ctx.drawImage(this.imageElement[0],0,0,this.naturalWidth,this.naturalHeight,0,0,Math.round(width),Math.round(height));
}
},
onClick:function(){
}
});
CL.CanvasImage = CanvasImage;
})(window.CanvasList = window.CanvasList || {});