Skip to content

Instantly share code, notes, and snippets.

@quephird
Last active September 17, 2015 23:16
Show Gist options
  • Save quephird/060e9ebd4ff8ff8bcb1a to your computer and use it in GitHub Desktop.
Save quephird/060e9ebd4ff8ff8bcb1a to your computer and use it in GitHub Desktop.
Partial solution to third problem in first set of exercises in Chapter 9 of PureScript by Example
module Plot where
import Prelude (($), bind, return)
import Control.Monad.Eff (Eff(..))
import Data.List (List(..), toList)
import Data.Maybe (Maybe(..))
import Graphics.Canvas ( Canvas(..)
, Context2D(..)
, closePath
, getCanvasElementById
, getContext2D
, lineTo
, moveTo
, rect
, setFillStyle
, setStrokeStyle
, strokePath)
type Point =
{ x :: Number
, y :: Number
}
renderPath :: forall eff. Context2D ->
Array Point ->
Eff (canvas :: Canvas | eff) Context2D
renderPath ctx pts = renderPath' $ toList pts where
renderPath' pts' =
case pts' of
(Cons { x: x1, y: y1 }
pts''@(Cons { x: x2, y: y2 } _)) -> do
strokePath ctx $ do
moveTo ctx x1 y1
lineTo ctx x2 y2
closePath ctx
renderPath' pts''
_ -> return ctx
main = do
Just canvas <- getCanvasElementById "canvas"
ctx <- getContext2D canvas
setStrokeStyle "#0000FF" ctx
renderPath ctx [{ x: 100.0, y: 200.0 }
,{ x: 300.0, y: 300.0 }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment