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 @@ -65,10 +65,10 @@ Opaque object describing a gradient.
6565canvasElementToImageSource :: CanvasElement -> CanvasImageSource
6666```
6767
68- #### ` withImage `
68+ #### ` tryLoadImage `
6969
7070``` purescript
71- withImage :: forall eff. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
71+ tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
7272```
7373
7474Wrapper 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 @@ -5,16 +5,21 @@ exports.canvasElementToImageSource = function(e) {
55 return e ;
66} ;
77
8- exports . withImage = function ( src ) {
9- return function ( f ) {
10- return function ( ) {
11- var img = new Image ( ) ;
12- img . src = src ;
13- img . addEventListener ( "load" , function ( ) {
14- f ( img ) ( ) ;
15- } , false ) ;
16-
17- return { } ;
8+ exports . tryLoadImageImpl = function ( src ) {
9+ return function ( e ) {
10+ return function ( f ) {
11+ return function ( ) {
12+ var img = new Image ( ) ;
13+ img . src = src ;
14+ img . addEventListener ( "load" , function ( ) {
15+ f ( img ) ( ) ;
16+ } , false ) ;
17+ img . addEventListener ( "error" , function ( error ) {
18+ e ( ) ;
19+ } , false ) ;
20+
21+ return { } ;
22+ }
1823 }
1924 } ;
2025} ;
Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ module Graphics.Canvas
8484 , restore
8585 , withContext
8686
87- , withImage
87+ , tryLoadImage
8888 , getImageData
8989 , putImageData
9090 , putImageDataFull
@@ -139,8 +139,12 @@ foreign import data CanvasGradient :: *
139139
140140foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
141141
142+ foreign import tryLoadImageImpl :: forall eff . String -> Eff (canvas :: Canvas | eff ) Unit -> (CanvasImageSource -> Eff (canvas :: Canvas | eff ) Unit ) -> Eff (canvas :: Canvas | eff ) Unit
143+
142144-- | Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
143- foreign import withImage :: forall eff . String -> (CanvasImageSource -> Eff eff Unit ) -> Eff eff Unit
145+ tryLoadImage :: forall eff . String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff ) Unit ) -> Eff (canvas :: Canvas | eff ) Unit
146+ tryLoadImage path k = tryLoadImageImpl path (k Nothing ) (k <<< Just )
147+
144148
145149foreign import getCanvasElementByIdImpl ::
146150 forall r eff . Fn3 String
You can’t perform that action at this time.
0 commit comments