forked from robterrell/cocos2d-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture2D.js
94 lines (80 loc) · 2.09 KB
/
Texture2D.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
'use strict'
var util = require('util'),
events = require('events'),
RemoteResource = require('remote_resources').RemoteResource
/**
* @class
*
* @memberOf cocos
*
* @opt {String} [file] The file path of the image to use as a texture
* @opt {Texture2D|HTMLImageElement} [data] Image data to read from
*/
function Texture2D (opts) {
var file = opts.file,
data = opts.data,
texture = opts.texture
if (file) {
this.name = file
data = resource(file)
} else if (texture) {
this.name = texture.name
data = texture.imgElement
}
this.size = {width: 0, height: 0}
if (data instanceof RemoteResource) {
events.addListenerOnce(data, 'load', this.dataDidLoad.bind(this))
this.imgElement = data.load()
} else {
this.imgElement = data
this.dataDidLoad(data)
}
}
Texture2D.inherit(Object, /** @lends cocos.Texture2D# */ {
imgElement: null,
size: null,
name: null,
isLoaded: false,
dataDidLoad: function (data) {
this.isLoaded = true
this.size = {width: this.imgElement.width, height: this.imgElement.height}
events.trigger(this, 'load', this)
},
drawAtPoint: function (ctx, point) {
if (!this.isLoaded) {
return
}
ctx.drawImage(this.imgElement, point.x, point.y)
},
drawInRect: function (ctx, rect) {
if (!this.isLoaded) {
return
}
ctx.drawImage(this.imgElement,
rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height
)
},
/**
* @getter data
* @type {String} Base64 encoded image data
*/
get data () {
return this.imgElement ? this.imgElement.src : null
},
/**
* @getter contentSize
* @type {geometry.Size} Size of the texture
*/
get contentSize () {
return this.size
},
get pixelsWide () {
return this.size.width
},
get pixelsHigh () {
return this.size.height
}
})
exports.Texture2D = Texture2D
// vim:et:st=4:fdm=marker:fdl=0:fdc=1