|
| 1 | +module Canvas where |
| 2 | + |
| 3 | +import Graphics.Element exposing (Element) |
| 4 | +import Basics exposing (pi) |
| 5 | +import Color exposing (..) |
| 6 | +import Native.Canvas |
| 7 | +import Task exposing (Task) |
| 8 | + |
| 9 | +-- Line Styles |
| 10 | + |
| 11 | +type LineCap = ButtCap | RoundCap | SquareCap |
| 12 | +type LineJoin = RoundJoin | BevelJoin | MiterJoin |
| 13 | + |
| 14 | +-- Composite Operations |
| 15 | + |
| 16 | +type CompositeOperation |
| 17 | + = SourceOver |
| 18 | + | SourceIn |
| 19 | + | SourceOut |
| 20 | + | SourceAtop |
| 21 | + | DestinationOver |
| 22 | + | DestinationIn |
| 23 | + | DestinationOut |
| 24 | + | DestinationAtop |
| 25 | + | Lighter |
| 26 | + | Copy |
| 27 | + | Xor |
| 28 | + | Multiply |
| 29 | + | Screen |
| 30 | + | Overlay |
| 31 | + | Darken |
| 32 | + | Lighten |
| 33 | + | ColorDodge |
| 34 | + | ColorBurn |
| 35 | + | HardLight |
| 36 | + | SoftLight |
| 37 | + | Difference |
| 38 | + | Exclusion |
| 39 | + | Hue |
| 40 | + | Saturation |
| 41 | + | Color |
| 42 | + | Luminosity |
| 43 | + |
| 44 | +-- Paths |
| 45 | + |
| 46 | +type alias HasPoint a = { a | x: Float, y: Float } |
| 47 | +type alias HasCircle a = HasPoint { a | r: Float } |
| 48 | +type alias HasRect a = HasPoint { a | w: Float, h: Float } |
| 49 | +type alias Point = HasPoint {} |
| 50 | +type alias Circle = HasCircle {} |
| 51 | +type alias Rectangle = HasRect {} |
| 52 | + |
| 53 | +type PathMethod |
| 54 | + = MoveTo Point |
| 55 | + | LineTo Point |
| 56 | + | Rect Rectangle |
| 57 | + | Arc { x: Float, y: Float, r: Float, startAngle: Float, endAngle: Float, ccw: Bool } |
| 58 | + | ArcTo { x1: Float, y1: Float, x2: Float, y2: Float, r: Float } |
| 59 | + | ClosePath |
| 60 | + |
| 61 | +type alias Path = List PathMethod |
| 62 | + |
| 63 | +-- Images |
| 64 | + |
| 65 | +type Image = Image |
| 66 | + |
| 67 | +-- Commands |
| 68 | + |
| 69 | +type Command |
| 70 | + -- Draw Commands |
| 71 | + = Clear Rectangle |
| 72 | + | FillPath Path |
| 73 | + | StrokePath Path |
| 74 | + | FillText String Float Float |
| 75 | + | StrokeText String Float Float |
| 76 | + | DrawImage Float Float Image |
| 77 | + |
| 78 | + -- State Commands |
| 79 | + | FillColor Color |
| 80 | + | StrokeColor Color |
| 81 | + | FillGrad Gradient |
| 82 | + | StrokeGrad Gradient |
| 83 | + |
| 84 | + | LineWidth Float |
| 85 | + | LineCapStyle LineCap |
| 86 | + | LineJoinStyle LineJoin |
| 87 | + | LineMiterLimit Float |
| 88 | + |
| 89 | + | ShadowBlur Float |
| 90 | + | ShadowColor Color |
| 91 | + | ShadowOffset Float Float |
| 92 | + |
| 93 | + | Translate Float Float |
| 94 | + | Rotate Float |
| 95 | + | Scale Float Float |
| 96 | + |
| 97 | + | Font String |
| 98 | + | Alpha Float |
| 99 | + | Composite CompositeOperation |
| 100 | + |
| 101 | + -- Wraps Commands in Save/Restore |
| 102 | + | Context (List Command) |
| 103 | + |
| 104 | +-- Draw Commands |
| 105 | + |
| 106 | +clearRect rect = Clear rect |
| 107 | +fillRect x y w h = FillPath [Rect (rect x y w h)] |
| 108 | +strokeRect x y w h = StrokePath [Rect (rect x y w h)] |
| 109 | + |
| 110 | +fillPath path = FillPath path |
| 111 | +strokePath path = StrokePath path |
| 112 | + |
| 113 | +fillCircle circle = FillPath [Arc circle] |
| 114 | +strokeCircle circle = StrokePath [Arc circle] |
| 115 | + |
| 116 | +fillText text x y = FillText text x y |
| 117 | +strokeText text x y = StrokeText text x y |
| 118 | + |
| 119 | +-- Style Commands |
| 120 | + |
| 121 | +fillColor color = FillColor color |
| 122 | +strokeColor color = StrokeColor color |
| 123 | +fillGrad grad = FillGrad grad |
| 124 | +strokeGrad grad = StrokeGrad grad |
| 125 | + |
| 126 | +lineWidth width = LineWidth width |
| 127 | +lineCap cap = LineCapStyle cap |
| 128 | +lineJoin join = LineJoinStyle join |
| 129 | +lineMiterLimit length = LineMiterLimit length |
| 130 | + |
| 131 | +shadowBlur blurRadius = ShadowBlur blurRadius |
| 132 | +shadowColor color = ShadowColor color |
| 133 | +shadowOffset offsetX offsetY = ShadowOffset offsetX offsetY |
| 134 | + |
| 135 | +translate x y = Translate x y |
| 136 | +rotate angle = Rotate angle |
| 137 | +scale scaleX scaleY = Scale scaleX scaleY |
| 138 | + |
| 139 | +font fnt = Font fnt |
| 140 | +alpha a = Alpha a |
| 141 | +composite compositeOp = Composite compositeOp |
| 142 | + |
| 143 | +context commands = Context commands |
| 144 | + |
| 145 | +-- Path Methods |
| 146 | + |
| 147 | +moveTo x y = MoveTo { x = x, y = y } |
| 148 | +lineTo x y = LineTo { x = x, y = y } |
| 149 | + |
| 150 | +rect x y w h = { x = x, y = y, w = w, h = h } |
| 151 | + |
| 152 | +circle x y r = arc x y r 0.0 (2.0 * pi) |
| 153 | + |
| 154 | +arcWithDir x y r startAngle endAngle ccw = |
| 155 | + { x = x, y = y, r = r, startAngle = startAngle, endAngle = endAngle, ccw = ccw } |
| 156 | + |
| 157 | +arc x y r startAngle endAngle = arcWithDir x y r startAngle endAngle False |
| 158 | + |
| 159 | +arcTo x1 y1 x2 y2 r = ArcTo { x1 = x1, y1 = y1, x2 = x2, y2 = y2, r = r } |
| 160 | + |
| 161 | +-- Images |
| 162 | + |
| 163 | +drawImage : Float -> Float -> Image -> Command |
| 164 | +drawImage x y img = DrawImage x y img |
| 165 | + |
| 166 | +loadImage : String -> Task String Image |
| 167 | +loadImage = Native.Canvas.loadImage |
| 168 | + |
| 169 | +-- Canvas |
| 170 | + |
| 171 | +canvas : (Int, Int) -> List Command -> Element |
| 172 | +canvas = Native.Canvas.canvas |
0 commit comments