Skip to content

Commit

Permalink
Handle opacity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Yorgey committed Apr 22, 2012
1 parent f8de823 commit aba6150
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Diagrams/Backend/Canvas.hs
Expand Up @@ -61,6 +61,7 @@ canvasStyle s = foldr (>>) (return ())
, handle lWidth , handle lWidth
, handle lJoin , handle lJoin
, handle lCap , handle lCap
, handle opacity_
] ]
where handle :: (AttributeClass a) => (a -> C.Render ()) -> Maybe (C.Render ()) where handle :: (AttributeClass a) => (a -> C.Render ()) -> Maybe (C.Render ())
handle f = f `fmap` getAttr s handle f = f `fmap` getAttr s
Expand All @@ -69,6 +70,7 @@ canvasStyle s = foldr (>>) (return ())
lWidth = C.lineWidth . getLineWidth lWidth = C.lineWidth . getLineWidth
lCap = C.lineCap . getLineCap lCap = C.lineCap . getLineCap
lJoin = C.lineJoin . getLineJoin lJoin = C.lineJoin . getLineJoin
opacity_ = C.globalAlpha . getOpacity


canvasTransf :: Transformation R2 -> C.Render () canvasTransf :: Transformation R2 -> C.Render ()
canvasTransf t = C.transform a1 a2 b1 b2 c1 c2 canvasTransf t = C.transform a1 a2 b1 b2 c1 c2
Expand Down
10 changes: 9 additions & 1 deletion src/Graphics/Rendering/Canvas.hs
Expand Up @@ -23,6 +23,7 @@ module Graphics.Rendering.Canvas
, lineWidth , lineWidth
, lineCap , lineCap
, lineJoin , lineJoin
, globalAlpha
, withStyle , withStyle
) where ) where


Expand All @@ -43,11 +44,12 @@ data DrawState = DS
, dsCap :: LineCap , dsCap :: LineCap
, dsJoin :: LineJoin , dsJoin :: LineJoin
, dsWidth :: Float , dsWidth :: Float
, dsAlpha :: Float
, dsTransform :: (Float,Float,Float,Float,Float,Float) , dsTransform :: (Float,Float,Float,Float,Float,Float)
} deriving (Eq) } deriving (Eq)


emptyDS :: DrawState emptyDS :: DrawState
emptyDS = DS 0 (0,0,0,1) 0 LineCapButt LineJoinMiter 0 (1,0,0,1,0,0) emptyDS = DS 0 (0,0,0,1) 0 LineCapButt LineJoinMiter 0 1 (1,0,0,1,0,0)


data RenderState = RS data RenderState = RS
{ drawState :: DrawState { drawState :: DrawState
Expand Down Expand Up @@ -202,6 +204,12 @@ fromLineJoin LineJoinRound = show "round"
fromLineJoin LineJoinBevel = show "bevel" fromLineJoin LineJoinBevel = show "bevel"
fromLineJoin _ = show "miter" fromLineJoin _ = show "miter"


globalAlpha :: Double -> Render ()
globalAlpha a = setDSWhen
(\ds -> ds { dsAlpha = a' })
(canvas $ C.globalAlpha a')
where a' = realToFrac a

-- TODO: update the transform's state for translate, scale, and rotate -- TODO: update the transform's state for translate, scale, and rotate
translate :: Double -> Double -> Render () translate :: Double -> Double -> Render ()
translate x y = canvas $ C.translate (realToFrac x,realToFrac y) translate x y = canvas $ C.translate (realToFrac x,realToFrac y)
Expand Down

0 comments on commit aba6150

Please sign in to comment.