Skip to content

Commit c7f7e51

Browse files
committed
Updated Gradients to use the Color.Gradient type
1 parent 1bc4f90 commit c7f7e51

File tree

6 files changed

+398
-161
lines changed

6 files changed

+398
-161
lines changed

elm-package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
],
1212
"native-modules": true,
1313
"dependencies": {
14-
"elm-lang/animation-frame": "1.0.1 <= v < 2.0.0",
1514
"elm-lang/core": "5.0.0 <= v < 6.0.0",
16-
"elm-lang/html": "2.0.0 <= v < 3.0.0",
17-
"mbr/elm-mouse-events": "1.0.4 <= v < 2.0.0"
15+
"elm-lang/html": "2.0.0 <= v < 3.0.0"
1816
},
1917
"elm-version": "0.18.0 <= v < 0.19.0"
2018
}

examples/6-gradients.elm

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Main exposing (..)
22

3-
import Html exposing (Html, Attribute)
3+
import Canvas exposing (Canvas, DrawOp(..), Point, Size, Style(Gradient))
4+
import Color exposing (Color, linear, radial)
5+
import Html exposing (Attribute, Html)
46
import Html.Attributes exposing (style)
5-
import Canvas exposing (Size, Point, DrawOp(..), Canvas, Style(LinearGradient, RadialGradient), ColorStop(..))
6-
import Color exposing (Color)
77
import MouseEvents exposing (MouseEvent)
88

99

@@ -90,38 +90,46 @@ handleClickState ( canvas, clickState ) =
9090
_ ->
9191
canvas
9292

93+
9394
drawBackground : Canvas -> Canvas
9495
drawBackground =
95-
let
96-
colorStops = [ ColorStop 1 Color.red
97-
, ColorStop 0.9 Color.orange
98-
, ColorStop 0.7 Color.yellow
99-
, ColorStop 0.5 Color.green
100-
, ColorStop 0.3 Color.blue
101-
, ColorStop 0.1 Color.purple
102-
]
103-
gradient = RadialGradient (Point 400 400) 0 (Point 400 400) 400 colorStops
104-
in
96+
let
97+
colorStops =
98+
[ ( 1, Color.red )
99+
, ( 0.9, Color.orange )
100+
, ( 0.7, Color.yellow )
101+
, ( 0.5, Color.green )
102+
, ( 0.3, Color.blue )
103+
, ( 0.1, Color.purple )
104+
]
105+
106+
gradient =
107+
radial ( 400, 400 ) 0 ( 400, 400 ) 400 colorStops
108+
in
105109
[ BeginPath
106-
, FillStyle gradient
110+
, FillStyle (Gradient gradient)
107111
, FillRect (Point 0 0) (Size 800 800)
108112
, Fill
109113
]
110114
|> Canvas.batch
111115
|> Canvas.draw
112116

117+
113118
drawLine : Point -> Point -> Canvas -> Canvas
114119
drawLine pt0 pt1 =
115-
let
116-
colorStops = [ ColorStop 0 Color.white
117-
, ColorStop 1 Color.black
118-
]
119-
gradient = LinearGradient (Point pt0.x 0) (Point pt1.x 0) colorStops
120-
in
120+
let
121+
colorStops =
122+
[ ( 0, Color.white )
123+
, ( 1, Color.black )
124+
]
125+
126+
gradient =
127+
linear ( pt0.x, 0 ) ( pt1.x, 0 ) colorStops
128+
in
121129
[ BeginPath
122130
, LineWidth 30
123131
, LineCap "round"
124-
, StrokeStyle gradient
132+
, StrokeStyle (Gradient gradient)
125133
, MoveTo pt0
126134
, LineTo pt1
127135
, Stroke

examples/Canvas.elm

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
module Canvas
22
exposing
33
( Canvas
4+
, DrawImageParams(..)
5+
, DrawOp(..)
46
, Error
57
, Point
8+
, Repeat(..)
69
, Size
7-
, DrawOp(..)
8-
, DrawImageParams(..)
9-
, initialize
10-
, toHtml
11-
, draw
10+
, Style(..)
1211
, batch
13-
, loadImage
12+
, draw
1413
, getImageData
1514
, getSize
15+
, initialize
16+
, loadImage
1617
, setSize
1718
, toDataUrl
19+
, toHtml
1820
)
1921

2022
{-| The canvas html element is a very simple way to render 2D graphics. Check out these examples, and get an explanation of the canvas element [here](https://github.com/elm-community/canvas). Furthermore, If you havent heard of [Elm-Graphics](http://package.elm-lang.org/packages/evancz/elm-graphics/latest), I recommend checking that out first, because its probably what you need. Elm-Canvas is for when you need unusually direct and low level access to the canvas element.
2123
24+
2225
# Main Types
23-
@docs Canvas, Point, Size, DrawOp, DrawImageParams
26+
27+
@docs Canvas, Point, Size, DrawOp, DrawImageParams, Style, Repeat
28+
2429
2530
# Basics
31+
2632
@docs initialize, toHtml, draw, batch
2733
34+
2835
# Loading Images
36+
2937
@docs loadImage, Error
3038
39+
3140
# Image Data
41+
3242
@docs getImageData, toDataUrl
3343
44+
3445
# Sizing
46+
3547
@docs getSize, setSize
3648
3749
-}
3850

39-
import Html exposing (Html, Attribute)
40-
import Task exposing (Task)
41-
import Color exposing (Color)
51+
import Color exposing (Color, Gradient)
52+
import Html exposing (Attribute, Html)
4253
import Native.Canvas
54+
import Task exposing (Task)
4355

4456

4557
{-| A `Canvas` contains image data, and can be rendered as html with `toHtml`. It is the primary type of this package.
@@ -97,10 +109,10 @@ type DrawOp
97109
| Transform Float Float Float Float Float Float
98110
| Translate Point
99111
| StrokeRect Point Size
100-
| StrokeStyle Color
112+
| StrokeStyle Style
101113
| TextAlign String
102114
| TextBaseline String
103-
| FillStyle Color
115+
| FillStyle Style
104116
| BeginPath
105117
| BezierCurveTo Point Point Point
106118
| QuadraticCurveTo Point Point
@@ -112,6 +124,23 @@ type DrawOp
112124
| Batch (List DrawOp)
113125

114126

127+
{-| `Style` specifies the style to apply as a `FillStyle` or a `StrokeStyle`.
128+
-}
129+
type Style
130+
= Color Color
131+
| Pattern Canvas Repeat
132+
| Gradient Gradient
133+
134+
135+
{-| Specifies the axis/axes along which to replicate a pattern. For use with the `Pattern` `Style`.
136+
-}
137+
type Repeat
138+
= Repeat
139+
| RepeatX
140+
| RepeatY
141+
| NoRepeat
142+
143+
115144
{-| The `DrawOp` `DrawImage` takes a `Canvas` and a `DrawImageParam`. We made three different `DrawImageParam`, because there are three different sets of parameters you can give the native javascript `ctx.drawImage()`. [See here for more info](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage.)
116145
-}
117146
type DrawImageParams
@@ -125,6 +154,7 @@ type DrawImageParams
125154
squareCanvas : Int -> Canvas
126155
squareCanvas length =
127156
initialize (Size length length)
157+
128158
-}
129159
initialize : Size -> Canvas
130160
initialize =
@@ -136,6 +166,7 @@ initialize =
136166
pixelatedRender : Canvas -> Html Msg
137167
pixelatedRender canvas =
138168
canvas |> toHtml [ class "pixelated" ]
169+
139170
-}
140171
toHtml : List (Attribute msg) -> Canvas -> Html msg
141172
toHtml =
@@ -153,13 +184,14 @@ toHtml =
153184
, LineTo p1
154185
, Stroke
155186
]
187+
156188
-}
157189
draw : DrawOp -> Canvas -> Canvas
158190
draw =
159191
Native.Canvas.draw
160192

161193

162-
{-| You dont want to apply `DrawOp` one at a time. Bundle many `DrawOp` together in one batch, using `batch`.
194+
{-| You dont want to apply `DrawOp` one at a time, its inefficient. Bundle many `DrawOp` together in one batch, using `batch`.
163195
164196
line : Point -> Point -> DrawOp
165197
line p0 p1 =
@@ -170,6 +202,7 @@ draw =
170202
, LineTo p1
171203
, Stroke
172204
]
205+
173206
-}
174207
batch : List DrawOp -> DrawOp
175208
batch =
@@ -193,6 +226,7 @@ batch =
193226
Nothing ->
194227
-- ..
195228
-- ..
229+
196230
-}
197231
loadImage : String -> Task Error Canvas
198232
loadImage =
@@ -215,6 +249,7 @@ loadImage =
215249
[ 0, 0, 0, 255, 255, 0, 0, 255
216250
, 0, 0, 0, 255, 255, 255, 255, 255
217251
]
252+
218253
-}
219254
getImageData : Point -> Size -> Canvas -> List Int
220255
getImageData =

0 commit comments

Comments
 (0)