Skip to content

line cap and dimensions bindings #1

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 2 commits into from
Aug 18, 2014
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

data Context2D :: *

type Dimensions = { height :: Number, width :: Number }

data LineCap where
Round :: LineCap
Square :: LineCap
Butt :: LineCap

type Rectangle = { h :: Number, w :: Number, y :: Number, x :: Number }

type ScaleTransform = { scaleY :: Number, scaleX :: Number }
Expand Down Expand Up @@ -39,8 +46,14 @@

fillRect :: forall eff. Context2D -> Rectangle -> Eff (canvas :: Canvas | eff) Context2D

getCanvasDimensions :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Dimensions

getCanvasElementById :: forall eff. String -> Eff (canvas :: Canvas | eff) CanvasElement

getCanvasHeight :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Number

getCanvasWidth :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Number

getContext2D :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Context2D

lineTo :: forall eff. Context2D -> Number -> Number -> Eff (canvas :: Canvas | eff) Context2D
Expand All @@ -57,8 +70,18 @@

scale :: forall eff. ScaleTransform -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

setCanvasDimensions :: forall eff. Dimensions -> CanvasElement -> Eff (canvas :: Canvas | eff) CanvasElement

setCanvasHeight :: forall eff. Number -> CanvasElement -> Eff (canvas :: Canvas | eff) CanvasElement

setCanvasWidth :: forall eff. Number -> CanvasElement -> Eff (canvas :: Canvas | eff) CanvasElement

setFillStyle :: forall eff. String -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

setLineCap :: forall eff. LineCap -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

setLineCapImpl :: forall eff. String -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

setLineWidth :: forall eff. Number -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

setShadowBlur :: forall eff. Number -> Context2D -> Eff (canvas :: Canvas | eff) Context2D
Expand Down
62 changes: 62 additions & 0 deletions src/Graphics/Canvas.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@ foreign import getContext2D
\ };\
\}" :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Context2D

foreign import getCanvasWidth
"function getCanvasWidth(canvas){\
\ return function(){\
\ return canvas.width;\
\ };\
\};" :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Number

foreign import getCanvasHeight
"function getCanvasHeight(canvas){\
\ return function(){\
\ return canvas.height;\
\ };\
\};" :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Number

foreign import setCanvasWidth
"function setCanvasWidth(width){\
\ return function(canvas){\
\ return function(){\
\ canvas.width = width;\
\ return canvas;\
\ };\
\ };\
\};" :: forall eff. Number -> CanvasElement -> Eff (canvas :: Canvas | eff) CanvasElement

foreign import setCanvasHeight
"function setCanvasHeight(height){\
\ return function(canvas){\
\ return function(){\
\ canvas.height = height;\
\ return canvas; \
\ };\
\ };\
\};" :: forall eff. Number -> CanvasElement -> Eff (canvas :: Canvas | eff) CanvasElement

type Dimensions = {width :: Number, height :: Number}

getCanvasDimensions :: forall eff. CanvasElement -> Eff (canvas :: Canvas | eff) Dimensions
getCanvasDimensions ce = do
w <- getCanvasWidth ce
h <- getCanvasHeight ce
return {width : w, height : h}

setCanvasDimensions :: forall eff. Dimensions -> CanvasElement -> Eff (canvas :: Canvas | eff) CanvasElement
setCanvasDimensions d ce = setCanvasHeight d.height ce >>= setCanvasWidth d.width

-- |
-- Context Properties
--
Expand Down Expand Up @@ -96,6 +141,23 @@ foreign import setShadowOffsetY
\ };\
\}" :: forall eff. Number -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

data LineCap = Round | Square | Butt

foreign import setLineCapImpl
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably not export this, but we need to fix the module exports for this module anyway.

"function setLineCapImpl(cap){\
\ return function(ctx) {\
\ return function() {\
\ ctx.lineCap = cap;\
\ return ctx;\
\ };\
\ };\
\}" :: forall eff. String -> Context2D -> Eff (canvas :: Canvas | eff) Context2D

setLineCap :: forall eff. LineCap -> Context2D -> Eff (canvas :: Canvas | eff) Context2D
setLineCap Round = setLineCapImpl "round"
setLineCap Square = setLineCapImpl "square"
setLineCap Butt = setLineCapImpl "butt"

-- |
-- Paths
--
Expand Down