美文网首页
使用canvas元素作为texture

使用canvas元素作为texture

作者: 普莱那 | 来源:发表于2022-01-16 20:41 被阅读0次
// 创建几何体
const boxGeometry = new THREE.BoxGeometry(1, 1, 1);

// 材质
const boxMaterial = new THREE.MeshBasicMaterial({
  transparent: true,
});
const canvas = createCanvas(400, 400);
const canvasTexture = new THREE.CanvasTexture(canvas);
boxMaterial.map = canvasTexture;

// 网格
const box = new THREE.Mesh(
  boxGeometry,
  boxMaterial
);

// canvas的宽高如何确定?需要结合webgl里该材质的显示大小,然后再结合屏幕像素比即可
// 比如上面的边长为1的box,显示到屏幕上,可能会占到200px
// 然后再结合到屏幕的设备像素比为2的话,那么制作canvas材质时,尺寸应该为 200 * 2 = 400

function createCanvas(w, h) {
    const canvas = document.createElement('canvas');
    canvas.classList.add('test');
    const context = canvas.getContext('2d');
    canvas.width = w;
    canvas.height = h;
    context.textAlign = 'center';
    context.fillStyle = '#ff0000';
    context.fillRect(0, 0, w, w);
    context.fillStyle = '#fff';
    context.font = `${0.1 * w}px sans-serif`;
    context.fillText(`${Math.random().toFixed(5)}`, w * 0.5, h * 0.5);
    return canvas;
}

相关文章

  • 使用canvas元素作为texture

  • canvas基本操作

    创建canvas元素, 使用id获取canvas元素;var c = document.getElementByI...

  • canvas 的基本用法

    一、元素 1、写法: 2、宽高设置 在使用 元素时必须设置宽度和高...

  • HTML5 Canvas

    canvas元素用于网页上绘制图形。 什么事Canvas? HTML5的canvas元素使用JavaScript在...

  • HTML 5 Canvas

    canvas 元素用于在网页上绘制图形。 什么是 Canvas? HTML5 的 canvas 元素使用 Java...

  • canvas实用技巧及案例

    canvas实用技巧及案例 canvas作为H5的新增元素,使用方便,作用广泛,因此我们很有必要把它学会,乃至融会...

  • canvas浅尝

    简单了解canvas 1.什么是canvas HTML5 的 canvas 元素使用 JavaScript 在网页...

  • canvas学习一

    一、什么是canvas? canvas 是 HTML5 的标签元素,使用 JavaScript 在canvas里绘...

  • Canvas 碰撞检测

    canvas 的尺寸分为两种,一个是 canvas 作为 html 元素本身的尺寸,另外一个 canvas 作为绘...

  • HTML5 canvas

    canvas元素用于在网页上绘制图形那么什么是canvas?HTML5的canvas元素使用脚本通常是用JavaS...

网友评论

      本文标题:使用canvas元素作为texture

      本文链接:https://www.haomeiwen.com/subject/egfqhrtx.html