KoctrX

Untitled

Jan 12th, 2022
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. import { store } from '@/store';
  2. import fileCreator from '@/lib/filesCreator';
  3.  
  4. export default new class {
  5. constructor() {
  6. window.utils = this;
  7. }
  8.  
  9. get zoomList() {
  10. return [500, 200, 175, 150, 125, 100, 75, 50, 25, 10];
  11. }
  12.  
  13. get zoom() {
  14. return store.state.editor.zoom;
  15. }
  16.  
  17. conv(val, slope_min = 0, slope_max = 1) {
  18. val = val / 100;
  19. const r = ((slope_max * (val)) - (slope_min * (val)) - (slope_min * -1)).toFixed(8);
  20.  
  21. return Number(r);
  22. }
  23.  
  24. reverseConv(val, min, max) {
  25. return ((val - min) / (max - min)) * 100;
  26. }
  27.  
  28. rotationSprite(sprite, angle = 0) {
  29. sprite.fcontrol.rotate(angle);
  30. this.canvasRender.control_canvas.renderAll();
  31.  
  32. sprite.angle = angle;
  33. sprite.position.set(
  34. sprite.fcontrol.left / this.canvasRender.zoom,
  35. sprite.fcontrol.top / this.canvasRender.zoom,
  36. );
  37.  
  38. this.canvasRender.updateCanvas();
  39. store.dispatch('controllSpriteEvent', { target: sprite.fcontrol, type: 'moved' });
  40. }
  41.  
  42. calculatePositionSprite(sprite, cr, position = 'tl', size = 30) {
  43. const realWidth = sprite.width / sprite.scale.x;
  44. const realHeight = sprite.height / sprite.scale.y;
  45.  
  46. const crWidth = this.canvasRender.renderer.width / this.canvasRender.zoom;
  47. const crHeight = this.canvasRender.renderer.height / this.canvasRender.zoom;
  48.  
  49. const scaleSize = this.conv(size, 30, 100) / 100;
  50.  
  51. const scaleX = ((crWidth * .4) * scaleSize) / realWidth;
  52. const scaleY = ((crWidth * .4) * scaleSize) / realHeight;
  53.  
  54. const scale = Math.min(scaleX, scaleY);
  55. let x = 0;
  56. let y = 0;
  57.  
  58. const newWidth = (realWidth * scale) / this.canvasRender.zoom;
  59. const newHeight = (realHeight * scale) / this.canvasRender.zoom;
  60.  
  61. const rightPosition = crWidth - newWidth * this.canvasRender.zoom;
  62. const bottomPosition = crHeight - newHeight * this.canvasRender.zoom;
  63.  
  64. const centerLeftPosition = crWidth / 2 - (newWidth * this.canvasRender.zoom / 2);
  65. const centerTopPosition = crHeight / 2 - (newHeight * this.canvasRender.zoom / 2);
  66. switch (position) {
  67. // center top
  68. case 'ct': {
  69. x = centerLeftPosition;
  70.  
  71. break;
  72. }
  73.  
  74. // right top
  75. case 'rt': {
  76. x = rightPosition;
  77. break;
  78. }
  79.  
  80. // center right
  81. case 'cr': {
  82. x = rightPosition;
  83. y = centerTopPosition;
  84. break;
  85. }
  86.  
  87. // bottom right
  88. case 'br': {
  89. x = rightPosition;
  90. y = bottomPosition;
  91. break;
  92. }
  93.  
  94. // bottom center
  95. case 'bc': {
  96. x = centerLeftPosition;
  97. y = bottomPosition;
  98. break;
  99. }
  100.  
  101. // bottom left
  102. case 'bl': {
  103. y = bottomPosition;
  104. break;
  105. }
  106.  
  107. // left center
  108. case 'lc': {
  109. y = centerTopPosition;
  110. break;
  111. }
  112.  
  113. // center center
  114. case 'cc': {
  115. x = centerLeftPosition;
  116. y = centerTopPosition;
  117. break;
  118. }
  119. }
  120.  
  121. sprite.scale.set(scale, scale);
  122. sprite.setXY(x, y);
  123.  
  124. sprite.fcontrol.set({
  125. width: sprite.width * this.canvasRender.zoom,
  126. height: sprite.height * this.canvasRender.zoom,
  127. scaleX: 1, scaleY: 1,
  128. left: x * this.canvasRender.zoom,
  129. top: y * this.canvasRender.zoom
  130. });
  131.  
  132. this.canvasRender.control_canvas.renderAll();
  133.  
  134. return { fcontrol: sprite.fcontrol, sprite };
  135. }
  136.  
  137. async onw(sprite) {
  138. //if(sprite.baseTexture) { sprite = new PIXI.Sprite(sprite); }
  139.  
  140. const base64 = this.canvasRender.renderer.plugins.extract.base64(sprite);
  141. const blob = await fetch(base64).then(res => res.blob());
  142.  
  143. window.open(URL.createObjectURL(blob));
  144. }
  145.  
  146. stackPackage(data = {}) {
  147. return { _tick: 1, isGroup: false, ...data };
  148. }
  149.  
  150. set zoom(val) {
  151. return store.state.editor.zoom = val;
  152. }
  153.  
  154. stringCssToObject(str) {
  155. return str.split(';').reduce((res, item) => {
  156. if (item) {
  157. const [key, val] = item.split(':');
  158. res = { ...res, [key.trim()]: val.trim() };
  159. }
  160.  
  161. return res;
  162. }, {})
  163. }
  164.  
  165. getSpriteParams(sprite) {
  166. return {};
  167. }
  168.  
  169. blobToFile(blob, fileName) {
  170. const file = new File([blob], fileName, { lastModified: Date.now(), type: blob.type });
  171. return file;
  172. }
  173.  
  174. calculateScale(w, h, toW, toH) {
  175. return Math.min(toW / w, toH / h);
  176. }
  177.  
  178. findNearIndex(val, arr) {
  179. return arr.reduce((res, item, index) => {
  180. const value = val - item;
  181. const abs = Math.abs(value);
  182. if (res === false || res.abs > abs) return { index, abs, value };
  183.  
  184. return res;
  185. }, false);
  186. }
  187.  
  188. toggleFullscreen() {
  189. if (document.fullscreenElement ||
  190. document.webkitFullscreenElement ||
  191. document.mozFullScreenElement ||
  192. document.msFullscreenElement
  193. ) {
  194. if (document.exitFullscreen) {
  195. document.exitFullscreen();
  196. } else if (document.mozCancelFullScreen) {
  197. document.mozCancelFullScreen();
  198. } else if (document.webkitExitFullscreen) {
  199. document.webkitExitFullscreen();
  200. } else if (document.msExitFullscreen) {
  201. document.msExitFullscreen();
  202. }
  203. } else {
  204. const element = document.querySelector('.workspace__vid-wrapper');
  205. if (element.requestFullscreen) {
  206. element.requestFullscreen();
  207. } else if (element.mozRequestFullScreen) {
  208. element.mozRequestFullScreen();
  209. } else if (element.webkitRequestFullscreen) {
  210. element.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  211. } else if (element.msRequestFullscreen) {
  212. element.msRequestFullscreen();
  213. }
  214. }
  215. }
  216.  
  217. get canvasRender() {
  218. return store.state.editor.canvasRender;
  219. }
  220.  
  221. async printScreen(cr, { open, download }) {
  222. const scaleX = cr.container.scale.x;
  223. const scaleY = cr.container.scale.y;
  224.  
  225. cr.container.scale.set(1, 1)
  226. const base64 = cr.renderer.plugins.extract.base64(cr.container);
  227.  
  228. cr.container.scale.set(scaleX, scaleY);
  229. const blob = await fetch(base64).then(res => res.blob());
  230. const filename = `Print_screen_${new Date().toISOString()}.png`;
  231.  
  232. if (open) window.open(URL.createObjectURL(blob));
  233. if (download) this.donwloadFile(blob, filename);
  234.  
  235. return new File([blob], filename, { lastModified: Date.now(), type: blob.type });
  236. }
  237.  
  238. donwloadFile(blob, filename) {
  239. const a = document.createElement('a');
  240. a.href = URL.createObjectURL(blob);
  241. a.download = filename;
  242. a.click();
  243.  
  244. URL.revokeObjectURL(a.href);
  245. }
  246.  
  247. parseZero(num) { return num < 10 ? `0${num}` : num }
  248.  
  249. parseTime(sec, isString = false, isMC = false) {
  250. let ms = parseInt(60 * (sec - parseInt(sec)));
  251. let suffix = isMC ? `.${this.parseZero(ms || 0)}` : ``;
  252.  
  253. if (sec / 60 > 1) {
  254. const newSec = Math.floor(sec % 60);
  255. const min = Math.floor(sec / 60);
  256.  
  257. if (isString) return `${min}m${this.parseZero(newSec)}s`
  258. return `${this.parseZero(min)}:${this.parseZero(newSec)}${suffix}`;
  259. } else {
  260. const newSec = Math.floor(sec);
  261.  
  262. if (isString) return `${(Number(sec) || 0).toFixed(2)}s`
  263. return `00:${this.parseZero(newSec)}${suffix}`;
  264. }
  265. }
  266.  
  267. getTimeRemaining(endtime) {
  268. const total = endtime - Date.now();
  269. const seconds = Math.floor((total / 1000) % 60);
  270. const minutes = Math.floor((total / 1000 / 60) % 60);
  271. const hours = Math.floor((total / (1000 * 60 * 60)) % 24);
  272. const days = Math.floor(total / (1000 * 60 * 60 * 24));
  273.  
  274. return { total, days, hours, minutes, seconds };
  275. }
  276.  
  277. async openBase64(b64) {
  278. window.open(URL.createObjectURL(await fetch(b64).then(res => res.blob())));
  279. }
  280.  
  281. openSprite(sprite, isBase = false) {
  282. if(isBase) sprite = new PIXI.Sprite(new PIXI.Texture(sprite));
  283.  
  284. return this.openBase64(cr.renderer.plugins.extract.base64(sprite));
  285. }
  286.  
  287. async addBackgroundImage(payload = {}, add = false) {
  288. const file = await fileCreator.getProcessedFile(`data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAB5JREFUOE9j/P///38GCgDjqAEMo2HAMBoGDMMiDAAlHz/RG+BMbgAAAABJRU5ErkJggg==`, {});
  289. if(add) return await store.dispatch('addTimeline', { ...file, isBackground: true, payload });
  290.  
  291. return file;
  292. }
  293.  
  294. changeZoom(value, type = false) {
  295. if (!type) {
  296. store.state.editor.zoom = value;
  297. } else {
  298. const min = this.zoomList[this.zoomList.length - 1];
  299. const max = this.zoomList[0];
  300.  
  301. if (type == 'minus' || type == 'plus') {
  302. const near = this.findNearIndex(this.zoom, this.zoomList);
  303. const nearValue = this.zoomList[near.index];
  304.  
  305. if (type == 'minus' && this.zoom != min) {
  306. if (nearValue == min && near.value === 0) return;
  307. if (near.value > 0) return this.zoom = this.zoomList[near.index];
  308.  
  309. return this.zoom = this.zoomList[near.index + 1];
  310. } else if (type == 'plus' && this.zoom != max) {
  311. if (nearValue == max && near.value === 0) return;
  312. if (near.value < 0) return this.zoom = this.zoomList[near.index];
  313.  
  314. return this.zoom = this.zoomList[near.index - 1];
  315. }
  316. } else if (type == 'fit' || type == 'fill') {
  317. const { width, height } = store.state.workspaceSize;
  318. const zoom = store.state.editor.canvasRender.calculateFitScreen(width, height);
  319.  
  320. this.zoom = zoom * 100;
  321. }
  322. // TODO:
  323. }
  324. }
  325.  
  326. memorySizeOf(obj) {
  327. let bytes = 0;
  328.  
  329. function sizeOf(obj) {
  330. if (obj !== null && obj !== undefined) {
  331. switch (typeof obj) {
  332. case 'number':
  333. bytes += 8;
  334. break;
  335. case 'string':
  336. bytes += obj.length * 2;
  337. break;
  338. case 'boolean':
  339. bytes += 4;
  340. break;
  341. case 'object':
  342. let objClass = Object.prototype.toString.call(obj).slice(8, -1);
  343. if (objClass === 'Object' || objClass === 'Array') {
  344. for (let key in obj) {
  345. if (!obj.hasOwnProperty(key)) continue;
  346. sizeOf(obj[key]);
  347. }
  348. } else bytes += obj.toString().length * 2;
  349. break;
  350. }
  351. }
  352. return bytes;
  353. };
  354.  
  355. function formatByteSize(bytes) {
  356. if (bytes < 1024) return bytes + " bytes";
  357. else if (bytes < 1048576) return (bytes / 1024).toFixed(3) + " KiB";
  358. else if (bytes < 1073741824) return (bytes / 1048576).toFixed(3) + " MiB";
  359. else return (bytes / 1073741824).toFixed(3) + " GiB";
  360. };
  361.  
  362. return formatByteSize(sizeOf(obj));
  363. }
  364.  
  365. async generateImageThumb(blob, width = 250, height = 250) {
  366. const canvas = document.createElement('canvas');
  367. canvas.style = 'left: -3000px; top: -3000px; position: absolute;';
  368.  
  369. const img = new Image();
  370. img.src = URL.createObjectURL(blob);
  371. await new Promise(resolve => img.onload = resolve);
  372.  
  373. const scale = this.calculateScale(img.naturalWidth, img.naturalHeight, width, height);
  374. canvas.width = scale * img.naturalWidth;
  375. canvas.height = scale * img.naturalHeight;
  376. const ctx = canvas.getContext('2d');
  377.  
  378. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  379.  
  380. const base64 = canvas.toDataURL();
  381. URL.revokeObjectURL(img.src);
  382.  
  383. return fetch(base64).then(res => res.blob());
  384. }
  385.  
  386. uuidv4() {
  387. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  388. const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  389. return v.toString(16);
  390. });
  391. }
  392. }
  393.  
Add Comment
Please, Sign In to add comment