简体   繁体   English

Three.js不在现有画布上渲染

[英]Three.js not rendering on existing canvas

I created a canvas with an id of 'canvas' which I provided as an argument to the WebGLRenderer of Three.js. 我创建了一个ID为'canvas'的画布,并将其作为Three.js的WebGLRenderer的参数提供。 However, nothing is showing up on that canvas. 但是,该画布上没有任何显示。 If I append the domElement to the document, the canvas shows up on the bottom but I would like to draw on my existing canvas. 如果将domElement附加到文档,则画布显示在底部,但我想在现有的画布上绘制。 Is there an extra setting I have to change? 我需要更改其他设置吗?

I am using this example code to start off with: 我使用此示例代码开始于:

  ctx = $('canvas').getContext('2d');
  var canvasElm = $('canvas');
  canvasWidth = parseInt(canvasElm.width);
  canvasHeight = parseInt(canvasElm.height);
  canvasTop = parseInt(canvasElm.style.top);
  canvasLeft = parseInt(canvasElm.style.left);

  var scene = new THREE.Scene(); // Create a Three.js scene object.
  var camera = new THREE.PerspectiveCamera(75, canvasWidth / canvasHeight, 0.1, 1000); // Define the perspective camera's attributes.

  var renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer(canvasElm) : new THREE.CanvasRenderer(); // Fallback to canvas renderer, if necessary.
  renderer.setSize(canvasWidth, canvasHeight); // Set the size of the WebGL viewport.
  //document.body.appendChild(renderer.domElement); // Append the WebGL viewport to the DOM.

  var geometry = new THREE.CubeGeometry(20, 20, 20); // Create a 20 by 20 by 20 cube.
  var material = new THREE.MeshBasicMaterial({ color: 0x0000FF }); // Skin the cube with 100% blue.
  var cube = new THREE.Mesh(geometry, material); // Create a mesh based on the specified geometry (cube) and material (blue skin).
  scene.add(cube); // Add the cube at (0, 0, 0).

  camera.position.z = 50; // Move the camera away from the origin, down the positive z-axis.

  var render = function () {
   cube.rotation.x += 0.01; // Rotate the sphere by a small amount about the x- and y-axes.
   cube.rotation.y += 0.01;

   renderer.render(scene, camera); // Each time we change the position of the cube object, we must re-render it.
   requestAnimationFrame(render); // Call the render() function up to 60 times per second (i.e., up to 60 animation frames per second).
  };

  render(); // Start the rendering of the animation frames.

I am using Chrome 56.0.2924.87 (64-bit) if that helps. 如果有帮助,我正在使用Chrome 56.0.2924.87(64位)。

By using jQuery selectors you are getting jQuery object instead of native HTMLElement. 通过使用jQuery选择器,您将获得jQuery对象而不是本机HTMLElement。

Try using querySelector() to get canvas: 尝试使用querySelector()获取画布:

var canvas = document.querySelector('canvas');
var canvasElm = $(canvas);
ctx = canvas.getContext('2d');
...
var renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer({canvas: canvas}) : new THREE.CanvasRenderer();

Notice that canvas element passed using object to WebGLRenderer 注意, canvas元素使用对象传递给WebGLRenderer

Your jquery selector is wrong (I am assuming it is jquery). 您的jquery选择器错误(我假设它是jquery)。
var canvasElm = $('canvas'); creates a new canvas element. 创建一个新的canvas元素。
If you want to select a canvas that has the id of 'canvas', use.. 如果要选择ID为'canvas'的画布,请使用。
var canvasElm = $('#canvas');
But this then gets a jquery object / list, so to get the actual canvas (first item in the list) you could use.. 但这然后获得了一个jquery对象/列表,因此可以使用实际的画布(列表中的第一项)。
var canvasElm = $('#canvas')[0];

eg. 例如。

var canvasElm = $('#canvas')[0];
renderer = new THREE.WebGLRenderer( { canvas: canvasElm } );

You would probably be better just using js without jquery. 仅使用不带jquery的js可能会更好。
eg. 例如。

canvasElm = document.getElementById('canvas');
renderer = new THREE.WebGLRenderer( { canvas: canvasElm } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM