-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.js
More file actions
49 lines (44 loc) · 1.38 KB
/
Copy pathsprite.js
File metadata and controls
49 lines (44 loc) · 1.38 KB
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
var sprite = function(filename, isPattern){
this.image = null;
this.pattern = null;
this.TO_RADIAN = Math.PI/180;
if(filename != undefined && filename != null && filename != ""){
this.image = new Image();
this.image.src = filename;
this.image.addEventListener("load",function(){
console.log('Image:'+filename+' loaded.');
});
this.image.addEventListener("error",function(){
console.log('Image:'+filename+' could not be loaded.');
});
if(isPattern){
this.pattern = Context.context.createPattern(this.image,'repeat');
}
}else{
console.log("sprite image not loaded, filename:"+filename);
}
this.draw = function(x,y,w,h){
if(this.pattern != null){
Context.context.fillStyle = this.pattern;
Context.context.fillRect(x,y,w,h);
}else{
if(w == undefined || h == undefined){
Context.context.drawImage(this.image,x,y,this.image.width,this.image.height);
}else{
Context.context.drawImage(this.image,x,y,w,h);
}
}
};
this.rotate = function(x,y,angle){
//save context
Context.context.save();
//move the context to the coords of picture
Context.context.translate(x,y);
//rotate context
Context.context.rotate(angle * this.TO_RADIAN);
//Draw image on the context such that center of the image is at the origin
Context.context.drawImage(this.image,-(this.image.width/2),-(this.image.height/2));
//reverse translation and rotation
Context.context.restore();
};
};