【发布时间】:2015-01-22 17:04:12
【问题描述】:
我找到了 three.js 的示例,我正在尝试在 <canvas></canvas> 元素上实现它。
我得到了对元素的引用,但没有得到视觉效果。我的画布元素的 ID 为“mycanvas”。
<canvas id="mycanvas" style="border: 5px solid white" width="600" height="500"></canvas>
我在主体中使用了一个名为 WebGLStart() 的 onload 函数,它调用下面的脚本。
<script>
function WebGLStart()
{
//Get the canvas and the context
var canvas = document.getElementById('mycanvas');
var canvas_context = canvas.getContext("webgl");
console.log("Canvas: "+canvas.width);
console.log("Canvas: "+canvas.height);
var RENDER_DIST = 1000,
FOV = 75;
var WIDTH = canvas.width,
HEIGHT= canvas.height;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(FOV, WIDTH / HEIGHT, 0.1, RENDER_DIST);
camera.position.z = 100;
scene.add(camera);
renderer = new THREE.WebGLRenderer();
renderer.setSize(WIDTH,HEIGHT);
console.log("R info: "+renderer.info);
canvas.appendChild(renderer.domElement);
init();
loopRun();
function init()
{
var geometry = new THREE.SphereGeometry(50);
var material = new THREE.MeshBasicMaterial({color: 0xff0000});
var sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
}
function loopRun()
{
requestAnimationFrame(loopRun);
renderer.render(scene, camera);
}
}
</script>
有什么理由不显示吗? 我在 chromes 提示(画布宽度和高度)上得到输出,但没有显示。任何想法将不胜感激
【问题讨论】:
标签: javascript html google-chrome canvas three.js