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 - Import html2canvas

From Dev

html2canvas in angular 4

From Dev

JSPDF and html2canvas with Angular

From Dev

Dynamic function calling Angular 2

From Dev

Calling function in child container from parent in Angular 2

From Dev

Calling function from HTML to typescript not waited to finish

From Dev

.every function in typescript + angular 2

From Dev

Calling a TypeScript function in HTML?

From Dev

Angular 2 + Typescript displaying variable from within function

From

Angular2 canActivate() calling async function

From Dev

Html2canvas - document.getElementById(...).toDataURL is not a function

From Dev

How to save image from html2canvas

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 variables from typescript file in html template [angular]

From Dev

Calling typescript function from HTML using a module system

From Dev

Calling a function from another function in typescript

From Dev

calling angular2 component function from sigma.js bind events

From Dev

Calling angular function from swift

From Dev

Angular 2 - AOT - Calling function 'ChartModule', function calls not supported

From Dev

angular 2 function calling inside an http post function

From Dev

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

From Dev

Nested function in Angular2 using TypeScript

From Dev

Angular 2 typescript invoke javascript function

From Dev

angular2 typescript $.cookie() is not a function

From Dev

Passing function as JSON data in angular 2 typescript

From Dev

Calling two Html2Canvas calls using jQuery.when()

From Dev

Problem with calling javascript from angular typescript

From Dev

Calling Typescript function from Highcharts load event

Related Related

  1. 1

    Angular 2 - Import html2canvas

  2. 2

    html2canvas in angular 4

  3. 3

    JSPDF and html2canvas with Angular

  4. 4

    Dynamic function calling Angular 2

  5. 5

    Calling function in child container from parent in Angular 2

  6. 6

    Calling function from HTML to typescript not waited to finish

  7. 7

    .every function in typescript + angular 2

  8. 8

    Calling a TypeScript function in HTML?

  9. 9

    Angular 2 + Typescript displaying variable from within function

  10. 10

    Angular2 canActivate() calling async function

  11. 11

    Html2canvas - document.getElementById(...).toDataURL is not a function

  12. 12

    How to save image from html2canvas

  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 variables from typescript file in html template [angular]

  16. 16

    Calling typescript function from HTML using a module system

  17. 17

    Calling a function from another function in typescript

  18. 18

    calling angular2 component function from sigma.js bind events

  19. 19

    Calling angular function from swift

  20. 20

    Angular 2 - AOT - Calling function 'ChartModule', function calls not supported

  21. 21

    angular 2 function calling inside an http post function

  22. 22

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

  23. 23

    Nested function in Angular2 using TypeScript

  24. 24

    Angular 2 typescript invoke javascript function

  25. 25

    angular2 typescript $.cookie() is not a function

  26. 26

    Passing function as JSON data in angular 2 typescript

  27. 27

    Calling two Html2Canvas calls using jQuery.when()

  28. 28

    Problem with calling javascript from angular typescript

  29. 29

    Calling Typescript function from Highcharts load event

HotTag

Archive