Advertisement
Avdluna

2 MEU DEUS DO CEU

Nov 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, ViewChild, ElementRef, Input, Output, EventEmitter } from '@angular/core';
  2. import { FormControl,FormBuilder, Validators } from '@angular/forms';
  3. import { NgxCroppieComponent } from 'ngx-croppie';
  4. import { CroppieOptions } from 'croppie';
  5. import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
  6. import { BadgeImageEditorComponent } from 'src/app/badge-image-editor/badge-image-editor.component';
  7.  
  8. @Component({
  9.   selector: 'app-identification-badge',
  10.   templateUrl: './identification-badge.component.html',
  11.   styleUrls: ['./identification-badge.component.css']
  12. })
  13.  
  14. export class IdentificationBadgeComponent {
  15.  
  16.   @ViewChild('myCanvas') canvasRef: ElementRef;
  17.  
  18.   reader  = new FileReader();
  19.   image = this.fb.group({
  20.     fileInput: this.fb.control('', Validators.required),
  21.   });
  22.   name = new FormControl('', Validators.required);
  23.   lastName =  new FormControl('', Validators.required);
  24.   file = new FormControl({value: 'None', disabled: true}, Validators.required);
  25.   url = '';
  26.   event;
  27.  
  28.   constructor(private fb: FormBuilder, public dialog: MatDialog) {
  29.     this.image.get('fileInput').valueChanges.subscribe(r => {
  30.       this.file.setValue(r.replace(/^.*[\\\/]/, ''));
  31.     });
  32.     this.file.setValue('Nenhum arquivo carregado');
  33.    }
  34.  
  35.   openDialog() {
  36.     let dialogRef = this.dialog.open(BadgeImageEditorComponent, {
  37.       height:'600px',
  38.       width:'600px',
  39.       data : {url: this.url}
  40.     });
  41.  
  42.     dialogRef.afterClosed().subscribe(result => {
  43.       if(result){
  44.         this.url = result.dataURL;
  45.         this.file.setValue(result.name);      
  46.       }
  47.        
  48.     });
  49.   }
  50.  
  51.    /**
  52.     * Enables hidden input for loading user image
  53.     */
  54.    uploadPhoto(){
  55.      this.openDialog();
  56.     //document.getElementById('file-input').click();
  57.    }
  58.  
  59.    /**
  60.     * Displays user's chosen image
  61.     * @param image choose event
  62.     */
  63.    showImage(event){
  64.  
  65.       let reader = new FileReader();
  66.  
  67.       reader.readAsDataURL(event.target.files[0]);
  68.  
  69.       reader.onload = (event) => {
  70.         this.url = event.target.result;
  71.         this.openDialog();
  72.         this.event = event;
  73.       };
  74.    }
  75.  
  76.  
  77.   /**
  78.    * Returns error message for empty name field
  79.    */
  80.    getNameErrorMessage() {
  81.       if(this.name.hasError('required')){
  82.         return 'Você precisa preencher este campo'
  83.       }
  84.       return '';
  85.  
  86.   }
  87.  
  88.    /**
  89.    * Returns error message for empty last name field
  90.    */
  91.   getLastNameErrorMessage() {
  92.     if(this.lastName.hasError('required')){
  93.       return 'Você precisa preencher este campo';
  94.     }
  95.     return '';
  96. }
  97.  
  98.  /**
  99.    * Returns error message for empty file field
  100.    */
  101.   getFileErrorMessage() {
  102.     if(this.file.hasError('required')){
  103.       return 'Você precisa preencher este campo'
  104.     }
  105.     return '';
  106.   }
  107.  
  108.   /**
  109.    * Generate badge image
  110.    */
  111.   generateBadge(){
  112.     const canvas = this.canvasRef.nativeElement;
  113.    
  114.     this.configureCanvas(canvas);
  115.  
  116.     this.downloadBadge(canvas);
  117.   };
  118.  
  119.   /**
  120.    * Create an href element for badge download
  121.    */
  122.  
  123.   downloadBadge(canvas){
  124.       const a = document.createElement('a');
  125.       a.href = canvas.toDataURL('image/jpeg');
  126.       a.download = 'idenfication-badge.jpg';
  127.       document.body.appendChild(a);
  128.       a.click();
  129.     }
  130.  
  131.     /**
  132.      * Configure HTML canvas to display the images one on top of the other and display text
  133.      * @param canvas
  134.      */
  135.     configureCanvas(canvas){
  136.       const imgUser = document.getElementById('user-image');
  137.       const imgVirtus = document.getElementById('virtus-image');
  138.  
  139.       const ctx = canvas.getContext('2d');
  140.       ctx.canvas.width = 3190;
  141.       ctx.canvas.height = 5079;
  142.  
  143.       ctx.drawImage(imgUser, 0,0,3190,3700);
  144.       ctx.drawImage(imgVirtus, 0,3700,3190,1400);
  145.      
  146.       ctx.fillStyle = '#112369';
  147.       ctx.font = '200px Sans-Serif';
  148.       ctx.fillText(this.name.value,200,4000);
  149.  
  150.       ctx.fillStyle = '#112369';
  151.       ctx.font = '250px Sans-Serif';
  152.       ctx.fillText(this.lastName.value,200,4250);
  153.     }
  154.  
  155.   /**
  156.    * Empty all fields, returning to original state
  157.    */
  158.   discardBadge(){
  159.     this.file.setValue('Nenhum arquivo carregado');
  160.     this.name.setValue('');
  161.     this.lastName.setValue('');
  162.     this.url = '';
  163.   };
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement