Skip to content

Commit 6c84d8f

Browse files
committed
Merge pull request #26 from izgzhen/master
add withImage
2 parents a0c6c4e + 397ba2c commit 6c84d8f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

docs/Graphics/Canvas.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ Opaque object describing a gradient.
6565
canvasElementToImageSource :: CanvasElement -> CanvasImageSource
6666
```
6767

68+
#### `withImage`
69+
70+
``` purescript
71+
withImage :: forall eff a. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
72+
```
73+
6874
#### `getCanvasElementById`
6975

7076
``` purescript

src/Graphics/Canvas.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ 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 {};
20+
}
21+
};
22+
};
23+
1024
exports.getCanvasElementByIdImpl = function(id, Just, Nothing) {
1125
return function() {
1226
var el = document.getElementById(id);

src/Graphics/Canvas.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ module Graphics.Canvas
8080
, restore
8181
, withContext
8282

83+
, withImage
8384
, getImageData
8485
, getImageDataWidth
8586
, getImageDataHeight
@@ -132,6 +133,9 @@ foreign import data CanvasGradient :: *
132133

133134
foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
134135

136+
-- | Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
137+
foreign import withImage :: forall eff a. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
138+
135139
foreign import getCanvasElementByIdImpl ::
136140
forall r eff. Fn3 String
137141
(CanvasElement -> r)

0 commit comments

Comments
 (0)