Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const { serialize } = require("./helpers");
 
const assert = require("assert");
 
Feature("Zooming and rotating");
 
const IMAGE =
  "https://htx-pub.s3.us-east-1.amazonaws.com/examples/images/nick-owuor-astro-nic-visuals-wDifg5xc9Z4-unsplash.jpg";
 
const BLUEVIOLET = {
  color: "#8A2BE2",
  rgbArray: [138, 43, 226],
};
const getConfigWithShape = (shape, props = "") => `
  <View>
    <Image name="img" value="$image" zoom="true" zoomBy="1.5" zoomControl="true" rotateControl="true"></Image>
    <${shape}Labels ${props} name="tag" toName="img">
        <Label value="Test" background="${BLUEVIOLET.color}"></Label>
    </${shape}Labels>
  </View>`;
 
const hScaleCoords = ([x, y], w, h) => {
  const ratio = w / h;
 
  return [x * ratio, y * ratio];
};
const rotateCoords = (point, degree, w, h) => {
  const [x, y] = point;
 
  if (!degree) return point;
 
  degree = (360 + degree) % 360;
  if (degree === 90) return hScaleCoords([h - y - 1, x], w, h);
  if (degree === 270) return hScaleCoords([y, w - x - 1], w, h);
  if (Math.abs(degree) === 180) return [w - x - 1, h - y - 1];
  return [x, y];
};
 
const shapes = [
  {
    shape: "KeyPoint",
    props: 'strokeWidth="5"',
    action: "clickKonva",
    regions: [
      {
        params: [100, 100],
      },
      {
        params: [200, 100],
      },
    ],
  },
  {
    shape: "Polygon",
    action: "clickPolygonPointsKonva",
    regions: [
      {
        params: [
          [
            [95, 95],
            [95, 105],
            [105, 105],
            [105, 95],
          ],
        ],
      },
      {
        params: [
          [
            [400, 10],
            [400, 90],
            [370, 30],
            [300, 10],
          ],
        ],
      },
    ],
  },
  {
    shape: "Rectangle",
    action: "dragKonva",
    regions: [
      {
        params: [95, 95, 10, 10],
      },
      {
        params: [400, 350, -50, -50],
      },
    ],
  },
  {
    shape: "Ellipse",
    action: "dragKonva",
    regions: [
      {
        params: [100, 100, 10, 10],
      },
      {
        params: [230, 300, -50, -30],
      },
    ],
  },
];
const shapesTable = new DataTable(["shape", "props", "action", "regions"]);
 
shapes.forEach(({ shape, props = "", action, regions }) => {
  shapesTable.add([shape, props, action, regions]);
});
 
Data(shapesTable).Scenario(
  "Simple rotation",
  async ({ I, LabelStudio, AtImageView, AtOutliner, AtPanels, current }) => {
    const config = getConfigWithShape(current.shape, current.props);
 
    const params = {
      config,
      data: { image: IMAGE },
    };
    const AtDetailsPanel = AtPanels.usePanel(AtPanels.PANEL.DETAILS);
 
    I.amOnPage("/");
    LabelStudio.init(params);
    AtDetailsPanel.collapsePanel();
    LabelStudio.waitForObjectsReady();
    AtOutliner.seeRegions(0);
    const canvasSize = await AtImageView.getCanvasSize();
 
    for (const region of current.regions) {
      I.pressKey(["u"]);
      I.pressKey("1");
      AtImageView[current.action](...region.params);
    }
    const standard = await I.executeScript(serialize);
    const rotationQueue = ["right", "right", "right", "right", "left", "left", "left", "left"];
    let degree = 0;
    let hasPixel = await AtImageView.hasPixelColor(100, 100, BLUEVIOLET.rgbArray);
 
    assert.equal(hasPixel, true);
    for (const rotate of rotationQueue) {
      I.click(locate(`[aria-label='rotate-${rotate}']`));
      degree += rotate === "right" ? 90 : -90;
      hasPixel = await AtImageView.hasPixelColor(
        ...rotateCoords([100, 100], degree, canvasSize.width, canvasSize.height).map(Math.round),
        BLUEVIOLET.rgbArray,
      );
      assert.strictEqual(hasPixel, true);
      const result = await I.executeScript(serialize);
 
      for (let i = 0; i < standard.length; i++) {
        assert.deepEqual(standard[i].result, result[i].result);
      }
    }
  },
);
 
Data(shapesTable).Scenario("Rotate zoomed", async ({ I, LabelStudio, AtImageView, AtOutliner, AtPanels, current }) => {
  const params = {
    config: getConfigWithShape(current.shape, current.props),
    data: { image: IMAGE },
  };
  const AtDetailsPanel = AtPanels.usePanel(AtPanels.PANEL.DETAILS);
 
  I.amOnPage("/");
  LabelStudio.init(params);
  AtDetailsPanel.collapsePanel();
  LabelStudio.waitForObjectsReady();
  AtOutliner.seeRegions(0);
  const canvasSize = await AtImageView.getCanvasSize();
 
  for (const region of current.regions) {
    I.pressKey(["u"]);
    I.pressKey("1");
    AtImageView[current.action](...region.params);
  }
  const rotationQueue = ["right", "right", "right", "right", "left", "left", "left", "left"];
  let degree = 0;
  const ZOOM = 3;
 
  AtImageView.setZoom(ZOOM, -100 * ZOOM, -100 * ZOOM);
  let hasPixel = await AtImageView.hasPixelColor(1, 1, BLUEVIOLET.rgbArray);
 
  assert.strictEqual(hasPixel, true, "Must have pixel before rotation");
  for (const rotate of rotationQueue) {
    I.click(locate(`[aria-label='rotate-${rotate}']`));
    degree += rotate === "right" ? 90 : -90;
    hasPixel = await AtImageView.hasPixelColor(
      ...rotateCoords([1, 1], degree, canvasSize.width, canvasSize.height).map(Math.round),
      BLUEVIOLET.rgbArray,
    );
 
    assert.strictEqual(hasPixel, true, `Must have pixel after rotation [${degree}deg]`);
  }
});
 
const windowSizesTable = new DataTable(["width", "height"]);
 
windowSizesTable.add([1280, 720]);
windowSizesTable.add([1920, 1080]);
windowSizesTable.add([800, 480]);
windowSizesTable.add([1017, 970]);
 
Data(windowSizesTable).Scenario(
  "Rotation with different window sizes",
  async ({ I, LabelStudio, AtImageView, AtOutliner, AtPanels, current }) => {
    const config = getConfigWithShape("Rectangle");
 
    const params = {
      config,
      data: { image: IMAGE },
    };
    const AtDetailsPanel = AtPanels.usePanel(AtPanels.PANEL.DETAILS);
 
    I.amOnPage("/");
    I.resizeWindow(current.width, current.height);
    LabelStudio.init(params);
    // On small screens you can't see the details panel from the beginning
    if (current.width > 1000) {
      AtDetailsPanel.collapsePanel();
    }
    LabelStudio.waitForObjectsReady();
    AtOutliner.seeRegions(0);
    const canvasSize = await AtImageView.getCanvasSize();
    const imageSize = await AtImageView.getImageFrameSize();
    const rotationQueue = ["right", "right", "right", "right", "left", "left", "left", "left"];
 
    assert(Math.abs(canvasSize.width - imageSize.width) < 1);
    assert(Math.abs(canvasSize.height - imageSize.height) < 1);
    for (const rotate of rotationQueue) {
      I.click(locate(`[aria-label='rotate-${rotate}']`));
      // Just checking that we see image, to get some time for rotating to be finished and correctly rendered
      I.seeElement('[alt="LS"]');
      I.waitTicks(2);
      const rotatedCanvasSize = await AtImageView.getCanvasSize();
      const rotatedImageSize = await AtImageView.getImageFrameSize();
 
      assert(Math.abs(rotatedCanvasSize.width - rotatedImageSize.width) < 1);
      assert(Math.abs(rotatedCanvasSize.height - rotatedImageSize.height) < 1);
    }
  },
);
 
const twoColumnsConfigs = [
  `<View>
    <View style="display:flex;align-items:start;gap:8px;flex-direction:{{direction}}">
        <RectangleLabels name="label" toName="image" showInline="{{showInline}}">
            <Label value="Label 1" background="#2C7873"/>
            <Label value="Label 2" background="#7232F2"/>
        </RectangleLabels>
        <Image name="image" value="$image" zoom="true" rotateControl="true"/>
    </View>
</View>`,
  `<View>
    <View style="display:flex;align-items:start;gap:8px;flex-direction:{{direction}}">
        <RectangleLabels name="label" toName="image" showInline="{{showInline}}">
            <Label value="Label 1" background="#2C7873"/>
            <Label value="Label 2" background="#7232F2"/>
        </RectangleLabels>
        <View style="flex: 100 0 1%; width: 100%">
            <Image name="image" value="$image" zoom="true" rotateControl="true"/>
        </View>
    </View>
</View>`,
];
 
const layoutVariations = new DataTable(["config", "inline", "reversed"]);
 
twoColumnsConfigs.forEach((config) => {
  for (const inline of [true, false]) {
    for (const reversed of [true, false]) {
      layoutVariations.add([config, inline, reversed]);
    }
  }
});
 
const compareSize = async (I, AtImageView, message1, message2) => {
  const { width: canvasWidth, height: canvasHeight } = await AtImageView.getCanvasSize();
  const { width: imageWidth, height: imageHeight } = await AtImageView.getImageFrameSize();
 
  const widthMessage = `[${message2}] Check width: [${[canvasWidth, imageWidth]}]`;
  const heightMessage = `[${message2}] Check height: [${[canvasHeight, imageHeight]}]`;
 
  I.say(`${message1} [stage: ${canvasWidth}x${canvasHeight}, image: ${imageWidth}x${imageHeight}]`);
  assert(Math.abs(canvasWidth - imageWidth) <= 1, widthMessage);
  assert(Math.abs(canvasHeight - imageHeight) <= 1, heightMessage);
};
 
Data(layoutVariations).Scenario(
  "Rotation in the two columns template",
  async ({ I, LabelStudio, AtImageView, AtOutliner, AtSettings, AtPanels, current }) => {
    I.amOnPage("/");
    let isVerticalLayout = false;
 
    const { config, inline, reversed } = current;
 
    const direction = (inline ? "column" : "row") + (reversed ? "-reverse" : "");
    const resultConfig = config.replace("{{direction}}", direction).replace("{{showInline}}", `${inline}`);
    const params = {
      config: resultConfig,
      data: { image: IMAGE },
      annotations: [
        {
          id: "rotations",
          result: [
            // The region just for canvas size visually indication
            {
              from_name: "label",
              id: "EUsEHxTyrv",
              image_rotation: 0,
              origin: "manual",
              original_height: 2802,
              original_width: 2242,
              to_name: "image",
              type: "rectanglelabels",
              value: {
                height: 100,
                labels: ["Label 2"],
                rotation: 0,
                width: 100,
                x: 0,
                y: 0,
              },
            },
          ],
        },
      ],
    };
    const AtDetailsPanel = AtPanels.usePanel(AtPanels.PANEL.DETAILS);
 
    I.say(`Two columns [config: ${twoColumnsConfigs.indexOf(config)}] [${direction}]`);
 
    LabelStudio.init(params);
    AtDetailsPanel.collapsePanel();
    LabelStudio.waitForObjectsReady();
    AtOutliner.seeRegions(1);
 
    I.click(locate("[aria-label='rotate-right']"));
    AtOutliner.seeRegions(1);
 
    I.wait(0.1);
    await compareSize(I, AtImageView, "Dimensions must be equal in landscape", "landscape, rotated");
 
    I.say("Change to vertcal layout");
    AtSettings.open();
    isVerticalLayout = !isVerticalLayout;
    AtSettings.setLayoutSettings({
      [AtSettings.LAYOUT_SETTINGS.VERTICAL_LAYOUT]: isVerticalLayout,
    });
    AtSettings.close();
 
    AtOutliner.seeRegions(1);
    I.wait(0.1);
    await compareSize(I, AtImageView, "Dimensions must be equal in portrait", "portrait");
 
    I.click(locate("[aria-label='rotate-right']"));
 
    AtOutliner.seeRegions(1);
    I.wait(0.1);
    await compareSize(I, AtImageView, "Dimensions must be equal after rotation in portrain", "portrait, rotated");
  },
);