-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
CanvasExample.re
175 lines (155 loc) · 5.05 KB
/
CanvasExample.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
open Revery;
open Revery.Font;
open Revery.Draw;
open Revery.UI;
let containerStyle =
Style.[
position(`Absolute),
top(0),
bottom(0),
left(0),
right(0),
alignItems(`Center),
justifyContent(`Center),
flexDirection(`Column),
];
let outerBox =
Style.[width(450), height(450), backgroundColor(Colors.black)];
module Sample = {
let make = () => {
<View style=containerStyle>
<Canvas
style=outerBox
render={(canvasContext, _dimensions) => {
let transform = Skia.Matrix.make();
Skia.Matrix.setIdentity(transform);
CanvasContext.concat(transform, canvasContext);
let paint = Skia.Paint.make();
Skia.Paint.setColor(
paint,
Skia.Color.makeArgb(0xFFl, 0xFFl, 0x00l, 0x00l),
);
let rect = Skia.Rect.makeLtrb(1.0, 1.0, 101., 201.);
CanvasContext.drawRect(~rect, ~paint, canvasContext);
let stroke = Skia.Paint.make();
Skia.Paint.setColor(
stroke,
Skia.Color.makeArgb(0xFFl, 0xFFl, 0xFFl, 0x00l),
);
Skia.Paint.setAntiAlias(stroke, true);
Skia.Paint.setStyle(stroke, Stroke);
Skia.Paint.setStrokeWidth(stroke, 2.);
let innerPath = Skia.Path.make();
Skia.Path.lineTo(innerPath, 5., 5.);
Skia.Path.lineTo(innerPath, 15., -5.);
Skia.Path.lineTo(innerPath, 20., 0.);
let translate = Skia.Matrix.makeScale(20., 20., 20., 20.);
let pathEffect =
Skia.PathEffect.create2dPath(~matrix=translate, innerPath);
Skia.Paint.setPathEffect(stroke, pathEffect);
let path = Skia.Path.make();
Skia.Path.addCircle(path, 125., 125., ~radius=100., ());
CanvasContext.drawPath(~path, ~paint=stroke, canvasContext);
let maybeSkia =
Revery_Font.Family.fromFile("Roboto-Regular.ttf")
|> Revery_Font.Family.toSkia(Revery_Font.Weight.Normal);
switch (Revery_Font.load(maybeSkia)) {
| Error(_) => ()
| Ok(font) =>
let textPaint = Skia.Paint.make();
Skia.Paint.setColor(textPaint, Color.toSkia(Colors.white));
Skia.Paint.setLcdRenderText(textPaint, true);
Skia.Paint.setAntiAlias(textPaint, true);
Skia.Paint.setTextSize(textPaint, 20.);
let shapedText =
"Hello, World"
|> FontCache.shape(font)
|> ShapeResult.getGlyphStrings;
Skia.Paint.setTextEncoding(textPaint, GlyphId);
shapedText
|> List.iter(((typeface, string)) => {
Skia.Paint.setTypeface(textPaint, typeface);
CanvasContext.drawText(
~paint=textPaint,
~x=10.0,
~y=20.0,
~text=string,
canvasContext,
);
});
};
let fill = Skia.Paint.make();
Skia.Paint.setColor(
fill,
Skia.Color.makeArgb(0xFFl, 0xFFl, 0x00l, 0x00l),
);
let rect = Skia.Rect.makeLtrb(200., 150., 250., 300.);
CanvasContext.drawOval(~rect, ~paint=fill, canvasContext);
let red = Skia.Color.makeArgb(0xFFl, 0xFFl, 0x00l, 0x00l);
let blue = Skia.Color.makeArgb(0xFFl, 0x00l, 0x00l, 0xFFl);
// Test out some layers...
let layer =
CanvasContext.createLayer(
~width=128l,
~height=128l,
canvasContext,
)
|> Option.get;
// Draw a circle onto our new layer...
let paint = Skia.Paint.make();
let linearGradient =
Skia.Shader.makeLinearGradient2(
~startPoint=Skia.Point.make(16., 16.0),
~stopPoint=Skia.Point.make(48.0, 48.0),
~startColor=red,
~stopColor=blue,
~tileMode=`clamp,
);
Skia.Paint.setShader(paint, linearGradient);
Skia.Paint.setColor(
paint,
Skia.Color.makeArgb(0xFFl, 0x00l, 0xFFl, 0x00l),
);
CanvasContext.drawCircle(
~x=32.,
~y=32.,
~radius=16.,
~paint,
layer,
);
// And then draw the layer a bunch of places!
let paint = Skia.Paint.make();
CanvasContext.drawLayer(
~paint,
~layer,
~x=300.,
~y=300.,
canvasContext,
);
CanvasContext.drawLayer(
~paint,
~layer,
~x=264.,
~y=264.,
canvasContext,
);
CanvasContext.drawLayer(
~paint,
~layer,
~x=264.,
~y=300.,
canvasContext,
);
CanvasContext.drawLayer(
~paint,
~layer,
~x=300.,
~y=264.,
canvasContext,
);
}}
/>
</View>;
};
};
let render = () => <Sample />;