Skip to content

Commit 7472587

Browse files
committed
Fix #36, updates for 0.10
1 parent c87e225 commit 7472587

File tree

6 files changed

+112
-34
lines changed

6 files changed

+112
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
output/
22
node_modules/
33
bower_components/
4+
.psc-ide-port
5+
.psc-package/
46

57
# OS generated files #
68
######################

bower.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"url": "git://github.com/paf31/purescript-canvas.git"
2121
},
2222
"dependencies": {
23-
"purescript-eff": "^1.0.0",
24-
"purescript-functions": "^1.0.0",
25-
"purescript-maybe": "^1.0.0",
26-
"purescript-exceptions": "^1.0.0",
23+
"purescript-eff": "^2.0.0",
24+
"purescript-functions": "^2.0.0",
25+
"purescript-maybe": "^2.0.0",
26+
"purescript-exceptions": "^2.0.0",
2727
"purescript-arraybuffer-types": "^0.2.0"
2828
}
2929
}

docs/Graphics/Canvas.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A 2D graphics context.
3030
#### `ImageData`
3131

3232
``` purescript
33-
type ImageData = { width :: Int, height :: Int, data :: Uint8ClampedArray }
33+
data ImageData :: *
3434
```
3535

3636
An image data object, used to store raster data outside the canvas.
@@ -68,10 +68,10 @@ canvasElementToImageSource :: CanvasElement -> CanvasImageSource
6868
#### `tryLoadImage`
6969

7070
``` purescript
71-
tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
71+
tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: CANVAS | eff) Unit) -> Eff (canvas :: CANVAS | eff) Unit
7272
```
7373

74-
Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
74+
Asynchronously load an image file by specifying its path.
7575

7676
#### `getCanvasElementById`
7777

@@ -681,6 +681,30 @@ createImageDataCopy :: forall eff. Context2D -> ImageData -> Eff (canvas :: CANV
681681

682682
Create a copy of an image data object.
683683

684+
#### `imageDataWidth`
685+
686+
``` purescript
687+
imageDataWidth :: ImageData -> Int
688+
```
689+
690+
Get the width of an `ImageData` object.
691+
692+
#### `imageDataHeight`
693+
694+
``` purescript
695+
imageDataHeight :: ImageData -> Int
696+
```
697+
698+
Get the height of an `ImageData` object.
699+
700+
#### `imageDataBuffer`
701+
702+
``` purescript
703+
imageDataBuffer :: ImageData -> Uint8ClampedArray
704+
```
705+
706+
Get the underlying buffer from an `ImageData` object.
707+
684708
#### `drawImage`
685709

686710
``` purescript

psc-package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "canvas",
3+
"source": "https://github.com/purescript/package-sets.git",
4+
"set": "psc-0.10.1",
5+
"depends": [
6+
"arraybuffer-types",
7+
"bifunctors",
8+
"control",
9+
"eff",
10+
"either",
11+
"exceptions",
12+
"foldable-traversable",
13+
"functions",
14+
"invariant",
15+
"maybe",
16+
"monoid",
17+
"newtype",
18+
"prelude"
19+
]
20+
}

src/Graphics/Canvas.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,18 @@ exports.restore = function(ctx) {
401401
};
402402
};
403403

404+
exports.imageDataWidth = function(image) {
405+
return image.width;
406+
};
407+
408+
exports.imageDataHeight = function(image) {
409+
return image.height;
410+
};
411+
412+
exports.imageDataBuffer = function(image) {
413+
return image.data;
414+
};
415+
404416
exports.getImageData = function(ctx) {
405417
return function(x) {
406418
return function(y) {

src/Graphics/Canvas.purs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
-- | Canvas API.
33

44
module Graphics.Canvas
5-
( CANVAS()
6-
, CanvasElement()
7-
, Context2D()
8-
, ImageData()
9-
, CanvasImageSource()
10-
, Arc()
5+
( CANVAS
6+
, CanvasElement
7+
, Context2D
8+
, ImageData
9+
, CanvasImageSource
10+
, Arc
1111
, Composite(..)
12-
, Dimensions()
12+
, Dimensions
1313
, LineCap(..)
1414
, LineJoin(..)
15-
, Rectangle()
16-
, ScaleTransform()
17-
, TextMetrics()
18-
, Transform()
19-
, TranslateTransform()
15+
, Rectangle
16+
, ScaleTransform
17+
, TextMetrics
18+
, Transform
19+
, TranslateTransform
2020
, TextAlign(..)
21-
, CanvasPattern()
21+
, CanvasPattern
2222
, PatternRepeat(..)
23-
, CanvasGradient()
24-
, LinearGradient()
25-
, RadialGradient()
26-
, QuadraticCurve()
27-
, BezierCurve()
23+
, CanvasGradient
24+
, LinearGradient
25+
, RadialGradient
26+
, QuadraticCurve
27+
, BezierCurve
2828

2929
, getCanvasElementById
3030
, getContext2D
@@ -90,6 +90,9 @@ module Graphics.Canvas
9090
, putImageDataFull
9191
, createImageData
9292
, createImageDataCopy
93+
, imageDataWidth
94+
, imageDataHeight
95+
, imageDataBuffer
9396

9497
, canvasElementToImageSource
9598
, drawImage
@@ -108,13 +111,13 @@ module Graphics.Canvas
108111
, bezierCurveTo
109112
) where
110113

111-
import Prelude (class Show, Unit, pure, bind, (<$>), (<>), ($), (>>=))
114+
import Prelude
112115

116+
import Control.Monad.Eff (Eff)
117+
import Control.Monad.Eff.Exception.Unsafe (unsafeThrow)
113118
import Data.ArrayBuffer.Types (Uint8ClampedArray)
114119
import Data.Function.Uncurried (Fn3, runFn3)
115120
import Data.Maybe (Maybe(..))
116-
import Control.Monad.Eff (Eff)
117-
import Control.Monad.Eff.Exception.Unsafe (unsafeThrow)
118121

119122
-- | The `Canvas` effect denotes computations which read/write from/to the canvas.
120123
foreign import data CANVAS :: !
@@ -126,7 +129,7 @@ foreign import data CanvasElement :: *
126129
foreign import data Context2D :: *
127130

128131
-- | An image data object, used to store raster data outside the canvas.
129-
type ImageData = { width :: Int, height :: Int, data :: Uint8ClampedArray }
132+
foreign import data ImageData :: *
130133

131134
-- | Opaque object for drawing elements and things to the canvas.
132135
foreign import data CanvasImageSource :: *
@@ -139,13 +142,21 @@ foreign import data CanvasGradient :: *
139142

140143
foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
141144

142-
foreign import tryLoadImageImpl :: forall eff. String -> Eff (canvas :: Canvas | eff) Unit -> (CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
143-
144-
-- | Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
145-
tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
145+
foreign import tryLoadImageImpl
146+
:: forall eff
147+
. String
148+
-> Eff (canvas :: CANVAS | eff) Unit
149+
-> (CanvasImageSource -> Eff (canvas :: CANVAS | eff) Unit)
150+
-> Eff (canvas :: CANVAS | eff) Unit
151+
152+
-- | Asynchronously load an image file by specifying its path.
153+
tryLoadImage
154+
:: forall eff
155+
. String
156+
-> (Maybe CanvasImageSource -> Eff (canvas :: CANVAS | eff) Unit)
157+
-> Eff (canvas :: CANVAS | eff) Unit
146158
tryLoadImage path k = tryLoadImageImpl path (k Nothing) (k <<< Just)
147159

148-
149160
foreign import getCanvasElementByIdImpl ::
150161
forall r eff. Fn3 String
151162
(CanvasElement -> r)
@@ -562,6 +573,15 @@ foreign import createImageData :: forall eff. Context2D -> Number -> Number -> E
562573
-- | Create a copy of an image data object.
563574
foreign import createImageDataCopy :: forall eff. Context2D -> ImageData -> Eff (canvas :: CANVAS | eff) ImageData
564575

576+
-- | Get the width of an `ImageData` object.
577+
foreign import imageDataWidth :: ImageData -> Int
578+
579+
-- | Get the height of an `ImageData` object.
580+
foreign import imageDataHeight :: ImageData -> Int
581+
582+
-- | Get the underlying buffer from an `ImageData` object.
583+
foreign import imageDataBuffer :: ImageData -> Uint8ClampedArray
584+
565585
foreign import drawImage :: forall eff. Context2D -> CanvasImageSource -> Number -> Number -> Eff (canvas :: CANVAS | eff) Context2D
566586

567587
foreign import drawImageScale :: forall eff. Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Eff (canvas :: CANVAS | eff) Context2D

0 commit comments

Comments
 (0)