[英]html5 3d, canvas, three.js
I have the following HTML 我有以下HTML
<canvas id="container" name ="container" width="300" height="300"></canvas>
and JavaScript code 和JavaScript代码
var WIDTH = 400, HEIGHT = 300;
var VIEW_ANGLE = 90, ASPECT = WIDTH / HEIGHT, NEAR = 1, FAR = 300;
renderer = new THREE.CanvasRenderer();
renderer.setSize( WIDTH, HEIGHT );
camera = new THREE.PerspectiveCamera(VIEW_ANGLE,ASPECT,NEAR,FAR);
camera.position.z = 200;
scene = new THREE.Scene();
scene.add(camera);
renderer.render( scene, camera );
document.body.appendChild( renderer.domElement );
//$('#container').appendChild(renderer.domElement);
$('#container').width(WIDTH);
$('#container').height(HEIGHT);
function getSphere() {
var radius = 50, segments = 16, rings = 16;
var geometry = new THREE.SphereGeometry(radius,segments,rings);
var material = new THREE.MeshLambertMaterial({
color: 0xCC0000
});
return new THREE.Mesh(geometry, material);
}
document.body.appendChild( renderer.domElement );
works, 作品,
But how to write into canvas with jQuery? 但是如何用jQuery写入画布?
$('#container').appendChild(renderer.domElement);
does not work. 不起作用。
You may try this: 你可以试试这个:
$('#container').append(renderer.domElement);
$('#container')
returns a jQuery Object, where appendChild()
is a JavaScript method which works with a JavaScript DOM element, but not with a jQuery object. $('#container')
返回一个jQuery对象,其中appendChild()
是一个JavaScript方法,它与JavaScript DOM元素一起使用,但不与jQuery对象一起使用。
Therefore, to append use the jQuery method .append()
. 因此,要追加使用jQuery方法
.append()
。
As your #container
is a canvas
element, it does not support the appendChild()
method. 由于
#container
是canvas
元素,因此它不支持appendChild()
方法。 Instead you can change it to a div
like: 相反,您可以将其更改为
div
:
<div id="container" name="container" style:"width:300;height:300"></div>
and then try with: 然后尝试:
document.getElementById('container').appendChild(renderer.domElement);
OR 要么
$('#container').append(renderer.domElement);
Here, you do not need to add a <canvas>
either, because renderer.domElement
is a canvas
element. 在这里,您也不需要添加
<canvas>
,因为renderer.domElement
是一个canvas
元素。 For example, in this link , the view source shows there is no <canvas>
in the HTML
. 例如,在此链接中 ,视图源显示
HTML
没有<canvas>
。 Instead, it creates a div
by JavaScript, appends that to the DOM
and at last appends the renderer.domElement
(which is <canvas>
) to that div
. 相反,它通过JavaScript创建一个
div
,将其附加到DOM
,最后将renderer.domElement
(它是<canvas>
)附加到该div
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.