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
import { types, getParent } from "mobx-state-tree";
import { FileLoader } from "../../../utils/FileLoader";
import { clamp } from "../../../utils/utilities";
import { FF_IMAGE_MEMORY_USAGE, isFF } from "../../../utils/feature-flags";
 
const fileLoader = new FileLoader();
 
export const ImageEntity = types
  .model("ImageEntity", {
    id: types.identifier,
    src: types.string,
    index: types.number,
 
    rotation: types.optional(types.number, 0),
 
    /**
     * Natural sizes of Image
     * Constants
     */
    naturalWidth: types.optional(types.integer, 1),
    naturalHeight: types.optional(types.integer, 1),
 
    stageWidth: types.optional(types.number, 1),
    stageHeight: types.optional(types.number, 1),
 
    /**
     * Zoom Scale
     */
    zoomScale: types.optional(types.number, 1),
 
    /**
     * Coordinates of left top corner
     * Default: { x: 0, y: 0 }
     */
    zoomingPositionX: types.optional(types.number, 0),
    zoomingPositionY: types.optional(types.number, 0),
 
    /**
     * Brightness of Canvas
     */
    brightnessGrade: types.optional(types.number, 100),
 
    contrastGrade: types.optional(types.number, 100),
  })
  .volatile(() => ({
    stageRatio: 1,
    // Container's sizes causing limits to calculate a scale factor
    containerWidth: 1,
    containerHeight: 1,
 
    stageZoom: 1,
    stageZoomX: 1,
    stageZoomY: 1,
    currentZoom: 1,
 
    /** Is image downloaded to local cache */
    downloaded: false,
    /** Is image being downloaded */
    downloading: false,
    /** If error happened during download */
    error: false,
    /** Download progress 0..1 */
    progress: 0,
    /** Local image src created with URL.createURLObject */
    currentSrc: undefined,
    /** Is image loaded using `<img/>` tag and cached by the browser */
    imageLoaded: false,
  }))
  .views((self) => ({
    get parent() {
      // Get the ImageEntityMixin
      return getParent(self, 2);
    },
    get imageCrossOrigin() {
      return self.parent?.imageCrossOrigin ?? "anonymous";
    },
  }))
  .actions((self) => ({
    preload() {
      if (self.ensurePreloaded() || !self.src) return;
 
      if (isFF(FF_IMAGE_MEMORY_USAGE)) {
        self.setDownloading(true);
        new Promise((resolve) => {
          const img = new Image();
          // Get from the image tag
          const crossOrigin = self.imageCrossOrigin;
          if (crossOrigin) img.crossOrigin = crossOrigin;
          img.onload = () => {
            self.setCurrentSrc(self.src);
            self.setDownloaded(true);
            self.setProgress(1);
            self.setDownloading(false);
            self.setImageLoaded(true);
            resolve();
          };
          img.onerror = () => {
            self.setError(true);
            self.setDownloading(false);
            resolve();
          };
          img.src = self.src;
        });
        return;
      }
 
      self.setDownloading(true);
      fileLoader
        .download(self.src, (_t, _l, progress) => {
          self.setProgress(progress);
        })
        .then((url) => {
          self.setDownloaded(true);
          self.setDownloading(false);
          self.setCurrentSrc(url);
        })
        .catch(() => {
          self.setDownloading(false);
          self.setError(true);
        });
    },
 
    ensurePreloaded() {
      if (isFF(FF_IMAGE_MEMORY_USAGE)) return self.currentSrc !== undefined;
 
      if (fileLoader.isError(self.src)) {
        self.setDownloading(false);
        self.setError(true);
        return true;
      }
      if (fileLoader.isPreloaded(self.src)) {
        self.setDownloading(false);
        self.setDownloaded(true);
        self.setProgress(1);
        self.setCurrentSrc(fileLoader.getPreloadedURL(self.src));
        return true;
      }
      return false;
    },
 
    setImageLoaded(value) {
      self.imageLoaded = value;
    },
 
    setProgress(progress) {
      self.progress = clamp(progress, 0, 100);
    },
 
    setDownloading(downloading) {
      self.downloading = downloading;
    },
 
    setDownloaded(downloaded) {
      self.downloaded = downloaded;
    },
 
    setCurrentSrc(src) {
      self.currentSrc = src;
    },
 
    setError() {
      self.error = true;
    },
  }))
  .actions((self) => ({
    setRotation(angle) {
      self.rotation = angle;
    },
 
    setNaturalWidth(width) {
      self.naturalWidth = width;
    },
 
    setNaturalHeight(height) {
      self.naturalHeight = height;
    },
 
    setStageWidth(width) {
      self.stageWidth = width;
    },
 
    setStageHeight(height) {
      self.stageHeight = height;
    },
 
    setStageRatio(ratio) {
      self.stageRatio = ratio;
    },
 
    setContainerWidth(width) {
      self.containerWidth = width;
    },
 
    setContainerHeight(height) {
      self.containerHeight = height;
    },
 
    setStageZoom(zoom) {
      self.stageZoom = zoom;
    },
 
    setStageZoomX(zoom) {
      self.stageZoomX = zoom;
    },
 
    setStageZoomY(zoom) {
      self.stageZoomY = zoom;
    },
 
    setCurrentZoom(zoom) {
      self.currentZoom = zoom;
    },
 
    setZoomScale(zoomScale) {
      self.zoomScale = zoomScale;
    },
 
    setZoomingPositionX(x) {
      self.zoomingPositionX = x;
    },
 
    setZoomingPositionY(y) {
      self.zoomingPositionY = y;
    },
 
    setBrightnessGrade(grade) {
      self.brightnessGrade = grade;
    },
 
    setContrastGrade(grade) {
      self.contrastGrade = grade;
    },
  }));