Draw Rect
Runnable luma.gl example: Draw Rect. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
说明:四个顶点配一条索引缓冲,两个三角形拼成矩形,六个索引复用四个顶点
核心逻辑:索引缓冲 + Model 的 indexCount,绘制时由 GPU 按索引取顶点
依赖:@luma.gl/core 9.4.2、webgl、shadertools、engine · jsDelivr CDN(UMD)
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>绘制矩形</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:12px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar .sp{flex:1 1 auto}
.wrap{position:relative;flex:1 1 auto;min-height:0;display:flex}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
#err{position:absolute;left:0;right:0;top:0;padding:16px;color:#ff9d85;
font:12px/1.7 ui-monospace,Consolas,monospace;white-space:pre-wrap;display:none}
</style>
</head>
<body>
<div class="bar">
<span>顶点 <b>4</b> · 索引 <b>6</b> · 三角形 <b>2</b></span>
<button id="shape">切换为竖矩形</button>
<span class="sp"></span>
<span>索引复用:一条缓冲画两个三角形</span>
</div>
<div class="wrap">
<canvas id="cv"></canvas>
<div id="err"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/core@9.4.2/dist/dist.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/webgl@9.4.2/dist/dist.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/shadertools@9.4.2/dist/dist.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/engine@9.4.2/dist/dist.min.js"></script>
<script>
(function () {
'use strict';
var cv = document.getElementById('cv');
var errBox = document.getElementById('err');
function fail(msg) {
errBox.style.display = 'block';
errBox.textContent = '运行失败:' + msg;
}
if (!window.luma || !luma.Model || !luma.AnimationLoop) {
fail('luma.gl 未加载成功,请检查能否访问 cdn.jsdelivr.net');
return;
}
/* 四个角点,顺序按逆时针;两个三角形共用 0 与 2 号顶点 */
var wide = new Float32Array([
-0.78, -0.42, 0,
0.78, -0.42, 0,
0.78, 0.42, 0,
-0.78, 0.42, 0
]);
var tall = new Float32Array([
-0.42, -0.78, 0,
0.42, -0.78, 0,
0.42, 0.78, 0,
-0.42, 0.78, 0
]);
var positions = wide.slice(0);
var colors = new Float32Array([
0.36, 0.78, 0.98,
0.36, 0.78, 0.98,
0.98, 0.62, 0.30,
0.98, 0.62, 0.30
]);
/* 0-1-2 与 0-2-3 拼成矩形,索引只有一次上传 */
var indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
luma.luma.createDevice({
type: 'webgl',
adapters: [luma.webgl2Adapter],
createCanvasContext: {
canvas: cv,
useDevicePixels: Math.min(window.devicePixelRatio, 2),
autoResize: true
}
}).then(function (device) {
var posBuffer = device.createBuffer({
data: positions,
usage: luma.Buffer.VERTEX | luma.Buffer.COPY_DST
});
var colBuffer = device.createBuffer({
data: colors,
usage: luma.Buffer.VERTEX
});
var idxBuffer = device.createBuffer({
data: indices,
usage: luma.Buffer.INDEX,
indexType: 'uint16'
});
/* vertexCount 是顶点数,indexCount 是索引数,两者不能混用 */
var model = new luma.Model(device, {
id: 'indexed-rect',
topology: 'triangle-list',
vertexCount: 4,
indexCount: 6,
indexBuffer: idxBuffer,
attributes: {
positions: posBuffer,
colors: colBuffer
},
bufferLayout: [
{ name: 'positions', stepMode: 'vertex', format: 'float32x3' },
{ name: 'colors', stepMode: 'vertex', format: 'float32x3' }
],
vs: [
'#version 300 es',
'in vec3 positions;',
'in vec3 colors;',
'out vec3 vColor;',
'void main() {',
' vColor = colors;',
' gl_Position = vec4(positions, 1.0);',
'}'
].join('\n'),
fs: [
'#version 300 es',
'precision highp float;',
'in vec3 vColor;',
'out vec4 fragColor;',
'void main() {',
' fragColor = vec4(vColor, 1.0);',
'}'
].join('\n')
});
var loop = new luma.AnimationLoop({
device: device,
onRender: function (props) {
var pass = props.device.beginRenderPass({ clearColor: [0.04, 0.06, 0.09, 1] });
model.draw(pass);
pass.end();
},
onError: function (e) { fail(e.message); }
});
loop.start();
var isWide = true;
var shapeBtn = document.getElementById('shape');
shapeBtn.addEventListener('click', function () {
isWide = !isWide;
positions.set(isWide ? wide : tall);
/* 只覆盖顶点坐标,索引与着色器都不动 */
posBuffer.write(positions);
shapeBtn.textContent = isWide ? '切换为竖矩形' : '切换为横矩形';
loop.setNeedsRedraw('shape');
});
}).catch(function (e) {
fail(e && e.message ? e.message : String(e));
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.