github three.js having one javascript file including these two paragraphs (js files) as mentioned in help file - github

I have a question in gihub three.js
https://github.com/mrdoob/three.js
When I want to have one javascript file including these two paragraphs (js files) as mentioned in help file what should I do?
I used require but the browser error is about THREE object
first paragraph :
<script src="js/three.min.js"></script>
second paragraph:
var scene, camera, renderer;
var geometry, material, mesh;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
geometry = new THREE.BoxGeometry( 200, 200, 200 );
material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
function animate() {
requestAnimationFrame( animate );
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
renderer.render( scene, camera );
}
and the tempcubejava.js I had been created is this:
//------------------
//const tempthree = require('./three.js')({regl})
require('./three.js')()
//------------------
//-----------------
var scene
var camera2
var renderer;
var geometry, material
var mesh2;
init();
//animate();
function init() {
scene = new THREE.Scene();
camera2 = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera2.position.z = 1000;
geometry = new THREE.BoxGeometry( 200, 200, 200 );
material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
mesh2 = new THREE.Mesh( geometry, material );
scene.add( mesh2 );
renderer = new THREE.WebGLRenderer();
renderer.autoClear = false;
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.render( scene, camera2 );
}
function animate() {
requestAnimationFrame( animate );
mesh.rotation.x += 0.05;
mesh.rotation.y += 0.02;
renderer.autoClear = false;
renderer.render( scene, camera2 );
}
//-----------------
so the result is :`
node tempcubejava.js
C:\femgl\tempcubejava.js:3
require('./three.js')()
^
TypeError: require(...) is not a function
at Object. (C:\femgl\tempcubejava.js:3:22)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:148:9)
and when I changed first lines as these:
//------------------
const tempthree = require('./three.js')
//require('./three.js')()
//------------------
I See this :
node tempcubejavaedited.js
C:\femgl\tempcubejavaedited.js:18
scene = new tempthree.THREE.Scene();
^
TypeError: Cannot read property 'Scene' of undefined
at init (C:\femgl\tempcubejavaedited.js:18:29)
at Object. (C:\femgl\tempcubejavaedited.js:13:1)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:148:9)
so what should I do to have a separated JS file
Best Regards

The recommended way is to simply include the three.js-library seperately in your HTML, like so:
<script src="/path/to/three.min.js"></script>
<script src="/path/to/your-script.js"></script>
This will make three.js available via the global variable THREE, or window.THREE and you can just use it from your script.
If you totally want to use three.js as a module (install the latest three.js version with npm install three), it should work like this:
var THREE = require('three');
However, for this to work in the browser you will need to use some sort of bundler (like browserify or webpack) which will combine both of the files into one.

Related

I had a problem when importing the module three.js

I wrote this thing in my HTML file
<script type="module" src="script.js"></script>
And this is the Javascript
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, 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.SphereGeometry(1, 100, 100);
const material = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.8, metalness: 1});
const ball = new THREE.Mesh(geometry, material);
scene.add(ball);
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(0, 10, 10);
scene.add(light);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
ball.rotation.x += 0.01;
ball.rotation.y += 0.01;
ball.rotation.z += 0.01;
renderer.render(scene, camera);
};
animate();
Also the error in the browser console is this
Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".
I tried many times reinstalling the module, even asking chatGPT for help but it also doesn't work. I BET NO ONE CAN FIX THIS
You are missing an import map in your code. Add the following above your <script> tag.
<script async src="https://unpkg.com/es-module-shims#1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three#0.148/build/three.module.js"
}
}
</script>
This will load three.js from a CDN (unpkg). However, you can replace the URL with a path to your locally hosted three.js version.

#rollup/plugin-babel cannot convert Spread operator correct when target is 'es5'

Rollup Version
rollup 2.78.0
#rollup/plugin-babel 5.3.1
Operating System (or Browser)
macOS 11.6.7
browser: chrome 107.0.5304.87
Node Version
14.18.0
rollup config
const plugins = [
nodeResolve({
preferBuiltins: false,
modulesOnly: true,
extensions: ['js', '.ts'],
}),
commonjs(),
// json(),
// globals(),
// builtins(),
typescript({
target: 'es5',
declaration: true,
declarationDir: 'dist',
}),
babel({
exclude: 'node_modules/**',
babelHelpers: 'runtime', // 开启体积优化
}),
// others
]
questions
I use Spread to convert a Set variable to Array variable, just like this:
const set = new Set();
const getSet = () => {
return [...set]
}
but I found that this function return [] all the time, so I check the build files, and found the Spread API is converted to Array.prototype.slice:
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
}
However, Array.prototype.slice cannot handle the Set, or in other word, Array.prototype.slice.call(Set) will return [] all the time, and it works if I set the target to es6.
expect
So, my questions is , how can it work when target is 'es5'? I think maybe I need add some configs, but I can't find a solution in document or other article

Cannot fide modules gulp

How can i fix this problem? Im usin VSCode and trying to install gulp in my project. What`s the problem ot this?
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:895:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
gulpfile.js
import { src, dest } from 'gulp';
import sass from 'gulp-sass';
import minifyCSS from 'gulp-csso';
function style() {
return src('./scss/**/*.scss')
.pipe(sass())
.pipe(minifyCSS())
.pipe(dest('./css'))
}
const _style = style;
export { _style as style };
terminal code: gulp _style
node --experimental-modules gulpfile.js and after it works
and change gulpfile.js to this type
const gulp = require('gulp');
const sass = require('gulp-sass');
const minifyCSS = require('gulp-csso');
function style() {
return gulp.src('./scss/**/*.scss')
.pipe(sass())
.pipe(minifyCSS())
.pipe(gulp.dest('./css'))
}
exports.style = style;

how to rewrite leaflet code to react leaflet

need help with rewrite code of pure js leaflet to react leaflet.
I want to see this Overlay in react-leaflet Overlay
I think i can do it with refs, but didnt find "getTileUrl" function :(
createLayer () {
const { map } = this.props.leaflet;
L.TileLayer.Rosreestr = L.TileLayer.extend({
options: {
tileSize: 1024,
},
getTileUrl: function (tilePoint) {
var map = this._map,
crs = map.options.crs,
tileSize = 1024,
nwPoint = tilePoint.multiplyBy(tileSize),
sePoint = nwPoint.add([tileSize, tileSize]);
var nw = crs.project(map.unproject(nwPoint, tilePoint.z)),
se = crs.project(map.unproject(sePoint, tilePoint.z)),
bbox = [nw.x, se.y, se.x, nw.y].join(',');
return L.Util.template(this._url, L.extend({
s: this._getSubdomain(tilePoint),
bbox: bbox
}));
}
});
L.tileLayer.rosreestr = function() {
return new L.TileLayer.Rosreestr("https://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/Cadastre/MapServer/export?layers=show&dpi=96&format=PNG32&bbox=${bbox}&size=1024%2C1024&transparent=true&f=image");
}
L.control.layers(null, {REESTR: L.tileLayer.rosreestr({},{ minZoom: 2, maxZoom: 18}) },{collapsed: false}).addTo(map);}
Want put code in THIS:
<Map ref={(ref) => { this.map = ref }} center={center} zoom={currentZoom} style={styleLeaf} maxZoom="18" >
<LayersControl position="topright" >
<BaseLayer checked name="Landscape">
<TileLayer
url="https://tile.thunderforest.com/landscape/{z}/{x}/{y}.png/>
</BaseLayer>
<BaseLayer name="OpenCycleMap">
<TileLayer
url="https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png"/>
</BaseLayer>
</LayersControl>
<Overlay checked name="REESTR" >
<TileLayer
ref={(ref) => { this.tile = ref }}
attribution="REESTR" url="https://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/Cadastre/MapServer/export?layers=show&dpi=96&format=PNG32&bbox=${bbox}&size=1024%2C1024&transparent=true&f=image">
</TileLayer>
</Overlay>

Google visualization api throws error when calling getImageURI in apps script

I want to get the PNG URL for a column chart, however when I call getImageURI function the next error is thrown: " Object [object Object] has no method 'getImageURI' ".
This error is only thrown within Apps Script, if I copy and paste my code in a HTML file works fine.
Here my code:
var data = google.visualization.arrayToDataTable([
['Element', 'Density', { role: 'style' }],
['Copper', 8.94, '#b87333', ],
['Silver', 10.49, 'silver'],
['Gold', 19.30, 'gold'],
['Platinum', 21.45, 'color: #e5e4e2' ],
]);
var options = {
title: "Density of Precious Metals, in g/cm^3",
bar: {groupWidth: '95%'},
legend: 'none',
};
var chart_div = document.getElementById('divGrafica');
var chart = new google.visualization.ColumnChart(chart_div);
// Wait for the chart to finish drawing before calling the getImageURI() method.
google.visualization.events.addListener(chart, 'ready', function () {
chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
console.log(chart_div.innerHTML);
});
chart.draw(data, options);
Is not implemented getImageURI in Apps Script?
The GViz API is not officially supported in HtmlService, and therefore various functions may not work as expected.