-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbasic-usage.html
More file actions
166 lines (147 loc) · 5.2 KB
/
basic-usage.html
File metadata and controls
166 lines (147 loc) · 5.2 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ThorVG WebCanvas - Basic Usage</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background: #f0f0f0;
}
#canvas {
border: 1px solid #333;
background: white;
margin: 20px 0;
}
.controls {
margin: 10px 0;
}
button {
padding: 10px 20px;
margin: 5px;
cursor: pointer;
}
.info {
background: #fff;
padding: 15px;
border-radius: 5px;
margin: 10px 0;
}
</style>
</head>
<body>
<h1>ThorVG WebCanvas - Basic Usage</h1>
<div class="info">
<h3>Usage Pattern:</h3>
<pre><code>// 1. Import and initialize ThorVG
const ThorVG = await import('../packages/webcanvas/dist/webcanvas.esm.js');
const TVG = await ThorVG.init({ renderer: 'sw' }); // or 'gl', 'wg'
// 2. Create canvas and start drawing
const canvas = new TVG.Canvas('#canvas', {
width: 800,
height: 600,
renderer: 'sw'
});</code></pre>
</div>
<div class="controls">
<label>Backend:
<select id="backend">
<option value="sw">Software</option>
<option value="gl">WebGL</option>
<option value="wg">WebGPU</option>
</select>
</label>
<button onclick="initAndDraw()">Initialize & Draw</button>
<button onclick="clearCanvas()">Clear</button>
</div>
<canvas id="canvas" width="800" height="600"></canvas>
<div class="info">
<h3>Status:</h3>
<pre id="log"></pre>
</div>
<script type="module">
let TVG = null;
let canvas = null;
function log(msg) {
const logEl = document.getElementById('log');
logEl.textContent += msg + '\n';
console.log(msg);
}
window.initAndDraw = async function() {
try {
const renderer = document.getElementById('backend').value;
log(`\n=== Initializing with ${renderer} renderer ===`);
// Step 1: Initialize ThorVG with renderer
if (!TVG) {
log('1. Loading ThorVG WASM module and initializing engine...');
const wasmModule = await import('../packages/webcanvas/dist/webcanvas.esm.js');
TVG = await wasmModule.init({
renderer: renderer,
locateFile: (path) => {
log(` locateFile: ${path}`);
return '../packages/webcanvas/dist/' + path.split('/').pop();
}
});
log('✓ Module loaded and engine initialized');
}
// Step 2: Create canvas
log('2. Creating canvas...');
if (canvas) {
canvas.clear();
}
canvas = new TVG.Canvas('#canvas', {
width: 800,
height: 600,
renderer: renderer
});
log('✓ Canvas created');
// Step 3: Draw shapes
log('3. Drawing shapes...');
// Red rectangle with rounded corners
const rect = new TVG.Shape();
rect.appendRect(100, 100, 200, 150, { rx: 10, ry: 10 });
rect.fill(255, 0, 0, 255);
// Blue circle with black stroke
const circle = new TVG.Shape();
circle.appendCircle(500, 200, 80, 80);
circle.fill(0, 100, 255, 255);
circle.stroke({ width: 5, color: [0, 0, 0, 255] });
// Green rectangle
const rect2 = new TVG.Shape();
rect2.appendRect(300, 400, 150, 100);
rect2.fill(0, 200, 100, 255);
// Add to canvas and render
canvas.add(rect).add(circle).add(rect2);
canvas.render();
log('✓ Drawing complete!');
} catch (error) {
log('ERROR: ' + error.message);
console.error(error);
}
};
window.clearCanvas = function() {
if (!canvas) {
log('ERROR: Canvas not initialized!');
return;
}
log('Clearing canvas...');
canvas.clear();
log('✓ Canvas cleared');
};
// Apply renderer from query string
const params = new URLSearchParams(window.location.search);
const renderer = params.get('renderer');
if (renderer && ['sw', 'gl', 'wg'].includes(renderer)) {
document.getElementById('backend').value = renderer;
}
// Handle backend change
document.getElementById('backend').addEventListener('change', (e) => {
const url = new URL(window.location.href);
url.searchParams.set('renderer', e.target.value);
window.location.href = url.toString();
});
log('Ready! Click "Initialize & Draw" to start.');
</script>
</body>
</html>