【发布时间】:2015-06-29 09:39:54
【问题描述】:
我正在我的 THREE.js 应用程序中创建二维表面,方法是创建 PlaneGeometry/BasicMaterial 网格并用画布支持它们的纹理:
this.canvas = makeCanvas(canvasWidth, canvasHeight);
this.ctx = this.canvas.getContext('2d');
this.texture = new THREE.Texture(this.canvas);
this.texture.minFilter = THREE.LinearFilter;
this.texture.magFilter = THREE.LinearFilter;
this.texture.anisotropy = 16;
this.mesh = new THREE.Mesh(new THREE.PlaneGeometry(width, height), new THREE.MeshBasicMaterial({
map: this.texture,
overdraw: true,
side: THREE.DoubleSide,
transparent: true
}));
这很好用——除非我想透明地绘制。然后,我需要创建另一个纹理以绑定为alphaMap,并在两个画布上下文之间复制我的所有绘图操作。性能很好,但代码看起来绝对可怕:
var circleAlpha = 'rgb(100, 100, 100);',
segmentAlpha = 'rgb(200, 200, 200);';
ctx.fillStyle = 'rgb(0, 255, 255);';
if (this.segment === -1) {
circleAlpha = segmentAlpha;
}
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.alphaCtx.clearRect(0, 0, this.canvas.width, this.canvas.height);
var drawArc = function (c, w, h, piece) {
var angw = 2 * Math.PI / 6;
c.beginPath();
c.arc(w / 2, h / 2, 512, angw * piece, angw * (piece + 1), false);
c.arc(w / 2, h / 2, 300, angw * (piece + 1), angw * piece, true);
c.closePath();
c.fill();
};
for (var i = 0; i < 6; i++) {
this.alphaCtx.fillStyle = i == this.segment ? segmentAlpha : circleAlpha;
drawArc(ctx, this.canvas.width, this.canvas.height, i);
drawArc(this.alphaCtx, this.canvas.width, this.canvas.height, i);
}
this.updateTexture();
this.alphaTexture.needsUpdate = true;
我一直在计划编写一个小型实用程序库来自动处理这个问题,但在我这样做之前,我想知道我是不是很傻,有一种更简单的方法可以做到这一点。
【问题讨论】:
标签: javascript canvas opengl-es three.js html5-canvas