Calling Typescript function from html2canvas in angular 2

Pein

I am using html2canvas in angular 2 to capture the webpage but i need to send the string image to the server side using a webapi post call

imageDownload() {
    html2canvas(document.body).then(function (canvas) {
        var imgData = canvas.toDataURL("image/png");
        document.body.appendChild(canvas);
    });
}

I am unable to access this typescript function from html2canvas,how can i access this method to send the image string to the server side

AddImagesResource(image: any) {
    this.imagesService.addCanvasResource(image)
        .subscribe(response => {
            this.eventsEmitter.broadcast('Success', 'Changes Saved Succesfully');
        },
        error => {
            this.eventsEmitter.broadcast('Error', 'Error Occured');
        });
}
Vikas Mahajan

add in Package.json

"dependencies": {
"html2canvas": "0.5.0-beta4",
"@types/html2canvas": "0.5.32"
.......
}

run npm install.

you might encounter the error related html2canvas not found or undefined. to solve this with systemjs:

   map: {

  // angular bundles
  '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
  ...............

  // other libraries
  'rxjs': 'npm:rxjs',
  'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
  'html2canvas': 'npm:html2canvas' //for html canvas
 }
packages: {
  .................
   rxjs: {
     defaultExtension: 'js'
   },
   html2canvas: {
     main:'./dist/html2canvas.js',
     defaultExtension: 'js'
   }
 }

now you will have the complete reference of html2canvas.

i have created the imagedata as dataURI and Blob object of the image.

HTML:

 <div class="container" #container>    
   <img id="imgtag" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTexytN9PLhK7bFL-OofVQ9EYmXNhrZP9plY-_L6lKoycVmcjVw"
    alt="Smiley face" height="420" width="420">
   <div style="margin-top:20px;margin-left:20px">
    <button type="button" class="btn btn-secondary" (click)=snapShot()>Snap Shot</button>
   </div>
</div>

TypeScript Code:

 import { Component, ViewChild, ElementRef } from '@angular/core';
 import * as html2canvas from 'html2canvas';
 export class AppComponent {
 @ViewChild('container') container: ElementRef;
 snapShot() {
     let dataURI = this.createThumbnail(this.container.nativeElement);
 }

createThumbnail(blockelement: any) {
let element = blockelement.querySelector('#imgtag');
console.log(element, 'element')
if (element !== null && element !== undefined) {
  html2canvas(element).then(function (canvas: any) {
    let dataURI = canvas.toDataURL();
    console.log('img', dataURI);

    let blobObject = this.dataURItoBlob(dataURI);
    console.log('img', blobObject);
    //this.AddImagesResource(blobObject) your case
    //AddImagesResource will call the service method which can
    //post this blob object as multipart file
    //this formdata and httppost call should in service layer,in your case its imagesService.
    /*
      let formData = new FormData();
      formData.append('file', blobObject);
      console.log(formData, 'formData');
      return this.http.post(API_URL.saveThumbnailURL, formData).map((res: Response) => {
      console.log(res);
      return res.json();
    })        */
     }.bind(this));
   }
 }
  dataURItoBlob(dataURI: string) {
  let byteString: any;
  if (dataURI.split(',')[0].indexOf('base64') >= 0)
    byteString = atob(dataURI.split(',')[1]);
  else
    byteString = decodeURI(dataURI.split(',')[1]);

  // separate out the mime component
  let mimeString: any = dataURI.split(',')[0].split(':')[1].split(';')[0];

  // write the bytes of the string to a typed array
  let blob: any = new Uint8Array(byteString.length);
  for (let i = 0; i < byteString.length; i++) {
    blob[i] = byteString.charCodeAt(i);
  }
  return new Blob([blob], {
    type: mimeString
  });
}
}

you can send this stringdata/blob Object to server as multipart file using formdata.

Hope this will help

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 2 calling a Typescript function from Jquery causes uncaught TypeError

From Dev

TypeScript and Html2Canvas?

From Dev

Angular2 RxJS calling class function from map function

From Dev

calling parent component function from router component in angular2

From Dev

Calling function in child container from parent in Angular 2

From Dev

Calling a function in a typescript file from HTML.

From Dev

Calling typescript getter in angular2

From Dev

.every function in typescript + angular 2

From Dev

.every function in typescript + angular 2

From Dev

How to call a function from imported module in angular 2 (typescript)

From Dev

Angular 2 + Typescript displaying variable from within function

From Java

Angular2 canActivate() calling async function

From Dev

Create a div in HTML when calling a function in Angular 2

From Dev

Calling 2 functions from within a function

From Dev

calling angular2 component function from sigma.js bind events

From Dev

Calling angular function from swift

From Dev

Calling angular function from swift

From Dev

angular2-jwt - Calling function 'provideAuth', function calls are not supported

From Dev

angular 2 function calling inside an http post function

From Dev

Angular 2 Typescript: TypeError: this.validator is not a function

From Dev

Angular 2 typescript invoke javascript function

From Dev

Angular 2 typescript invoke javascript function

From Dev

Nested function in Angular2 using TypeScript

From Dev

angular2 typescript $.cookie() is not a function

From Dev

calling javascript function defined in html script tag from angular controller

From Dev

Calling a method from the constructor in Angular2/IONIC2

From Dev

Angular 2 custom validation not calling inside of component function

From Dev

Get item from Observable in Angular 2 with Typescript

From Dev

How would one access this from the Class inside a function in an object using TypeScript / Angular2

Related Related

  1. 1

    Angular 2 calling a Typescript function from Jquery causes uncaught TypeError

  2. 2

    TypeScript and Html2Canvas?

  3. 3

    Angular2 RxJS calling class function from map function

  4. 4

    calling parent component function from router component in angular2

  5. 5

    Calling function in child container from parent in Angular 2

  6. 6

    Calling a function in a typescript file from HTML.

  7. 7

    Calling typescript getter in angular2

  8. 8

    .every function in typescript + angular 2

  9. 9

    .every function in typescript + angular 2

  10. 10

    How to call a function from imported module in angular 2 (typescript)

  11. 11

    Angular 2 + Typescript displaying variable from within function

  12. 12

    Angular2 canActivate() calling async function

  13. 13

    Create a div in HTML when calling a function in Angular 2

  14. 14

    Calling 2 functions from within a function

  15. 15

    calling angular2 component function from sigma.js bind events

  16. 16

    Calling angular function from swift

  17. 17

    Calling angular function from swift

  18. 18

    angular2-jwt - Calling function 'provideAuth', function calls are not supported

  19. 19

    angular 2 function calling inside an http post function

  20. 20

    Angular 2 Typescript: TypeError: this.validator is not a function

  21. 21

    Angular 2 typescript invoke javascript function

  22. 22

    Angular 2 typescript invoke javascript function

  23. 23

    Nested function in Angular2 using TypeScript

  24. 24

    angular2 typescript $.cookie() is not a function

  25. 25

    calling javascript function defined in html script tag from angular controller

  26. 26

    Calling a method from the constructor in Angular2/IONIC2

  27. 27

    Angular 2 custom validation not calling inside of component function

  28. 28

    Get item from Observable in Angular 2 with Typescript

  29. 29

    How would one access this from the Class inside a function in an object using TypeScript / Angular2

HotTag

Archive