wgge 0.1.23 → 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@ export default class ImageHelper {
12
12
  * @param {Number} max
13
13
  * @returns {number}
14
14
  */
15
- static sanitizeZoom(size, viewBoxSize, zoom = 0.0, min = 0, max = Number.MAX_VALUE) {
15
+ static sanitizeZoom(size, viewBoxSize, zoom = 0.0, min = 0.0, max = Number.MAX_VALUE) {
16
16
  if (size.size() === 0) return 1;
17
17
 
18
18
  const minZoomX = viewBoxSize.x / size.x;
@@ -92,4 +92,21 @@ export default class CanvasRenderer extends RendererBase {
92
92
  drawCircle(center, radius, fill, stroke) {
93
93
  this.drawArc(center, radius, fill, stroke);
94
94
  }
95
+
96
+ drawImage(image, drawStart, drawEnd, imageStart, imageEnd, opacity = 1, clear = true) {
97
+ if (!(image && this.context2d)) return;
98
+ if (clear) this.context2d.clearRect(drawStart.x, drawStart.y, drawEnd.x, drawEnd.y);
99
+ this.context2d.globalAlpha = opacity;
100
+ this.context2d.drawImage(
101
+ image,
102
+ imageStart.x,
103
+ imageStart.y,
104
+ imageEnd.x,
105
+ imageEnd.y,
106
+ drawStart.x,
107
+ drawStart.y,
108
+ drawEnd.x,
109
+ drawEnd.y
110
+ );
111
+ }
95
112
  }
@@ -0,0 +1,58 @@
1
+ import Vector2 from "../../model/vector/Vector2";
2
+ import CanvasRenderer from "./CanvasRenderer";
3
+
4
+ export default class ImageCanvasRenderer extends CanvasRenderer {
5
+
6
+ /**
7
+ * @type ImageViewModel
8
+ */
9
+ model;
10
+
11
+ constructor(game, model, canvas) {
12
+ super(game, model, canvas);
13
+
14
+ this.model = model;
15
+ this.image = null;
16
+
17
+ }
18
+
19
+ activateInternal() {
20
+ this.reloadImage();
21
+ }
22
+
23
+ deactivateInternal() {
24
+ this.image = null;
25
+ }
26
+
27
+ renderInternal() {
28
+ if (this.model.image.uri.isDirty) {
29
+ this.reloadImage();
30
+ return;
31
+ }
32
+ this.renderImage();
33
+ }
34
+
35
+ renderImage() {
36
+ if (!(this.image && this.context2d)) return;
37
+ const half = this.viewSize.multiply(0.5 / this.model.actualZoom.get());
38
+ const corner = this.model.actualCoordinates.subtract(half);
39
+ this.drawImage(
40
+ this.image,
41
+ new Vector2(0, 0),
42
+ this.model.viewSize,
43
+ corner,
44
+ corner.add(half),
45
+ this.model.actualOpacity.get()
46
+ );
47
+ }
48
+
49
+ reloadImage() {
50
+ this.image = null;
51
+ if (this.model.image.uri.isEmpty()) return;
52
+ this.game.assets.getAsset(this.model.image.uri.get(), (img) => {
53
+ this.image = img;
54
+ this.renderImage();
55
+ });
56
+ }
57
+
58
+ }
@@ -0,0 +1,42 @@
1
+ import DomRenderer from "../dom/DomRenderer";
2
+ import DOMHelper from "../../helper/DOMHelper";
3
+ import ImageCanvasRenderer from "./ImageCanvasRenderer";
4
+
5
+ export default class ImageViewRenderer extends DomRenderer {
6
+
7
+ /**
8
+ * @type ImageViewModel
9
+ */
10
+ model;
11
+
12
+ constructor(game, model, dom) {
13
+ super(game, model, dom);
14
+
15
+ this.model = model;
16
+ this.canvas = null;
17
+ }
18
+
19
+ activateInternal() {
20
+ this.canvas = DOMHelper.createElement(this.dom, 'canvas');
21
+ this.addChild(new ImageCanvasRenderer(this.game, this.model, this.canvas));
22
+
23
+ this.updateSize();
24
+ }
25
+
26
+ deactivateInternal() {
27
+ this.resetChildren();
28
+ this.removeElement(this.canvas);
29
+ this.canvas = null;
30
+ }
31
+
32
+ renderInternal() {
33
+ if (this.model.viewSize.isDirty) {
34
+ this.updateSize();
35
+ }
36
+ }
37
+
38
+ updateSize() {
39
+ this.canvas.width = this.model.viewSize.x;
40
+ this.canvas.height = this.model.viewSize.y;
41
+ }
42
+ }
@@ -1,6 +1,6 @@
1
1
  import DomRenderer from "./DomRenderer";
2
2
 
3
- export default class ImageRenderer extends DomRenderer {
3
+ export default class ImageDomRenderer extends DomRenderer {
4
4
 
5
5
  /**
6
6
  * @type DirtyValue
@@ -1,6 +1,5 @@
1
1
  import ObjectModel from "../../../core/model/ObjectModel";
2
2
  import StringValue from "../../../core/model/value/StringValue";
3
- import FloatValue from "../../../core/model/value/FloatValue";
4
3
  import Vector2 from "../../../core/model/vector/Vector2";
5
4
 
6
5
  export default class ImageModel extends ObjectModel {
@@ -10,21 +9,6 @@ export default class ImageModel extends ObjectModel {
10
9
  */
11
10
  uri;
12
11
 
13
- /**
14
- * @type FloatValue
15
- */
16
- opacity;
17
-
18
- /**
19
- * @type Vector2
20
- */
21
- coordinates;
22
-
23
- /**
24
- * @type FloatValue
25
- */
26
- zoom;
27
-
28
12
  /**
29
13
  * @type Vector2
30
14
  */
@@ -34,10 +18,7 @@ export default class ImageModel extends ObjectModel {
34
18
  super(persistent);
35
19
 
36
20
  this.uri = this.addProperty('uri', new StringValue());
37
- this.opacity = this.addProperty('opacity', new FloatValue());
38
- this.coordinates = this.addProperty('coordinates', new Vector2());
39
- this.size = this.addProperty('size', new Vector2());
40
- this.zoom = this.addProperty('startZoom', new FloatValue(1));
21
+ this.size = this.addProperty('size', new Vector2(0, 0, false));
41
22
  }
42
23
 
43
24
  }
@@ -0,0 +1,103 @@
1
+ import ObjectModel from "../../../core/model/ObjectModel";
2
+ import FloatValue from "../../../core/model/value/FloatValue";
3
+ import Vector2 from "../../../core/model/vector/Vector2";
4
+ import ImageModel from "./ImageModel";
5
+ import ImageHelper from "../../../core/helper/ImageHelper";
6
+ import NumberHelper from "../../../core/helper/NumberHelper";
7
+
8
+ export default class ImageViewModel extends ObjectModel {
9
+
10
+ /**
11
+ * @type ImageModel
12
+ */
13
+ image;
14
+
15
+ /**
16
+ * @type Vector2
17
+ */
18
+ viewSize;
19
+
20
+ /**
21
+ * @type FloatValue
22
+ */
23
+ opacity;
24
+
25
+ /**
26
+ * @type FloatValue
27
+ */
28
+ actualOpacity;
29
+
30
+ /**
31
+ * @type Vector2
32
+ */
33
+ coordinates;
34
+
35
+ /**
36
+ * @type Vector2
37
+ */
38
+ actualCoordinates;
39
+
40
+ /**
41
+ * @type FloatValue
42
+ */
43
+ zoom;
44
+
45
+ /**
46
+ * @type FloatValue
47
+ */
48
+ actualZoom;
49
+
50
+ constructor(persistent = true) {
51
+ super(persistent);
52
+
53
+ this.image = this.addProperty('image', new ImageModel());
54
+
55
+ this.viewSize = this.addProperty('viewSize', new Vector2());
56
+
57
+ this.opacity = this.addProperty('opacity', new FloatValue(1));
58
+ this.actualOpacity = this.addProperty('actualOpacity', new FloatValue(1));
59
+
60
+ this.coordinates = this.addProperty('coordinates', new Vector2());
61
+ this.actualCoordinates = this.addProperty('actualCoordinates', new Vector2());
62
+
63
+ this.zoom = this.addProperty('zoom', new FloatValue(1));
64
+ this.actualZoom = this.addProperty('actualZoom', new FloatValue(1));
65
+
66
+ this.opacity.addEventListener('change', () => this.updateOpacity());
67
+
68
+ this.zoom.addEventListener('change', () => this.updateZoom());
69
+ this.viewSize.addEventListener('change', () => this.updateZoom());
70
+ this.image.size.addEventListener('change', () => this.updateZoom());
71
+
72
+ this.coordinates.addEventListener('change', () => this.updateCoordinates());
73
+ this.viewSize.addEventListener('change', () => this.updateCoordinates());
74
+ this.actualZoom.addEventListener('change', () => this.updateCoordinates());
75
+ this.image.size.addEventListener('change', () => this.updateCoordinates());
76
+ }
77
+
78
+ updateOpacity() {
79
+ this.model.actualOpacity.set(NumberHelper.between(0, 1, this.model.opacity.get()));
80
+ }
81
+
82
+ updateCoordinates() {
83
+ this.actualCoordinates.set(
84
+ ImageHelper.sanitizeCenter(
85
+ this.image.size,
86
+ this.viewSize,
87
+ this.actualZoom.get(),
88
+ this.coordinates
89
+ )
90
+ );
91
+ }
92
+
93
+ updateZoom() {
94
+ this.actualZoom.set(
95
+ ImageHelper.sanitizeZoom(
96
+ this.image.size,
97
+ this.viewSize,
98
+ this.zoom.get()
99
+ )
100
+ );
101
+ }
102
+
103
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wgge",
3
- "version": "0.1.23",
3
+ "version": "0.1.25",
4
4
  "description": "Web GL Game Engine",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -1,93 +0,0 @@
1
- import DomRenderer from "../dom/DomRenderer";
2
- import Vector2 from "../../model/vector/Vector2";
3
- import DOMHelper from "../../helper/DOMHelper";
4
- import ImageHelper from "../../helper/ImageHelper";
5
-
6
- export default class ImageToCanvasRenderer extends DomRenderer {
7
-
8
- /**
9
- * @type ImageModel
10
- */
11
- model;
12
-
13
- constructor(game, model, dom) {
14
- super(game, model, dom);
15
-
16
- this.model = model;
17
- this.image = null;
18
- }
19
-
20
- activateInternal() {
21
- this.canvas = DOMHelper.createElement(this.dom, 'canvas');
22
- this.context2d = this.canvas.getContext('2d');
23
- this.updateSize();
24
- this.reloadImage();
25
- }
26
-
27
- deactivateInternal() {
28
- this.removeElement(this.canvas);
29
- this.context2d = null;
30
- }
31
-
32
- renderInternal() {
33
- if (this.model.size.isDirty) {
34
- this.updateSize();
35
- }
36
- if (this.model.uri.isDirty) {
37
- this.reloadImage();
38
- return;
39
- }
40
- this.renderImage();
41
- }
42
-
43
- renderImage() {
44
- if (!(this.image && this.context2d)) return;
45
- this.context2d.clearRect(0, 0, this.model.size.x, this.model.size.y);
46
- this.context2d.globalAlpha = this.model.opacity.get();
47
- const imageSize = new Vector2(this.image.width, this.image.height);
48
- const zoom = ImageHelper.sanitizeZoom(
49
- imageSize,
50
- this.model.size,
51
- this.model.zoom.get()
52
- );
53
-
54
- this.model.zoom.set(zoom);
55
-
56
- const center = ImageHelper.sanitizeCenter(
57
- imageSize,
58
- this.model.size,
59
- zoom,
60
- this.model.coordinates
61
- );
62
-
63
- this.model.coordinates.set(center);
64
-
65
- const viewSize = this.model.size.multiply(1 / zoom);
66
- const corner = center.subtract(viewSize.multiply(0.5));
67
- this.context2d.drawImage(
68
- this.image,
69
- corner.x,
70
- corner.y,
71
- viewSize.x,
72
- viewSize.y,
73
- 0,
74
- 0,
75
- this.model.size.x,
76
- this.model.size.y
77
- );
78
- }
79
-
80
- reloadImage() {
81
- this.image = null;
82
- if (this.model.uri.isEmpty()) return;
83
- this.game.assets.getAsset(this.model.uri.get(), (img) => {
84
- this.image = img;
85
- this.renderImage();
86
- });
87
- }
88
-
89
- updateSize() {
90
- this.canvas.width = this.model.size.x;
91
- this.canvas.height = this.model.size.y;
92
- }
93
- }