Permalink
Browse files

Handle opacity.

  • Loading branch information...
1 parent f8de823 commit aba6150335a50dd7f28fd88352cd15516f4aab64 Brent Yorgey committed Apr 22, 2012
Showing with 11 additions and 1 deletion.
  1. +2 −0 src/Diagrams/Backend/Canvas.hs
  2. +9 −1 src/Graphics/Rendering/Canvas.hs
@@ -61,6 +61,7 @@ canvasStyle s = foldr (>>) (return ())
, handle lWidth
, handle lJoin
, handle lCap
+ , handle opacity_
]
where handle :: (AttributeClass a) => (a -> C.Render ()) -> Maybe (C.Render ())
handle f = f `fmap` getAttr s
@@ -69,6 +70,7 @@ canvasStyle s = foldr (>>) (return ())
lWidth = C.lineWidth . getLineWidth
lCap = C.lineCap . getLineCap
lJoin = C.lineJoin . getLineJoin
+ opacity_ = C.globalAlpha . getOpacity
canvasTransf :: Transformation R2 -> C.Render ()
canvasTransf t = C.transform a1 a2 b1 b2 c1 c2
@@ -23,6 +23,7 @@ module Graphics.Rendering.Canvas
, lineWidth
, lineCap
, lineJoin
+ , globalAlpha
, withStyle
) where
@@ -43,11 +44,12 @@ data DrawState = DS
, dsCap :: LineCap
, dsJoin :: LineJoin
, dsWidth :: Float
+ , dsAlpha :: Float
, dsTransform :: (Float,Float,Float,Float,Float,Float)
} deriving (Eq)
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
{ drawState :: DrawState
@@ -202,6 +204,12 @@ fromLineJoin LineJoinRound = show "round"
fromLineJoin LineJoinBevel = show "bevel"
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
translate :: Double -> Double -> Render ()
translate x y = canvas $ C.translate (realToFrac x,realToFrac y)

0 comments on commit aba6150

Please sign in to comment.