import { NativeImage } from "electron" ; enum ImageSource { Inline = "inline" , Embedded = "embedded" } export class Image { canvas : HTMLCanvasElement ; path : string ; src : string ; type : ImageSource ; constructor ( path , canvasSource , width , height ) { this . init ( path , canvasSource , width , height ) ; } private init ( path , canvasSource , width , height ) { if ( canvasSource != null ) { let canvas = document . createElement ( "canvas" ) as HTMLCanvasElement ; canvas . width = width ; canvas . height = height ; let ctx = canvas . getContext ( "2d" ) ; ctx . drawImage ( canvasSource , 0 , 0 ) ; this . canvas = canvas ; } this . path = path ; } get isEmpty ( ) { return this . canvas == null ; } static empty ( ) { return new Image ( null , null , 0 , 0 ) ; } static load ( path ) { let nativeImage = NativeImage . createFromPath ( path ) ; let img = document . createElement ( "image" ) as HTMLImageElement ; img . src = nativeImage . toDataURL ( ) ; return new Image ( path , img , img . width , img . height ) ; } clone ( ) { return new Image ( this . path , this . canvas , this . width , this . height ) ; } get width ( ) { return this . canvas . width ; } get height ( ) { return this . canvas . height ; } serialize ( ) { return { type : ImageSource . Inline , src : this . canvas . toDataURL ( "image/png" ) } ; } deserialize ( obj , completeCallback = null ) { console . log ( obj ) ; if ( obj . type !== ImageSource . Inline ) return ; let img = document . createElement ( "img" ) as HTMLImageElement ; img . onload = ( ) => { console . log ( "image loaded" ) ; console . log ( img ) ; console . log ( img . width ) ; console . log ( img . height ) ; this . init ( "" , img , img . width , img . height ) ; completeCallback ( ) ; } ; img . src = obj . src ; } }