import IPlatform from "./IPlatform" ; export default class WebPlatform implements IPlatform { public name = "WEB" ; public Image = Image ; public Promise = Promise ; public buffers = [ ] ; public getBuffer ( width ? , height ? , clear ? ) { let cvs ; if ( this . buffers . length ) { if ( width ) { for ( let i = 0 ; i < this . buffers . length ; i ++ ) { if ( this . buffers [ i ] . width === width && this . buffers [ i ] . height === height ) { cvs = this . buffers . splice ( i , 1 ) [ 0 ] ; break ; } } } else { cvs = this . buffers . pop ( ) ; } if ( cvs ) { if ( clear ) { cvs . getContext ( "2d" ) . clearRect ( 0 , 0 , cvs . width , cvs . height ) ; } return cvs ; } } cvs = document . createElement ( "canvas" ) ; if ( width ) { cvs . width = width ; cvs . height = height ; } return cvs ; } public freeBuffer ( buffer ) { this . buffers . push ( buffer ) ; } public loadAsset ( img , url , loaded , error ) { img . crossOrigin = "Anonymous" ; img . addEventListener ( "load" , loaded ) ; img . addEventListener ( "error" , error ) ; img . src = url ; } public requestAnimationFrame ( cb ) { window . requestAnimationFrame ( cb ) ; } }