2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import {Injectable} from "@angular/core";
23 export interface ICanvasImage {
32 export class ImageCreatorService {
34 private _canvas:HTMLCanvasElement;
37 this._canvas = <HTMLCanvasElement>$('<canvas>')[0];
38 this._canvas.setAttribute('style', 'display:none');
40 let body = document.getElementsByTagName('body')[0];
41 body.appendChild(this._canvas);
45 * Create an image composed of different image layers
51 getMultiLayerBase64Image(canvasImages: ICanvasImage[], canvasWidth?:number, canvasHeight?:number):Promise<string> {
53 var promise = new Promise<string>((resolve, reject) => {
54 if(canvasImages && canvasImages.length === 0){
58 //If only width was set, use it for height, otherwise use first canvasImage height
59 canvasHeight = canvasHeight || canvasImages[0].height;
60 canvasWidth = canvasWidth || canvasImages[0].width;
64 const onImageLoaded = () => {
66 if(imagesLoaded < canvasImages.length){
69 this._canvas.setAttribute('width', (canvasWidth * 4).toString());
70 this._canvas.setAttribute('height', (canvasHeight * 4).toString());
71 const canvasCtx = this._canvas.getContext('2d');
73 canvasCtx.clearRect(0, 0, this._canvas.width, this._canvas.height);
74 images.forEach((image, index) => {
75 const canvasImage = canvasImages[index];
76 canvasCtx.drawImage(image, canvasImage.x, canvasImage.y, canvasImage.width, canvasImage.height);
79 let base64Image = this._canvas.toDataURL();
82 canvasImages.forEach(canvasImage => {
83 let image = new Image();
84 image.onload = onImageLoaded;
85 image.src = canvasImage.src;