-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathManagedContextExample.tsx
More file actions
183 lines (159 loc) · 5.5 KB
/
ManagedContextExample.tsx
File metadata and controls
183 lines (159 loc) · 5.5 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
import { Component, StatefulComponent } from 'valdi_core/src/Component';
import { systemBoldFont, systemFont } from 'valdi_core/src/SystemFont';
import { AnyRenderFunction } from 'valdi_core/src/AnyRenderFunction';
import { createManagedContext, EmbeddedPlatformViewRasterMethod } from 'drawing/src/ManagedContextFactory';
import { BitmapAlphaType, BitmapColorType, ImageEncoding } from 'drawing/src/IBitmap';
import { createBitmap, createBitmapWithBuffer } from 'drawing/src/BitmapFactory';
import { Asset } from 'valdi_tsx/src/Asset';
import { makeAssetFromBytes } from 'valdi_core/src/Asset';
import { Device } from 'valdi_core/src/Device';
const COMPONENT_HEIGHT = 200;
class InnerComponent extends Component {
onRender(): void {
<view width={'100%'} height={'100%'} backgroundColor="black" padding={60} touchEnabled={false} slowClipping>
<view
padding={8}
background="linear-gradient(90deg, lightgray, gray)"
borderRadius={16}
alignSelf="center"
rotation={Math.PI / -8}
borderWidth={1}
borderColor="red"
>
<view marginLeft={8} marginRight={8} width={200} height={60} borderRadius={'50%'}>
<textfield
width={230}
height={60}
limitToViewport={false}
position="absolute"
value={'Hello, World!'}
color="red"
font={systemBoldFont(40)}
touchEnabled={false}
/>
</view>
</view>
</view>;
}
}
function Section(viewModel: { name: string; children?: AnyRenderFunction | void }) {
<layout flexDirection="column">
<layout padding={16}>
<label value={viewModel.name} font={systemBoldFont(17)} color="black" />
</layout>
{viewModel.children?.()}
</layout>;
}
function Button(viewModel: { title: string; onTap: () => void }) {
<view
backgroundColor="cornflowerblue"
padding={16}
borderRadius={16}
boxShadow={'1 1 3 rgba(0, 0, 0, 0.16)'}
onTap={viewModel.onTap}
marginBottom={8}
>
<label value={viewModel.title} font={systemBoldFont(14)} color="black" />
</view>;
}
const DOWNSCALE_RATIO = [1, 2, 4, 8];
interface State {
rasterMethod: EmbeddedPlatformViewRasterMethod;
rasterizedAsset?: Asset;
downscaleRatioIndex: number;
}
export class App extends StatefulComponent<{}, State> {
state: State = {
rasterMethod: EmbeddedPlatformViewRasterMethod.FAST,
downscaleRatioIndex: 0,
};
onRender(): void {
<view width={'100%'} height={'100%'} paddingTop={Device.getDisplayTopInset()} backgroundColor="white">
<Section name="Preview">
{
<layout height={COMPONENT_HEIGHT}>
<InnerComponent />
</layout>
}
</Section>
<Section name="Rasterized">
{<image src={this.state.rasterizedAsset} width={'100%'} height={COMPONENT_HEIGHT} />}
</Section>
<Section name="Controls">
<layout flexDirection="column" padding={8}>
<Button
title={`Raster method: ${
this.state.rasterMethod === EmbeddedPlatformViewRasterMethod.FAST ? 'Fast' : 'Accurate'
}`}
onTap={this.toggleRasterMethod}
/>
<Button
title={`Downscale ratio: ${DOWNSCALE_RATIO[this.state.downscaleRatioIndex]}`}
onTap={this.toggleDownscaleRatio}
/>
<Button title="Rasterize" onTap={this.rasterize} />
</layout>
</Section>
</view>;
}
toggleDownscaleRatio = () => {
const nextDownscaleRatioIndex = (this.state.downscaleRatioIndex + 1) % DOWNSCALE_RATIO.length;
this.setState({
downscaleRatioIndex: nextDownscaleRatioIndex,
});
};
toggleRasterMethod = () => {
this.setState({
rasterMethod:
this.state.rasterMethod === EmbeddedPlatformViewRasterMethod.FAST
? EmbeddedPlatformViewRasterMethod.ACCURATE
: EmbeddedPlatformViewRasterMethod.FAST,
});
};
rasterize = () => {
this.doRasterize()
.then(asset => {
console.log('Rasterized asset:', asset);
this.setState({ rasterizedAsset: asset });
})
.catch(error => {
console.error('Error rasterizing:', error);
});
};
private getComponentSize(): [number, number] {
const width = this.renderer.getComponentRootElements(this, true)[0].frame.width;
const height = COMPONENT_HEIGHT;
return [width, height];
}
private async doRasterize(): Promise<Asset> {
const [componentWidth, componentHeight] = this.getComponentSize();
const context = createManagedContext({
embeddedPlatformViewRasterMethod: this.state.rasterMethod,
deltaRasterization: true,
});
context.render(() => {
<InnerComponent />;
});
await context.onAllAssetsLoaded();
await context.layout(componentWidth, componentHeight, false);
const { frame } = await context.draw();
const scale = Device.getDisplayScale() / DOWNSCALE_RATIO[this.state.downscaleRatioIndex];
const rasterWidth = Math.floor(scale * componentWidth);
const rasterHeight = Math.floor(scale * componentHeight);
const bytesPerPixel = 4;
const bitmap = createBitmap({
width: rasterWidth,
height: rasterHeight,
colorType: BitmapColorType.RGBA8888,
alphaType: BitmapAlphaType.OPAQUE,
rowBytes: rasterWidth * bytesPerPixel,
});
frame.rasterInto(bitmap, true);
const buffer = bitmap.encode(ImageEncoding.PNG, 1.0);
frame.dispose();
bitmap.dispose();
context.dispose();
return makeAssetFromBytes(buffer);
}
}
const styles = {};