konvajs / ng2-konva

Angular & Canvas - JavaScript library for drawing complex canvas graphics using Angular.
http://rafaelescala.com/ng2-konva/
113 stars 32 forks source link

Documentation on ko-image #22

Open ephes opened 5 years ago

ephes commented 5 years ago

Hi *,

I spend half of today figuring out how to display an image on a konva layer in an angular app. Maybe an example in the documentation regarding image handling could save noobs like me a lot of time. But maybe it's just me. Anyway, here's what worked for me (disclaimer: since I'm a javascript beginner, maybe my approach is completely wrong):

import { Component, OnInit, EventEmitter } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { Observable, of } from 'rxjs';

// declare window to remove typescript warning
interface Window {
  Image: any;
}
declare const window: Window;

@Component({
  selector: 'app-detail',
  templateUrl: './app-detail.component.html',
  styleUrls: ['./app-detail.component.css']
})
export class DetailComponent implements OnInit {
  public configStage: Observable<any> = of({
    width: 700,
    height: 1000
  });

  public configImage:EventEmitter<any> = new EventEmitter();

  constructor(private route: ActivatedRoute) { }

  showImage(src: string) {
    const image = new window.Image();
    image.src = src;
    image.onload = () => {
      this.configImage.emit({
        image: image,
      })
    }
  }

  ngOnInit() {
    this.showImage('/path/to/image');
  }
}
saucey commented 5 years ago

This screwed me up for a few hours, Brilliant Thanks!!!

victacora commented 5 years ago

Thank you :D

torhy13 commented 5 years ago

God bless you ephes! Thanks a LOT! Guys, please pay attention to this issue! This is important!

bcrigler commented 4 years ago

Thank you for doing this... this and many other things need to be in the documentation, for sure.

Why did you use EventEmitter? Why not just use an Observable like the configStage property?

viradia2 commented 4 years ago

Hi @ephes, I am new to Konva and writing code in Angular 6+ so I can load images. I have used ko-circle and other shapes but how to load images.