Skip to content

Commit eb91189

Browse files
committed
Batch type changed, draw function added
1 parent 934f10c commit eb91189

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/Canvas.elm

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Canvas
77
, DrawImageParams(..)
88
, initialize
99
, toHtml
10+
, draw
1011
, batch
1112
, loadImage
1213
, getImageData
@@ -21,7 +22,7 @@ module Canvas
2122
@docs Canvas, Size, DrawOp, DrawImageParams
2223
2324
# Basics
24-
@docs initialize, toHtml, batch
25+
@docs initialize, toHtml, draw, batch
2526
2627
# Loading Images
2728
@docs loadImage, Error
@@ -103,6 +104,7 @@ type DrawOp
103104
| Clip
104105
| ClosePath
105106
| DrawImage Canvas DrawImageParams
107+
| Batch (List DrawOp)
106108

107109

108110
{-| 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.)
@@ -134,11 +136,26 @@ toHtml : List (Attribute msg) -> Canvas -> Html msg
134136
toHtml =
135137
Native.Canvas.toHtml
136138

137-
138-
{-| This is our primary way of drawing onto canvases. Give `batch` a list of `DrawOp` and you can apply those `DrawOp` to a canvas.
139+
{-| This is our primary way of drawing onto canvases. Give `draw` a `drawOp` and apply it to a canvas.
139140
140141
drawLine : Point -> Point -> Canvas -> Canvas
141142
drawLine p0 p1 =
143+
(Canvas.batch >> Canvas.draw)
144+
[ BeginPath
145+
, LineWidth 2
146+
, MoveTo p0
147+
, LineTo p1
148+
, Stroke
149+
]
150+
-}
151+
draw : DrawOp -> Canvas -> Canvas
152+
draw =
153+
Native.Canvas.draw
154+
155+
{-| You dont want to apply `DrawOp` one at a time. Bundle many `DrawOp` together in one batch, using `batch`.
156+
157+
line : Point -> Point -> DrawOp
158+
line p0 p1 =
142159
Canvas.batch
143160
[ BeginPath
144161
, LineWidth 2
@@ -147,9 +164,9 @@ toHtml =
147164
, Stroke
148165
]
149166
-}
150-
batch : List DrawOp -> Canvas -> Canvas
167+
batch : List DrawOp -> DrawOp
151168
batch =
152-
Native.Canvas.batch
169+
Batch
153170

154171

155172
{-| Load up an image as a `Canvas` from a url.

src/Native/Canvas.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,12 @@ var _elm_canvas$elm_canvas$Native_Canvas = function () { // eslint-disable-line
5757

5858
}
5959

60-
61-
function batch(drawOps, model) {
60+
function draw(drawOp, model) {
6261
model = cloneModel(model);
6362

6463
var ctx = model.canvas().getContext("2d");
6564

66-
while (drawOps.ctor !== "[]") {
67-
handleDrawOp(ctx, drawOps._0);
68-
69-
drawOps = drawOps._1;
70-
}
65+
handleDrawOp(ctx, drawOp);
7166

7267
return model;
7368
}
@@ -77,6 +72,17 @@ var _elm_canvas$elm_canvas$Native_Canvas = function () { // eslint-disable-line
7772
var point, point1, size, color;
7873

7974
switch (drawOp.ctor) {
75+
case "Batch" :
76+
var drawOps = drawOp._0;
77+
78+
while (drawOps.ctor !== "[]") {
79+
handleDrawOp(ctx, drawOps._0);
80+
81+
drawOps = drawOps._1;
82+
}
83+
84+
break;
85+
8086
case "Font" :
8187

8288
ctx.font = drawOp._0;
@@ -525,7 +531,8 @@ var _elm_canvas$elm_canvas$Native_Canvas = function () { // eslint-disable-line
525531
toHtml: F2(toHtml), // eslint-disable-line no-undef
526532
getImageData: F3(getImageData), // eslint-disable-line no-undef
527533
clone: cloneModel,
528-
batch: F2(batch), // eslint-disable-line no-undef
534+
draw: F2(draw),
535+
// batch: F2(batch), // eslint-disable-line no-undef
529536
toDataURL: F3(toDataURL) // eslint-disable-line no-undef
530537
};
531538
}();

0 commit comments

Comments
 (0)