- 离屏 Canvas 使用自定义字体,为什么不生效?
// Step 1: 加载字体 uni.loadFontFace({ family: 'Digifaw', source: 'url("/static/fonts/Digifaw.ttf")', global: true, success: (res) => { console.log('✅ 字体加载成功', res) }, fail: (err) => { console.error('❌ 字体加载失败', err) } }) createOrUpdateCanvasTexture(text) { console.log('Creating or updating canvas texture with text:', text); // 创建离屏 Canvas const canvas = uni.createOffscreenCanvas({ type: '2d', width: 2500, // 提高分辨率 height: 1000, // 提高分辨率 }); const context = canvas.getContext('2d'); // 清除画布并绘制新的文本 context.clearRect(0, 0, canvas.width, canvas.height); // 设置字体样式 context.fillStyle = 'black'; // 文本颜色 context.font = 'normal 66px "Digifaw", sans-serif'; context.textAlign = 'right'; // 旋转画布 90 度(右旋) context.save(); // 保存当前状态 context.translate(canvas.width / 2, canvas.height / 2); // 移动到画布中心 context.rotate(Math.PI / 2); // 旋转 90 度(右旋) context.translate(-canvas.width / 2, -canvas.height / 2); // 恢复原位 // 垂直拉伸文本 context.scale(1, 1.6); context.fillText(text, canvas.width / 2, canvas.height / 2); context.restore(); // 恢复之前保存的状态 // 将文本内容存储在 Canvas 对象上,以便后续比较 canvas.text = text; // 输出 Canvas 的宽度和高度,确保它们符合预期 console.log('Canvas dimensions: ', { width: canvas.width, height: canvas.height }); return new THREE.CanvasTexture(canvas); }, updateScreenText01(mesh, newText) { if (!mesh || !mesh.material || !mesh.material.map) { console.error('Invalid mesh or material.'); return; } const currentTexture = mesh.material.map; const currentCanvas = currentTexture.image; mesh.material.depthTest = false; console.log('Current canvas text:', currentCanvas && currentCanvas.text); console.log('New text:', newText); // 如果文本没有变化,则无需更新 if (currentCanvas && currentCanvas.text === newText) { console.log('Text has not changed, no need to update.'); return; } // 如果材质已经有纹理,则直接更新纹理 if (currentCanvas && typeof currentCanvas.getContext === 'function') { const context = currentCanvas.getContext('2d'); // 清空 Canvas context.clearRect(0, 0, currentCanvas.width, currentCanvas.height); // 设置字体样式 context.fillStyle = 'black'; context.font = 'normal 335px "Digifaw", sans-serif'; context.textAlign = 'right'; // 旋转画布 90 度(右旋) context.save(); // 保存当前状态 // 添加倾斜 const skewAngle = 1.5 * Math.PI / 180; // 将角度转换为弧度 const skewX = -Math.tan(skewAngle); // 计算x轴方向的倾斜系数 context.transform(1, 0, skewX, 1, 0, 0); // 应用变换 // 垂直拉伸文本 context.scale(0.5, 2.0); context.fillText(newText, currentCanvas.width / 2 + 440, currentCanvas.height / 2 - 160); context.restore(); // 恢复之前保存的状态 // 标记材质需要更新 mesh.material.map.needsUpdate = true; // 打印 Canvas 的尺寸和内容 console.log('Updated canvas dimensions:', { width: currentCanvas.width, height: currentCanvas.height }); } else { // 否则创建新的纹理 mesh.material.map = this.createOrUpdateCanvasTexture(newText); mesh.material.map.needsUpdate = true; } // 确保材质支持透明度 mesh.material.transparent = true; }, [图片] 数码管字体并没有生效
04-11 - 如何获取getBluetoothDevices返回蓝牙设备的广播数据段中的 ServiceData?
[图片] 问题描述: serviceData的值是Object类型,该如何调用下面的属性呢? 错误调用方式:device.serviceData.00001523-0000-1000-8000...... 该怎么调用呢?
2020-11-03