Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to load the image inside canvas without chopping off #37

Open
eshwerintouch opened this issue Oct 30, 2018 · 8 comments
Open

Need to load the image inside canvas without chopping off #37

eshwerintouch opened this issue Oct 30, 2018 · 8 comments

Comments

@eshwerintouch
Copy link

Hi team,

The library was very nice. But I see that the original image was chopped off on top and bottom.

I mean that the image width was completely fine but it was not covering the all content when it comes to height. Some of the contents on top and bottom of the image was deleted when it was on edit mode.

Please fix it OR let me know if there's any option to load it completely.

@eshwerintouch
Copy link
Author

Even after giving AspectRatio = 0 the canvas still cut of some part of the image.

@Fridthjof
Copy link

Did you find somewhat of a solution?

i currently got the same issue.

@eshwerintouch
Copy link
Author

@Fridthjof Nothing particularly. But I found out that if we give the container height and width same as the original image aspect ratio it loads up fine. As the high resolution images go out of the window if I try to give the same height dynamically by getting the original height now I do resize the image using ngx-pic as per my container width and load the image so I get the exact image.

@eshwerintouch
Copy link
Author

@Fridthjof reference link where I have used the editor

@Fridthjof
Copy link

I Fixed it by dynamically setting aspectRatio, by dividing the uploaded image height and width and inserting that value into the aspectRatio option.

@eshwerintouch
Copy link
Author

eshwerintouch commented Oct 31, 2018 via email

@Fridthjof
Copy link

Fridthjof commented Oct 31, 2018

Sure.

Component that uses the lib.

imageUrl: null;
    filedUploaded = false;
    aspectRatio = null;

uploadImageUrl(event) {
        this.readImage(event.target);
    }

private readImage(inputValue: any): void {
        const file: File = inputValue.files[0];
        const myReader: FileReader = new FileReader();

        myReader.onloadend = (e) => {
            this.imageUrl = myReader.result; // Base64 encoding the image
        };
        myReader.readAsDataURL(file);

        const img = new Image();
        img.src = window.URL.createObjectURL(file);
        img.onload = (fn) => {
            this.aspectRatio = (img.naturalHeight / img.naturalWidth); // Calculation of the aspectRatio
            this.filedUploaded = true;
        };
    }

HTML.

<div *ngIf="filedUploaded && imageUrl">
       <div style="height: 50%; width: 50%">
            <app-image-editor
                            [incImageUrl]="imageUrl"
                            [incAspectRatio]="aspectRatio">
            </app-image-editor>
        </div>
 </div>

My image-editor lib

@Component({
    selector: 'app-image-editor',
    viewProviders: [CanvasWhiteboardComponent],
    templateUrl: 'image-editor.component.html',
    encapsulation: ViewEncapsulation.None
})

export class ImageEditorComponent implements OnInit {
    @Input() incImageUrl: string;
    @Input() incAspectRatio: number;

    canvasOptions: CanvasWhiteboardOptions;

    ngOnInit() {

        this.canvasOptions = {
            drawButtonEnabled: false,
            drawButtonClass: 'drawButtonClass',
            drawButtonText: 'Drawing',
            drawingEnabled: true,
            clearButtonEnabled: true,
            clearButtonClass: 'clearButtonClass',
            clearButtonText: 'Clear',
            undoButtonText: 'Undo',
            undoButtonEnabled: true,
            redoButtonText: 'Redo',
            redoButtonEnabled: true,
            colorPickerEnabled: true,
            saveDataButtonEnabled: true,
            saveDataButtonText: 'Save',
            lineWidth: 5,
            strokeColor: 'rgb(0,0,0)',
            shouldDownloadDrawing: true,
            imageUrl: this.incImageUrl,
            aspectRatio: this.incAspectRatio
        };
     }
}

My image-editor HTML

<canvas-whiteboard #canvasWhiteboard
                   [options]="canvasOptions"
                   (onBatchUpdate)="sendBatchUpdate($event)"
                   (onClear)="onCanvasClear()"
                   (onUndo)="onCanvasUndo($event)"
                   (onRedo)="onCanvasRedo($event)"
                   (onSave)="onSave($event)"
                   (onImageLoaded)="check($event)">
</canvas-whiteboard>

@ashishvspl076
Copy link

@eshwerintouch @Fridthjof have u solved image chopping issue ? i am also facing this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants