-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
224 lines (176 loc) · 5.76 KB
/
main.ts
File metadata and controls
224 lines (176 loc) · 5.76 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import CanvasKitInit from 'canvaskit-wasm/bin/canvaskit';
import StretchLayoutInit from './stretch-layout';
import LayoutCtx, { measureWindowSize } from './layout';
import { createCanvas } from './dom';
import ElementNode from './layout/models/element_node';
// Window Size
let WINDOW_SIZE = {
offsetHeight: document.body.offsetHeight,
offsetWidth: document.body.offsetWidth,
}
enum RENDER_NODE_TYPE {
NODE,
TEXT,
}
const RENDER_QUEUE = [];
let surface = undefined
let layout = undefined;
let CanvasKit = undefined;
let fontMgr = undefined;
const updateCanvasSize = (id: string) => {
console.log("update canvas size")
createCanvas(id, { height: WINDOW_SIZE.offsetHeight, width: WINDOW_SIZE.offsetWidth })
}
const update = () => {
if (!LayoutCtx.CTX) return false;
surface = CanvasKit.MakeCanvasSurface('canvas');
const layoutCtx = LayoutCtx.CTX;
const root_node = layoutCtx.getRootNode()
RENDER_QUEUE.length = 0
root_node.addStyle({
height: WINDOW_SIZE.offsetHeight,
width: WINDOW_SIZE.offsetWidth
})
layoutCtx.computeLayout({ height: WINDOW_SIZE.offsetHeight, width: WINDOW_SIZE.offsetWidth })
const layout_draw_task = {
type: RENDER_NODE_TYPE.NODE,
rect: CanvasKit.XYWHRect(layout.x, layout.y, layout.width, layout.height),
paint: () => {
const paint = new CanvasKit.Paint()
paint.setColor(CanvasKit.Color4f(0.5, 0.5, 0, 1.0));
// paint.setStyle(CanvasKit.PaintStyle.Stroke);
paint.setAntiAlias(true);
return paint
},
}
RENDER_QUEUE.push(layout_draw_task)
for (let i = 0; i < layout.childCount; i++) {
let child_node = layout.child(i);
console.log(`child_node: ${i}`)
console.log(child_node)
const node_draw_task = {
type: RENDER_NODE_TYPE.NODE,
rect: CanvasKit.XYWHRect(child_node.x, child_node.y, child_node.width, child_node.height),
paint: () => {
const paint = new CanvasKit.Paint()
paint.setColor(CanvasKit.Color4f(0, 0.5, Math.random(), 1.0));
// paint.setStyle(CanvasKit.PaintStyle.Stroke);
paint.setAntiAlias(true);
return paint
},
}
RENDER_QUEUE.push(node_draw_task)
if (i === 0) {
const text_draw_task = {
type: RENDER_NODE_TYPE.TEXT,
fontSize: 48,
fontHeight: 64,
position: {
x: child_node.x,
y: child_node.y,
height: child_node.height,
width: child_node.width
},
paint: () => {
const paint = new CanvasKit.Paint()
paint.setColor(CanvasKit.Color4f(Math.random(), Math.random(), Math.random(), 1.0));
// paint.setStyle(CanvasKit.PaintStyle.Stroke);
paint.setAntiAlias(true);
return paint
},
}
RENDER_QUEUE.push(text_draw_task)
}
}
surface.drawOnce(draw);
}
const draw = (canvas) => {
console.log("draw")
canvas.clear(CanvasKit.WHITE);
RENDER_QUEUE.forEach((draw_task) => {
switch (draw_task.type) {
case RENDER_NODE_TYPE.NODE: {
canvas.drawRect(draw_task.rect, draw_task.paint())
break;
}
case RENDER_NODE_TYPE.TEXT: {
const paraStyle = new CanvasKit.ParagraphStyle({
textStyle: {
color: CanvasKit.RED,
fontFamilies: ['Roboto'],
fontSize: draw_task.fontSize,
fontHeight: draw_task.fontHeight,
},
textAlign: CanvasKit.TextAlign.Center,
});
const text = 'Render with CanvasKit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!';
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
builder.addText(text);
const paragraph = builder.build();
paragraph.layout(draw_task.position.width);
canvas.drawParagraph(paragraph, draw_task.position.x, draw_task.position.y);
surface.flush();
// canvas.drawText(text, draw_task.position.x, draw_task.position.y, draw_task.paint(), new CanvasKit.Font(null, 36))
break;
}
}
})
}
const init = async () => {
// init stretch-layout
await StretchLayoutInit();
// init Canvas Element
createCanvas("canvas", { width: WINDOW_SIZE.offsetWidth, height: WINDOW_SIZE.offsetHeight })
// init CvanvasKit
CanvasKit = await CanvasKitInit({
locateFile: (file) => 'node_modules/canvaskit-wasm/bin/' + file
});
// init CanvasKit Font
const response = await fetch('https://storage.googleapis.com/skia-cdn/misc/Roboto-Regular.ttf')
const robotoData = await response.arrayBuffer();
fontMgr = CanvasKit.FontMgr.FromData([robotoData]);
}
const run = async () => {
await init();
const layoutCtx = new LayoutCtx();
await layoutCtx.init();
test()
let root_node = new ElementNode();
root_node.setStyle({
justifyContent: "center",
flexDirection: "column",
alignItems: "center",
})
let child_node_1 = new ElementNode();
child_node_1.setStyle({ width: 500, height: 64, marginBottom: 20 })
let child_node_2 = new ElementNode();
child_node_2.setStyle({ width: 300, height: 300, marginBottom: 16 })
let child_node_3 = new ElementNode();
child_node_3.setStyle({ width: 300, height: 100 })
root_node.appendChild(child_node_1)
root_node.appendChild(child_node_2)
root_node.appendChild(child_node_3)
layoutCtx.setRootNode(root_node)
surface = CanvasKit.MakeCanvasSurface('canvas');
update()
window.addEventListener("resize", () => {
WINDOW_SIZE = measureWindowSize()
updateCanvasSize("canvas")
update()
})
}
WINDOW_SIZE = measureWindowSize()
run()
const test = async () => {
const layoutCtx = new LayoutCtx();
await layoutCtx.init()
const root = new ElementNode();
root.setStyle({
display: "flex",
flexDirection: "row-reverse",
})
console.log(root)
const child = new ElementNode();
root.appendChild(child)
console.log(root)
}