add image method to see if animation set exists
@@ -1,113 +1,118 @@
1
class image
2
{
3
subImages = null;
4
subImagesPerRow = null;
5
loop = false;
6
chain = null;
7
8
g_image = null;
9
10
animationCycles = {};
11
12
constructor (image, subImgTotal = 1, perRow = 1, loop = false, chain = null)
13
{
14
subImgTotal = subImgTotal || 1;
15
perRow = perRow || 1;
16
17
this.subImages = subImgTotal;
18
this.subImagesPerRow = perRow;
19
this.g_image = image;
20
this.loop = loop;
21
this.chain = chain;
22
23
this.cacheWidth = -1;
24
this.cacheHeight = -1;
25
26
image.id = "loadedImage";
27
document.body.append(image);
28
console.log("Image loaded: " + image.src);
29
}
30
31
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
32
{
33
var shouldDraw = true;
34
35
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
36
shouldDraw = false;
37
38
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
39
shouldDraw = false;
40
41
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
42
shouldDraw = false;
43
44
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
45
shouldDraw = false;
46
47
if (shouldDraw)
48
{
49
var context = scene.context;
50
context.save();
51
var oldAlpha = context.globalAlpha;
52
context.globalAlpha = alpha;
53
54
context.translate(dx, dy);
55
context.rotate(rotation * (Math.PI / 180));
56
57
(mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);
58
59
var subImageData = this.getSubImage(subImage);
60
61
context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);
62
63
context.restore();
64
context.globalAlpha = oldAlpha;
65
}
66
}
67
68
addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
69
{
70
this.animationCycles[label] =
71
{
72
frames: cycle,
73
loop: loop,
74
chain: chain,
75
chainDefaultIndex: chainDefaultIndex,
76
chainSpeed: chainSpeed
77
};
78
}
79
80
get width ()
81
{
82
if (this.cacheWidth < 0)
83
this.cacheWidth = this.g_image.width / this.subImagesPerRow;
84
85
return this.cacheWidth;
86
}
87
88
get height ()
89
{
90
if (this.cacheHeight < 0)
91
this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
92
93
return this.cacheHeight;
94
}
95
96
cacheWidth = -1;
97
cacheHeight = -1;
98
99
getSubImage (subImageIndex)
100
{
101
var subImage = Math.round(subImageIndex);
102
subImage %= this.subImages;
103
104
var c = subImage % this.subImagesPerRow;
105
var r = Math.floor(subImage / this.subImagesPerRow);
106
var sw = (this.g_image.width / this.subImagesPerRow);
107
var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
108
var sx = sw * c;
109
var sy = sh * r;
110
111
return { sw: sw, sh: sh, sx: sx, sy: sy };
112
}
1
class image
2
{
3
subImages = null;
4
subImagesPerRow = null;
5
loop = false;
6
chain = null;
7
8
g_image = null;
9
10
animationCycles = {};
11
12
constructor (image, subImgTotal = 1, perRow = 1, loop = false, chain = null)
13
{
14
subImgTotal = subImgTotal || 1;
15
perRow = perRow || 1;
16
17
this.subImages = subImgTotal;
18
this.subImagesPerRow = perRow;
19
this.g_image = image;
20
this.loop = loop;
21
this.chain = chain;
22
23
this.cacheWidth = -1;
24
this.cacheHeight = -1;
25
26
image.id = "loadedImage";
27
document.body.append(image);
28
console.log("Image loaded: " + image.src);
29
}
30
31
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
32
{
33
var shouldDraw = true;
34
35
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
36
shouldDraw = false;
37
38
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
39
shouldDraw = false;
40
41
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
42
shouldDraw = false;
43
44
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
45
shouldDraw = false;
46
47
if (shouldDraw)
48
{
49
var context = scene.context;
50
context.save();
51
var oldAlpha = context.globalAlpha;
52
context.globalAlpha = alpha;
53
54
context.translate(dx, dy);
55
context.rotate(rotation * (Math.PI / 180));
56
57
(mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);
58
59
var subImageData = this.getSubImage(subImage);
60
61
context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);
62
63
context.restore();
64
context.globalAlpha = oldAlpha;
65
}
66
}
67
68
hasAnimationCycle (label)
69
{
70
return this.animationCycles[label] != null;
71
}
72
73
addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
74
{
75
this.animationCycles[label] =
76
{
77
frames: cycle,
78
loop: loop,
79
chain: chain,
80
chainDefaultIndex: chainDefaultIndex,
81
chainSpeed: chainSpeed
82
};
83
}
84
85
get width ()
86
{
87
if (this.cacheWidth < 0)
88
this.cacheWidth = this.g_image.width / this.subImagesPerRow;
89
90
return this.cacheWidth;
91
}
92
93
get height ()
94
{
95
if (this.cacheHeight < 0)
96
this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
97
98
return this.cacheHeight;
99
}
100
101
cacheWidth = -1;
102
cacheHeight = -1;
103
104
getSubImage (subImageIndex)
105
{
106
var subImage = Math.round(subImageIndex);
107
subImage %= this.subImages;
108
109
var c = subImage % this.subImagesPerRow;
110
var r = Math.floor(subImage / this.subImagesPerRow);
111
var sw = (this.g_image.width / this.subImagesPerRow);
112
var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
113
var sx = sw * c;
114
var sy = sh * r;
115
116
return { sw: sw, sh: sh, sx: sx, sy: sy };
117
}
113
118
}
@@ -1,113 +1,118 @@1
class image1
class image
2
{2
{
3
subImages = null;3
subImages = null;
4
subImagesPerRow = null;4
subImagesPerRow = null;
5
loop = false;5
loop = false;
6
chain = null;6
chain = null;
7
7
8
g_image = null;8
g_image = null;
9
9
10
animationCycles = {};10
animationCycles = {};
11
11
12
constructor (image, subImgTotal = 1, perRow = 1, loop = false, chain = null)12
constructor (image, subImgTotal = 1, perRow = 1, loop = false, chain = null)
13
{13
{
14
subImgTotal = subImgTotal || 1;14
subImgTotal = subImgTotal || 1;
15
perRow = perRow || 1;15
perRow = perRow || 1;
16
16
17
this.subImages = subImgTotal;17
this.subImages = subImgTotal;
18
this.subImagesPerRow = perRow;18
this.subImagesPerRow = perRow;
19
this.g_image = image;19
this.g_image = image;
20
this.loop = loop;20
this.loop = loop;
21
this.chain = chain;21
this.chain = chain;
22
22
23
this.cacheWidth = -1;23
this.cacheWidth = -1;
24
this.cacheHeight = -1;24
this.cacheHeight = -1;
25
25
26
image.id = "loadedImage";26
image.id = "loadedImage";
27
document.body.append(image);27
document.body.append(image);
28
console.log("Image loaded: " + image.src);28
console.log("Image loaded: " + image.src);
29
}29
}
30
30
31
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)31
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
32
{32
{
33
var shouldDraw = true;33
var shouldDraw = true;
34
34
35
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))35
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
36
shouldDraw = false;36
shouldDraw = false;
37
37
38
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))38
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
39
shouldDraw = false;39
shouldDraw = false;
40
40
41
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))41
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
42
shouldDraw = false;42
shouldDraw = false;
43
43
44
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))44
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
45
shouldDraw = false;45
shouldDraw = false;
46
46
47
if (shouldDraw)47
if (shouldDraw)
48
{48
{
49
var context = scene.context;49
var context = scene.context;
50
context.save();50
context.save();
51
var oldAlpha = context.globalAlpha;51
var oldAlpha = context.globalAlpha;
52
context.globalAlpha = alpha;52
context.globalAlpha = alpha;
53
53
54
context.translate(dx, dy);54
context.translate(dx, dy);
55
context.rotate(rotation * (Math.PI / 180));55
context.rotate(rotation * (Math.PI / 180));
56
56
57
(mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);57
(mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);
58
58
59
var subImageData = this.getSubImage(subImage);59
var subImageData = this.getSubImage(subImage);
60
60
61
context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);61
context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);
62
62
63
context.restore();63
context.restore();
64
context.globalAlpha = oldAlpha;64
context.globalAlpha = oldAlpha;
65
}65
}
66
}66
}
67
67
68
addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed) 68
hasAnimationCycle (label)
69
{69
{
70
this.animationCycles[label] =70
return this.animationCycles[label] != null;
71
{71
}
72
frames: cycle,72
73
loop: loop,73
addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
74
chain: chain,74
{
75
chainDefaultIndex: chainDefaultIndex,75
this.animationCycles[label] =
76
chainSpeed: chainSpeed76
{
77
};77
frames: cycle,
78
}78
loop: loop,
79
79
chain: chain,
80
get width () 80
chainDefaultIndex: chainDefaultIndex,
81
{81
chainSpeed: chainSpeed
82
if (this.cacheWidth < 0)82
};
83
this.cacheWidth = this.g_image.width / this.subImagesPerRow;83
}
84
84
85
return this.cacheWidth;85
get width ()
86
}86
{
87
87
if (this.cacheWidth < 0)
88
get height () 88
this.cacheWidth = this.g_image.width / this.subImagesPerRow;
89
{89
90
if (this.cacheHeight < 0)90
return this.cacheWidth;
91
this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);91
}
92
92
93
return this.cacheHeight;93
get height ()
94
}94
{
95
95
if (this.cacheHeight < 0)
96
cacheWidth = -1;96
this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
97
cacheHeight = -1;97
98
98
return this.cacheHeight;
99
getSubImage (subImageIndex) 99
}
100
{100
101
var subImage = Math.round(subImageIndex);101
cacheWidth = -1;
102
subImage %= this.subImages;102
cacheHeight = -1;
103
103
104
var c = subImage % this.subImagesPerRow;104
getSubImage (subImageIndex)
105
var r = Math.floor(subImage / this.subImagesPerRow);105
{
106
var sw = (this.g_image.width / this.subImagesPerRow);106
var subImage = Math.round(subImageIndex);
107
var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));107
subImage %= this.subImages;
108
var sx = sw * c;108
109
var sy = sh * r;109
var c = subImage % this.subImagesPerRow;
110
110
var r = Math.floor(subImage / this.subImagesPerRow);
111
return { sw: sw, sh: sh, sx: sx, sy: sy };111
var sw = (this.g_image.width / this.subImagesPerRow);
112
}112
var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
113
var sx = sw * c;
114
var sy = sh * r;
115
116
return { sw: sw, sh: sh, sx: sx, sy: sy };
117
}
113
}118
}