KoctrX

Untitled

Jun 2nd, 2022
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Editor {
  2.     constructor(image, canvas) {
  3.         this.app = new PIXI.Application({
  4.             width: canvas.width,
  5.             height: canvas.height,
  6.             antialias: false,
  7.             transparent: true,
  8.             resolution: 1
  9.         });
  10.  
  11.         this.opts = {
  12.             angle: 0,
  13.             flipX: false,
  14.             flipY: false
  15.         };
  16.  
  17.         this.loadImage(image, canvas);
  18.     }
  19.  
  20.     destroyCrop() {
  21.         if(this.cr) this.cr.destroy();
  22.         this.cr = false;
  23.     }
  24.  
  25.     extract(type = 'base64') {
  26.         const base64 = this.app.renderer.plugins.extract.base64(this.app.stage);
  27.  
  28.         if(type === 'blob') {
  29.             return fetch(base64).then(res => res.blob());
  30.         }
  31.        
  32.         return base64;
  33.     }
  34.  
  35.     destroy() {
  36.         if(this.cr) this.cr.destroy();
  37.         this.cr = false;
  38.         ed.app.destroy();
  39.     }
  40.  
  41.     get config() { return this.opts; }
  42.  
  43.     flip(to = 'x') {
  44.         if(to === 'x') {
  45.             this.sprite.anchor.x = this.sprite.anchor.x == 1 ? 0 : 1;
  46.             this.sprite.scale.x *= -1;
  47.         } else {
  48.             this.sprite.anchor.y = this.sprite.anchor.y == 1 ? 0 : 1;
  49.             this.sprite.scale.y *= -1;
  50.         }
  51.  
  52.         this.opts.flipX = this.sprite.scale.x < 0;
  53.         this.opts.flipY = this.sprite.scale.y < 0;
  54.     }
  55.  
  56.     calculateCropPosition() {
  57.         const cropBox = this.cr.getCropBoxData();
  58.        
  59.         let x = cropBox.left / Math.abs(this.sprite.scale.x);
  60.         let y = cropBox.top / Math.abs(this.sprite.scale.y);
  61.         let width = cropBox.width / Math.abs(this.sprite.scale.x);
  62.         let height = cropBox.height / Math.abs(this.sprite.scale.y);
  63.         const frame = this.sprite.texture.frame;
  64.         const texture = this.sprite.texture;
  65.  
  66.         if(x < 0) x = 0;
  67.         if(y < 0) y = 0;
  68.        
  69.         if(!(this.sprite.angle % 270) && this.sprite.angle != 0) {
  70.             let tempWidth = width;
  71.             y = x;
  72.             x = texture.width - height - y;
  73.  
  74.             width = height;
  75.             height = tempWidth;    
  76.         } else if(!(this.sprite.angle % 180) && this.sprite.angle != 0) {
  77.             x = texture.width - x - width;
  78.             y = texture.height - y - height;
  79.         } else if(!(this.sprite.angle % 90) && this.sprite.angle != 0) {
  80.             let tempX = x;
  81.             let tempWidth = width;
  82.            
  83.             x = y;
  84.             y = tempX;
  85.  
  86.             width = height;
  87.             height = tempWidth;
  88.         }
  89.  
  90.         return { x, y, width, height };
  91.     }
  92.  
  93.     crop() {
  94.         if(!this.cr) return;
  95.         const base = this.sprite.texture.baseTexture;        
  96.         let { x, y, width, height } = this.calculateCropPosition();
  97.        
  98.         const frame = this.sprite.texture.frame;
  99.  
  100.         if(x < 0) x = 0;
  101.         if(y < 0) y = 0;
  102.        
  103.         if(frame) {
  104.             x += frame.x;
  105.             y += frame.y;
  106.         }
  107.        
  108.         if(x > base.width || x + width > base.width) {
  109.             x = 0; width = base.width;
  110.         }
  111.        
  112.         if(y > base.height || y + height > base.height) {
  113.             y = 0; height = base.height;
  114.         }
  115.        
  116.         const rect = new PIXI.Rectangle(x, y, width, height);
  117.         this.sprite.texture.frame = rect;
  118.         this.drawFit();
  119.        
  120.         this.cr.destroy();
  121.         this.cr = false;
  122.     }
  123.  
  124.     activateCrop() {
  125.         const parentElement = document.querySelector('.take-selfie__holder');
  126.         let canvas = document.getElementById('crop_canvas');
  127.  
  128.         const { x, y, width, height } = this.app.stage.getBounds();
  129.        
  130.         canvas.width = width;
  131.         canvas.height = height;
  132.  
  133.         canvas.parentElement.setAttribute(
  134.             'style', `position: absolute; left: ${x}px; top: ${y}px`
  135.         );
  136.  
  137.         if(this.cr) this.cr.destroy();
  138.         this.cr = new Cropper(canvas, {});
  139.     }
  140.  
  141.     drawFit() {
  142.         const originImageWidth = Math.abs(this.sprite.width / this.sprite.scale.x);
  143.         const originImageHeight = Math.abs(this.sprite.height / this.sprite.scale.y);
  144.  
  145.         const isCalculated = !(this.sprite.angle % 180);
  146.  
  147.         const w = !isCalculated ? originImageHeight : originImageWidth;
  148.         const h = !isCalculated ? originImageWidth : originImageHeight;
  149.  
  150.         const cw = this.app.renderer.width;
  151.         const ch = this.app.renderer.height;
  152.        
  153.         const scale = this.calcScale(cw, ch, w, h);
  154.         this.sprite.scale.set(
  155.             scale * (this.sprite.scale.x < 0 ? -1 : 1),
  156.             scale * (this.sprite.scale.y < 0 ? -1 : 1)
  157.         );
  158.  
  159.         const centerX = (!isCalculated ? this.sprite.height : this.sprite.width);
  160.         const centerY = (!isCalculated ? this.sprite.width : this.sprite.height);
  161.  
  162.         this.sprite.position.set(
  163.             this.app.renderer.width / 2,
  164.             this.app.renderer.height / 2
  165.         );
  166.  
  167.         if(!(this.sprite.angle % 360)) {
  168.             this.sprite.position.x -= this.sprite.width / 2;
  169.             this.sprite.position.y -= this.sprite.height / 2;
  170.         } else if(!(this.sprite.angle % 270) && this.sprite.angle != 0) {
  171.             this.sprite.position.x -= this.sprite.height / 2;
  172.             this.sprite.position.y += this.sprite.width / 2;
  173.         } else if(!(this.sprite.angle % 180) && this.sprite.angle != 0) {
  174.             this.sprite.position.x += this.sprite.width / 2;
  175.             this.sprite.position.y += this.sprite.height / 2;
  176.         } else if(!(this.sprite.angle % 90) && this.sprite.angle != 0) {
  177.             this.sprite.position.x += this.sprite.height / 2;
  178.             this.sprite.position.y -= this.sprite.width / 2;
  179.         }
  180.     }
  181.  
  182.     rotate(angle = 1) {
  183.         this.sprite.angle += (angle > 0 ? 90 : -90);
  184.         if(this.sprite.angle > 360) {
  185.             this.sprite.angle = 90;
  186.         }
  187.  
  188.         if(this.sprite.angle < 0) {
  189.             this.sprite.angle = 270;
  190.         }
  191.  
  192.         this.opts.angle = this.sprite.angle;
  193.         // this.sprite.anchor.set(.5, .5);
  194.  
  195.         this.drawFit();
  196.     }
  197.  
  198.     calcScale(w, h, width, height) {
  199.         return Math.min(w / width, h / height);
  200.     }
  201.  
  202.     async loadImage(image, canvas) {
  203.         const texture = await PIXI.Texture.fromURL(image.getAttribute('src'));
  204.         this.sprite = new PIXI.Sprite(texture);
  205.  
  206.         const scale = this.calcScale(
  207.             this.app.renderer.width,
  208.             this.app.renderer.height,
  209.             this.sprite.width,
  210.             this.sprite.height
  211.         );
  212.  
  213.         this.sprite.scale.set(scale, scale);
  214.         this.sprite.position.set(
  215.             (this.app.renderer.width - this.sprite.width) / 2,
  216.             (this.app.renderer.height - this.sprite.height) / 2
  217.         );
  218.        
  219.         this.app.stage.addChild(this.sprite);
  220.         canvas.replaceWith(this.app.view);
  221.         this.app.view.setAttribute('id', 'canvas');
  222.     }
  223. }
Add Comment
Please, Sign In to add comment