add same scene but with threeJS
This commit is contained in:
parent
5b136ab38d
commit
76028e9de3
2 changed files with 24 additions and 17 deletions
|
@ -9,16 +9,10 @@
|
|||
body {
|
||||
margin: 0px;
|
||||
}
|
||||
|
||||
canvas {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<canvas id="canvas" width="1280" height="720"></canvas>
|
||||
|
||||
<script src="https://unpkg.com/three@0.146.0/build/three.min.js"></script>
|
||||
<script type="module" src="./js/main.js"></script>
|
||||
</body>
|
||||
|
|
35
js/main.js
35
js/main.js
|
@ -1,16 +1,29 @@
|
|||
window.addEventListener("load", () => main());
|
||||
|
||||
const main = () => {
|
||||
const cnv = document.getElementById("canvas");
|
||||
const ctx = cnv.getContext("2d");
|
||||
|
||||
const size = 100;
|
||||
|
||||
ctx.fillStyle = "blue";
|
||||
ctx.fillRect(
|
||||
cnv.width / 2 - size / 2,
|
||||
cnv.height / 2 - size / 2,
|
||||
size,
|
||||
size
|
||||
const scene = new THREE.Scene();
|
||||
const camera = new THREE.PerspectiveCamera(
|
||||
75,
|
||||
window.innerWidth / window.innerHeight,
|
||||
0.1,
|
||||
1000
|
||||
);
|
||||
|
||||
const renderer = new THREE.WebGLRenderer();
|
||||
renderer.setSize(window.innerWidth, window.innerHeight);
|
||||
document.body.appendChild(renderer.domElement);
|
||||
|
||||
const geometry = new THREE.BoxGeometry(1, 1, 1);
|
||||
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
|
||||
const cube = new THREE.Mesh(geometry, material);
|
||||
scene.add(cube);
|
||||
|
||||
camera.position.z = 5;
|
||||
|
||||
const animate = () => {
|
||||
requestAnimationFrame(animate);
|
||||
renderer.render(scene, camera);
|
||||
}
|
||||
|
||||
animate();
|
||||
};
|
||||
|
|
Reference in a new issue