squishjs 1.3.9 → 1.4.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/util/views.js +49 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squishjs",
3
- "version": "1.3.9",
3
+ "version": "1.4.0",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
package/src/util/views.js CHANGED
@@ -23,6 +23,10 @@ const getView = (plane, view, playerIds, translation = {}, scale = {}) => {
23
23
  let shouldInclude = true;
24
24
 
25
25
  const translatedCoords = [];
26
+ // Same vertices as translatedCoords but WITHOUT the [0,100] viewport
27
+ // clamp. Needed for asset crop math: to know how much of an image
28
+ // falls outside the viewport we need its true (unclamped) extent.
29
+ const rawTranslatedCoords = [];
26
30
 
27
31
  // same hack as geometry utils
28
32
  const vertices = node.node.coordinates2d || [
@@ -70,6 +74,22 @@ const getView = (plane, view, playerIds, translation = {}, scale = {}) => {
70
74
  }
71
75
 
72
76
  translatedCoords.push([translatedX, translatedY]);
77
+
78
+ // Unclamped transform of the same vertex (scale + translation,
79
+ // but no clamp to [0,100]). Used only for asset crop below.
80
+ let rawX = (scale.x || 1) * (x - view.x);
81
+ let rawY = (scale.y || 1) * (y - view.y);
82
+
83
+ if (shouldTranslate) {
84
+ if (translation.x) {
85
+ rawX += translation.x;
86
+ }
87
+ if (translation.y) {
88
+ rawY += translation.y;
89
+ }
90
+ }
91
+
92
+ rawTranslatedCoords.push([rawX, rawY]);
73
93
  }
74
94
 
75
95
  if (shouldInclude) {
@@ -89,10 +109,35 @@ const getView = (plane, view, playerIds, translation = {}, scale = {}) => {
89
109
  const thirdPoint = copied.node.coordinates2d[2];
90
110
  const width = secondPoint[0] - firstPoint[0];
91
111
  const height = thirdPoint[1] - secondPoint[1];
92
- Object.values(copied.node.asset)[0].pos.x = firstPoint[0];
93
- Object.values(copied.node.asset)[0].pos.y = firstPoint[1];
94
- Object.values(copied.node.asset)[0].size.x = width;
95
- Object.values(copied.node.asset)[0].size.y = height;
112
+
113
+ const assetObj = Object.values(copied.node.asset)[0];
114
+
115
+ // pos/size = the clamped, on-screen visible box.
116
+ assetObj.pos.x = firstPoint[0];
117
+ assetObj.pos.y = firstPoint[1];
118
+ assetObj.size.x = width;
119
+ assetObj.size.y = height;
120
+
121
+ // Crop the source image to the portion clipped by the
122
+ // viewport. Without this the renderer squashes the whole
123
+ // image into the (smaller) visible box instead of cutting
124
+ // off the off-screen part. Using the unclamped corners we
125
+ // measure how much of the image fell outside [0,100] on
126
+ // each edge, as a percentage of the image's full span.
127
+ const rawFirst = rawTranslatedCoords[0]; // top-left
128
+ const rawThird = rawTranslatedCoords[2]; // bottom-right
129
+ const fullWidth = rawThird[0] - rawFirst[0];
130
+ const fullHeight = rawThird[1] - rawFirst[1];
131
+
132
+ if (fullWidth > 0) {
133
+ assetObj.cropLeft = Math.max(0, (0 - rawFirst[0]) / fullWidth) * 100;
134
+ assetObj.cropRight = Math.max(0, (rawThird[0] - 100) / fullWidth) * 100;
135
+ }
136
+
137
+ if (fullHeight > 0) {
138
+ assetObj.cropTop = Math.max(0, (0 - rawFirst[1]) / fullHeight) * 100;
139
+ assetObj.cropBottom = Math.max(0, (rawThird[1] - 100) / fullHeight) * 100;
140
+ }
96
141
  }
97
142
  }
98
143
  copied.node.playerIds = playerIds || [];