BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

image.js 1.6 KiB Download Raw
1class image{
2 subImages = null;
3 subImagesPerRow = null;
4
5 g_image = null;
6
7 animationCycles = {};
8
9 constructor(image, subImgTotal = 1, perRow = 1)
10 {
11 subImgTotal = subImgTotal || 1;
12 perRow = perRow || 1;
13
14 this.subImages = subImgTotal;
15 this.subImagesPerRow = perRow;
16 this.g_image = image;
17
18 image.id="loadedImage";
19 document.body.append(image);
20 console.log("Image loaded: " + image.src);
21 }
22
23 draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
24 {
25 var shouldDraw = true;
26
27 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
28 shouldDraw = false;
29
30 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
31 shouldDraw = false;
32
33 if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
34 shouldDraw = false;
35
36 if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
37 shouldDraw = false;
38
39 if(shouldDraw)
40 {
41 var context = scene.context;
42 context.save();
43 var oldAlpha = context.globalAlpha;
44 context.globalAlpha = alpha;
45
46 context.translate(dx, dy);
47 context.rotate(rotation * (Math.PI/180));
48
49 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
50
51 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
52
53 context.restore();
54 context.globalAlpha = oldAlpha;
55 }
56 }
57
58 addAnimationCycle(label, cycle)
59 {
60 this.animationCycles[label] = cycle;
61 }
62
63 get width()
64 {
65 return this.g_image.width / this.subImagesPerRow;
66 }
67
68 get height()
69 {
70 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
71 }
72}