| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - html texture</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #ffffff;
- }
- #draw_element {
- width: 600px;
- background-color: #aaaaaa;
- color: #000000;
- font-family: sans-serif;
- font-size: 30px;
- line-height: 1.5;
- text-align: center;
- padding: 30px;
- /* border: 10px solid #cccccc; */
- }
- #draw_element img {
- animation: swing 1s ease-in-out infinite alternate;
- }
- #draw_element input[type="text"] {
- font-size: 24px;
- padding: 8px 12px;
- border: 2px solid #888;
- border-radius: 6px;
- width: 80%;
- margin-top: 10px;
- }
- #draw_element button {
- font-size: 24px;
- padding: 8px 20px;
- margin-top: 10px;
- border: none;
- border-radius: 6px;
- background-color: #4CAF50;
- color: white;
- cursor: pointer;
- }
- #draw_element button:hover {
- background-color: #2196F3;
- }
- @keyframes swing {
- from { transform: rotate(-15deg); }
- to { transform: rotate(15deg); }
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - HTMLTexture
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
- import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
- import { InteractionManager } from 'three/addons/interaction/InteractionManager.js';
- let camera, scene, renderer, mesh, interactions;
- init();
- function init() {
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- if ( renderer.getContext().texElementImage2D === undefined ) {
- info.innerHTML += '<br>This browser does not support the <a href="https://github.com/WICG/html-in-canvas" target="_blank">HTML-in-Canvas API</a>.';
- }
- renderer.toneMapping = THREE.NeutralToneMapping;
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.z = 500;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xaaaaaa );
- scene.environment = new THREE.PMREMGenerator( renderer ).fromScene( new RoomEnvironment(), 0.02 ).texture;
- // HTML element
- const element = document.createElement( 'div' );
- element.id = 'draw_element';
- element.innerHTML = `
- Hello world!<br>I'm multi-line, <b>formatted</b>,
- rotated text with emoji (😀), RTL text
- <span dir=rtl>من فارسی صحبت میکنم</span>,
- vertical text,
- <p style="writing-mode: vertical-rl;">
- 这是垂直文本
- </p>
- an inline image (<img width="150" src="textures/758px-Canestra_di_frutta_(Caravaggio).jpg">), and
- <svg width="50" height="50">
- <circle cx="25" cy="25" r="20" fill="green" />
- <text x="25" y="30" font-size="15" text-anchor="middle" fill="#fff">
- SVG
- </text>
- </svg>!
- <br>
- <input type="text" placeholder="Type here...">
- <button>Click me</button>
- `;
- const geometry = new RoundedBoxGeometry( 200, 200, 200, 10, 10 );
- const material = new THREE.MeshStandardMaterial( { roughness: 0, metalness: 0.5 } );
- material.map = new THREE.HTMLTexture( element );
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- // Interaction
- interactions = new InteractionManager();
- interactions.connect( renderer, camera );
- interactions.add( mesh );
- // Button click handler
- element.querySelector( 'button' ).addEventListener( 'click', function () {
- this.textContent = 'Clicked!';
- } );
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate( time ) {
- mesh.rotation.x = Math.sin( time * 0.0005 ) * 0.5;
- mesh.rotation.y = Math.cos( time * 0.0008 ) * 0.5;
- interactions.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|