Skip to content
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

Make it so that we can use Web.DOM.Document createElement with the purescript-canvas library. Useful for double buffering. #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Notable changes to this project are documented in this file. The format is based

Breaking changes:
- Added support for PureScript 0.14 and dropped support for all previous versions (#71)
- Functions now operate over Web.HTML.HTMLCanvasElement instead of CanvasElement. Kept CanvasElement around for reverse compatibility, but people will still need to switch to HTMLCanvasElement when defining Typeclass instances (#76)

New features:
- Added `Eq` instances for `LineJoin`, `Composite`, `TextAlign`, and `PatternRepeat` (#67)
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"purescript-functions": "master",
"purescript-maybe": "master",
"purescript-exceptions": "master",
"purescript-arraybuffer-types": "main"
"purescript-arraybuffer-types": "main",
"purescript-web-html": "master"
}
}
27 changes: 14 additions & 13 deletions src/Graphics/Canvas.purs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ import Effect.Exception.Unsafe (unsafeThrow)
import Data.ArrayBuffer.Types (Uint8ClampedArray)
import Data.Function.Uncurried (Fn3, runFn3)
import Data.Maybe (Maybe(..))
import Web.HTML.HTMLCanvasElement (HTMLCanvasElement)

-- | A canvas HTML element.
foreign import data CanvasElement :: Type
-- | A canvas HTML element. DEPRECATED: Please use HTMLCanvasElement
type CanvasElement = HTMLCanvasElement

-- | A 2D graphics context.
foreign import data Context2D :: Type
Expand All @@ -141,7 +142,7 @@ foreign import data CanvasPattern :: Type
-- | Opaque object describing a gradient.
foreign import data CanvasGradient :: Type

foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
foreign import canvasElementToImageSource :: HTMLCanvasElement -> CanvasImageSource

foreign import tryLoadImageImpl
:: String
Expand All @@ -159,45 +160,45 @@ tryLoadImage path k = tryLoadImageImpl path (k Nothing) (k <<< Just)
foreign import getCanvasElementByIdImpl
:: forall r
. Fn3 String
(CanvasElement -> r)
(HTMLCanvasElement -> r)
r
(Effect r)

-- | Get a canvas element by ID, or `Nothing` if the element does not exist.
getCanvasElementById :: String -> Effect (Maybe CanvasElement)
getCanvasElementById :: String -> Effect (Maybe HTMLCanvasElement)
getCanvasElementById elId = runFn3 getCanvasElementByIdImpl elId Just Nothing

-- | Get the 2D graphics context for a canvas element.
foreign import getContext2D :: CanvasElement -> Effect Context2D
foreign import getContext2D :: HTMLCanvasElement -> Effect Context2D

-- | Get the canvas width in pixels.
foreign import getCanvasWidth :: CanvasElement -> Effect Number
foreign import getCanvasWidth :: HTMLCanvasElement -> Effect Number

-- | Get the canvas height in pixels.
foreign import getCanvasHeight :: CanvasElement -> Effect Number
foreign import getCanvasHeight :: HTMLCanvasElement -> Effect Number

-- | Set the canvas width in pixels.
foreign import setCanvasWidth :: CanvasElement -> Number -> Effect Unit
foreign import setCanvasWidth :: HTMLCanvasElement -> Number -> Effect Unit

-- | Set the canvas height in pixels.
foreign import setCanvasHeight :: CanvasElement -> Number -> Effect Unit
foreign import setCanvasHeight :: HTMLCanvasElement -> Number -> Effect Unit

-- | Canvas dimensions (width and height) in pixels.
type Dimensions = { width :: Number, height :: Number }

-- | Get the canvas dimensions in pixels.
getCanvasDimensions :: CanvasElement -> Effect Dimensions
getCanvasDimensions :: HTMLCanvasElement -> Effect Dimensions
getCanvasDimensions ce = do
w <- getCanvasWidth ce
h <- getCanvasHeight ce
pure {width : w, height : h}

-- | Set the canvas dimensions in pixels.
setCanvasDimensions :: CanvasElement -> Dimensions -> Effect Unit
setCanvasDimensions :: HTMLCanvasElement -> Dimensions -> Effect Unit
setCanvasDimensions ce d = setCanvasHeight ce d.height *> setCanvasWidth ce d.width

-- | Create a data URL for the current canvas contents
foreign import canvasToDataURL :: CanvasElement -> Effect String
foreign import canvasToDataURL :: HTMLCanvasElement -> Effect String

-- | Set the current line width.
foreign import setLineWidth :: Context2D -> Number -> Effect Unit
Expand Down