-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.ts
More file actions
474 lines (460 loc) · 14.8 KB
/
demo.ts
File metadata and controls
474 lines (460 loc) · 14.8 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* draw-call 网页演示
* 展示库的各项功能:逐句布局、样式、文本排版、图片渲染等
*/
import { Box, createCanvas, Image, linearGradient, Svg, svg, Text } from "@/index";
// 创建一个 canvas 并绘制图片内容,返回用于 Image 组件的 canvas
function createDemoImage(): HTMLCanvasElement {
const imgCanvas = document.createElement("canvas");
imgCanvas.width = 120;
imgCanvas.height = 60;
const ctx = imgCanvas.getContext("2d");
if (!ctx) return imgCanvas;
// 绘制渐变背景
const gradient = ctx.createLinearGradient(0, 0, 120, 60);
gradient.addColorStop(0, "#667eea");
gradient.addColorStop(1, "#764ba2");
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.roundRect(0, 0, 120, 60, 8);
ctx.fill();
// 绘制文字
ctx.fillStyle = "#ffffff";
ctx.font = "bold 14px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Image Demo", 60, 30);
return imgCanvas;
}
// 获取 canvas 元素并设置尺寸
const canvasEl = document.getElementById("canvas") as HTMLCanvasElement;
if (!canvasEl) throw new Error("Canvas element not found");
// 根据设备像素比设置实际大小
const dpr = window.devicePixelRatio || 1;
const width = 720;
const height = 600;
// 创建 Canvas 实例
const canvas = createCanvas({
width,
height,
pixelRatio: dpr,
canvas: canvasEl,
fitContent: true,
});
// 渲染演示内容
canvas.render(
Box({
width: "fill",
height: "fill",
background: "#ffffff",
direction: "row",
children: [
// 左侧:功能展示区
Box({
width: "fill",
flex: 1,
background: linearGradient(135, "#667eea", "#764ba2"),
padding: 20,
direction: "column",
justify: "space-between",
align: "start",
children: [
// 标题部分
Box({
direction: "column",
gap: 8,
children: [
Text({
content: "draw-call",
font: { size: 32, weight: "bold", family: "sans-serif" },
color: "#ffffff",
}),
Text({
content: "声明式 Canvas 绘图",
font: { size: 14, family: "sans-serif" },
color: "rgba(255,255,255,0.8)",
}),
],
}),
// 功能列表
Box({
direction: "column",
gap: 12,
children: [
// 功能项 1
Box({
direction: "row",
gap: 8,
align: "center",
children: [
Box({
width: 8,
height: 8,
background: "#ffd700",
border: { radius: 4 },
}),
Text({
content: "Flexbox 布局引擎",
font: { size: 12, family: "sans-serif" },
color: "#ffffff",
}),
],
}),
// 功能项 2
Box({
direction: "row",
gap: 8,
align: "center",
children: [
Box({
width: 8,
height: 8,
background: "#ffd700",
border: { radius: 4 },
}),
Text({
content: "丰富的样式支持",
font: { size: 12, family: "sans-serif" },
color: "#ffffff",
}),
],
}),
// 功能项 3
Box({
direction: "row",
gap: 8,
align: "center",
children: [
Box({
width: 8,
height: 8,
background: "#ffd700",
border: { radius: 4 },
}),
Text({
content: "自动换行 & 排版",
font: { size: 12, family: "sans-serif" },
color: "#ffffff",
}),
],
}),
],
}),
],
}),
// 右侧:卡片展示
Box({
width: "fill",
flex: 2,
padding: 20,
direction: "column",
gap: 12,
background: "#f5f7fa",
justify: "center",
align: "center",
children: [
// 卡片 1:样式展示
Box({
width: "fill",
background: "#ffffff",
border: { radius: 8, width: 1, color: "#e0e0e0" },
padding: 12,
shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.08)" },
direction: "column",
gap: 8,
children: [
Text({
content: "渐变背景示例",
font: { size: 12, weight: "bold", family: "sans-serif" },
color: "#333",
}),
Box({
height: 30,
background: linearGradient(90, "#667eea", "#764ba2", "#f093fb"),
border: { radius: 4 },
}),
],
}),
// 卡片 2:阴影展示
Box({
width: "fill",
background: "#ffffff",
border: { radius: 8, width: 1, color: "#e0e0e0" },
padding: 12,
shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.08)" },
direction: "column",
gap: 8,
children: [
Text({
content: "多彩标签",
font: { size: 12, weight: "bold", family: "sans-serif" },
color: "#333",
}),
Box({
direction: "row",
gap: 6,
wrap: true,
children: [
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#e8f4ff",
border: { radius: 3 },
children: [
Text({
content: "Canvas",
font: { size: 11, family: "sans-serif" },
color: "#1890ff",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#f6ffed",
border: { radius: 3 },
children: [
Text({
content: "TypeScript",
font: { size: 11, family: "sans-serif" },
color: "#52c41a",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#fff7e6",
border: { radius: 3 },
children: [
Text({
content: "声明式",
font: { size: 11, family: "sans-serif" },
color: "#fa8c16",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#ffe8e8",
border: { radius: 3 },
children: [
Text({
content: "Flexbox",
font: { size: 11, family: "sans-serif" },
color: "#f5222d",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#f0f0ff",
border: { radius: 3 },
children: [
Text({
content: "布局",
font: { size: 11, family: "sans-serif" },
color: "#2f54eb",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#e8f4f8",
border: { radius: 3 },
children: [
Text({
content: "渲染",
font: { size: 11, family: "sans-serif" },
color: "#1765ad",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#f9f0ff",
border: { radius: 3 },
children: [
Text({
content: "组件化",
font: { size: 11, family: "sans-serif" },
color: "#722ed1",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#fff0f5",
border: { radius: 3 },
children: [
Text({
content: "跨平台",
font: { size: 11, family: "sans-serif" },
color: "#eb2f96",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#f6ffec",
border: { radius: 3 },
children: [
Text({
content: "高性能",
font: { size: 11, family: "sans-serif" },
color: "#52c41a",
}),
],
}),
Box({
padding: { left: 6, right: 6, top: 3, bottom: 3 },
background: "#fffbe6",
border: { radius: 3 },
children: [
Text({
content: "易用性",
font: { size: 11, family: "sans-serif" },
color: "#d48806",
}),
],
}),
],
}),
],
}),
// 卡片 3:文本排版
Box({
width: "fill",
background: "#ffffff",
border: { radius: 8, width: 1, color: "#e0e0e0" },
padding: 12,
shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.08)" },
direction: "column",
gap: 8,
children: [
Text({
content: "自动换行示例",
font: { size: 12, weight: "bold", family: "sans-serif" },
color: "#333",
}),
Text({
content: "draw-call 支持自动换行和文本排版,可以创建专业的 Canvas 内容。",
font: { size: 10, family: "sans-serif" },
color: "#666",
wrap: true,
lineHeight: 1.4,
}),
],
}),
// 卡片 4:图片展示
Box({
width: "fill",
background: "#ffffff",
border: { radius: 8, width: 1, color: "#e0e0e0" },
padding: 12,
shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.08)" },
direction: "column",
gap: 8,
children: [
Text({
content: "Canvas 图片",
font: { size: 12, weight: "bold", family: "sans-serif" },
color: "#333",
}),
Image({
src: createDemoImage(),
width: 120,
height: 60,
border: { radius: 8 },
}),
],
}),
// 卡片 5:SVG 图形展示
Box({
width: "fill",
background: "#ffffff",
border: { radius: 8, width: 1, color: "#e0e0e0" },
padding: 12,
shadow: { offsetY: 2, blur: 8, color: "rgba(0,0,0,0.08)" },
direction: "column",
gap: 8,
children: [
Text({
content: "SVG 图形",
font: { size: 12, weight: "bold", family: "sans-serif" },
color: "#333",
}),
Svg({
width: "fill",
height: 100,
viewBox: { width: 200, height: 100 },
children: [
// 绘制背景网格
svg.rect({
x: 0,
y: 0,
width: 200,
height: 100,
fill: "rgba(245, 247, 250, 0.5)",
}),
// 绘制矩形
svg.rect({
x: 10,
y: 20,
width: 40,
height: 30,
rx: 5,
fill: "#667eea",
}),
// 绘制圆形
svg.circle({
cx: 80,
cy: 35,
r: 15,
fill: "#764ba2",
}),
// 绘制椭圆
svg.ellipse({
cx: 120,
cy: 35,
rx: 20,
ry: 12,
fill: "#f093fb",
}),
// 绘制线条
svg.line({
x1: 10,
y1: 70,
x2: 60,
y2: 70,
stroke: { color: "#3498db", width: 2 },
}),
// 绘制多边形
svg.polygon({
points: [
[80, 70],
[90, 55],
[100, 70],
],
fill: "#e74c3c",
}),
// 绘制路径
svg.path({
d: "M120 70 Q130 50, 140 70 T160 70",
fill: "none",
stroke: { color: "#2ecc71", width: 2 },
}),
// 绘制文本
svg.text({
x: 160,
y: 35,
content: "SVG",
font: { size: 16, weight: "bold", family: "sans-serif" },
fill: "#333",
}),
],
}),
],
}),
],
}),
],
})
);