Skip to content

add withImage #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/Graphics/Canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Opaque object describing a gradient.
canvasElementToImageSource :: CanvasElement -> CanvasImageSource
```

#### `withImage`

``` purescript
withImage :: forall eff a. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
```

#### `getCanvasElementById`

``` purescript
Expand Down
14 changes: 14 additions & 0 deletions src/Graphics/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ exports.canvasElementToImageSource = function(e) {
return e;
};

exports.withImage = function (src) {
return function(f) {
return function () {
var img = new Image();
img.src = src;
img.addEventListener("load", function() {
f(img)();
}, false);

return {};
}
};
};

exports.getCanvasElementByIdImpl = function(id, Just, Nothing) {
return function() {
var el = document.getElementById(id);
Expand Down
4 changes: 4 additions & 0 deletions src/Graphics/Canvas.purs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ module Graphics.Canvas
, restore
, withContext

, withImage
, getImageData
, getImageDataWidth
, getImageDataHeight
Expand Down Expand Up @@ -132,6 +133,9 @@ foreign import data CanvasGradient :: *

foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource

-- | Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
foreign import withImage :: forall eff a. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment to this?


foreign import getCanvasElementByIdImpl ::
forall r eff. Fn3 String
(CanvasElement -> r)
Expand Down