File tree Expand file tree Collapse file tree 3 files changed +23
-14
lines changed
Expand file tree Collapse file tree 3 files changed +23
-14
lines changed Original file line number Diff line number Diff line change @@ -57,10 +57,10 @@ Opaque object describing a gradient.
5757canvasElementToImageSource :: CanvasElement -> CanvasImageSource
5858```
5959
60- #### ` withImage `
60+ #### ` tryLoadImage `
6161
6262``` purescript
63- withImage :: forall eff. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
63+ tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
6464```
6565
6666Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
Original file line number Diff line number Diff line change @@ -7,16 +7,21 @@ exports.canvasElementToImageSource = function(e) {
77 return e ;
88} ;
99
10- exports . withImage = function ( src ) {
11- return function ( f ) {
12- return function ( ) {
13- var img = new Image ( ) ;
14- img . src = src ;
15- img . addEventListener ( "load" , function ( ) {
16- f ( img ) ( ) ;
17- } , false ) ;
18-
19- return { } ;
10+ exports . tryLoadImageImpl = function ( src ) {
11+ return function ( e ) {
12+ return function ( f ) {
13+ return function ( ) {
14+ var img = new Image ( ) ;
15+ img . src = src ;
16+ img . addEventListener ( "load" , function ( ) {
17+ f ( img ) ( ) ;
18+ } , false ) ;
19+ img . addEventListener ( "error" , function ( error ) {
20+ e ( ) ;
21+ } , false ) ;
22+
23+ return { } ;
24+ }
2025 }
2126 } ;
2227} ;
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ module Graphics.Canvas
7979 , restore
8080 , withContext
8181
82- , withImage
82+ , tryLoadImage
8383 , getImageData
8484 , putImageData
8585 , putImageDataFull
@@ -128,8 +128,12 @@ foreign import data CanvasGradient :: *
128128
129129foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
130130
131+ foreign import tryLoadImageImpl :: forall eff . String -> Eff (canvas :: Canvas | eff ) Unit -> (CanvasImageSource -> Eff (canvas :: Canvas | eff ) Unit ) -> Eff (canvas :: Canvas | eff ) Unit
132+
131133-- | Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
132- foreign import withImage :: forall eff . String -> (CanvasImageSource -> Eff eff Unit ) -> Eff eff Unit
134+ tryLoadImage :: forall eff . String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff ) Unit ) -> Eff (canvas :: Canvas | eff ) Unit
135+ tryLoadImage path k = tryLoadImageImpl path (k Nothing ) (k <<< Just )
136+
133137
134138foreign import getCanvasElementByIdImpl ::
135139 forall r eff . Fn3 String
You can’t perform that action at this time.
0 commit comments