波浪js背景颜色

Posted

技术标签:

【中文标题】波浪js背景颜色【英文标题】:wave js background color 【发布时间】:2019-06-25 18:26:59 【问题描述】:

我正在使用three.js 制作波浪画布动画,但我不知道如何删除画布的背景颜色(仅背景颜色)。

if (WEBGL.isWebGLAvailable() === false) 
  document.body.appendChild(WEBGL.getWebGLErrorMessage());


var SEPARATION = 100,
  AMOUNTX = 50,
  AMOUNTY = 50;

var container, stats;
var camera, scene, renderer;

var particles,
  count = 0;

var mouseX = 0,
  mouseY = -450;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();

function init() 
  container = document.createElement("div");
  document.body.appendChild(container);

  camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    1,
    10000,
  );
  camera.position.z = 1000;

  scene = new THREE.Scene();

  var numParticles = AMOUNTX * AMOUNTY;

  var positions = new Float32Array(numParticles * 3);
  var scales = new Float32Array(numParticles);

  var i = 0,
    j = 0;

  for (var ix = 0; ix < AMOUNTX; ix++) 
    for (var iy = 0; iy < AMOUNTY; iy++) 
      positions[i] = ix * SEPARATION - (AMOUNTX * SEPARATION) / 2; // x
      positions[i + 1] = 0; // y
      positions[i + 2] = iy * SEPARATION - (AMOUNTY * SEPARATION) / 2; // z

      scales[j] = 1;

      i += 3;
      j++;
    
  

  var geometry = new THREE.BufferGeometry();
  geometry.addAttribute("position", new THREE.BufferAttribute(positions, 3));
  geometry.addAttribute("scale", new THREE.BufferAttribute(scales, 1));

  var material = new THREE.ShaderMaterial(
    uniforms: 
      color:  value: new THREE.Color(0xffffff) ,
    ,
    vertexShader: document.getElementById("vertexshader").textContent,
    fragmentShader: document.getElementById("fragmentshader").textContent,
  );

  particles = new THREE.Points(geometry, material);
  scene.add(particles);

  renderer = new THREE.WebGLRenderer( antialias: true );
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.domElement.id = "outerTest";
  container.appendChild(renderer.domElement);

  document.addEventListener("mousemove", onDocumentMouseMove, false);
  document.addEventListener("touchstart", onDocumentTouchStart, false);
  document.addEventListener("touchmove", onDocumentTouchMove, false);

  window.addEventListener("resize", onWindowResize, false);


function onWindowResize() 
  windowHalfX = window.innerWidth / 2;
  windowHalfY = window.innerHeight / 2;

  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize(window.innerWidth, window.innerHeight);


//

function onDocumentMouseMove(event) 
  mouseX = event.clientX - windowHalfX;
  mouseY = event.clientY - windowHalfY;


function onDocumentTouchStart(event) 
  if (event.touches.length === 1) 
    event.preventDefault();

    mouseX = event.touches[0].pageX - windowHalfX;
    mouseY = event.touches[0].pageY - windowHalfY;
  


function onDocumentTouchMove(event) 
  if (event.touches.length === 1) 
    event.preventDefault();

    mouseX = event.touches[0].pageX - windowHalfX;
    mouseY = event.touches[0].pageY - windowHalfY;
  


//

function animate() 
  requestAnimationFrame(animate);

  render();
  //stats.update();


function render() 
  camera.position.x += (mouseX - camera.position.x) * 0.05;
  camera.position.y += (-mouseY - camera.position.y) * 0.05;
  camera.lookAt(scene.position);

  var positions = particles.geometry.attributes.position.array;
  var scales = particles.geometry.attributes.scale.array;

  var i = 0,
    j = 0;

  for (var ix = 0; ix < AMOUNTX; ix++) 
    for (var iy = 0; iy < AMOUNTY; iy++) 
      positions[i + 1] =
        Math.sin((ix + count) * 0.3) * 50 + Math.sin((iy + count) * 0.5) * 50;

      scales[j] =
        (Math.sin((ix + count) * 0.3) + 1) * 8 +
        (Math.sin((iy + count) * 0.5) + 1) * 8;

      i += 3;
      j++;
    
  

  particles.geometry.attributes.position.needsUpdate = true;
  particles.geometry.attributes.scale.needsUpdate = true;

  renderer.render(scene, camera);

  count += 0.1;

请忽略:要真正用three.js显示任何东西,我们需要三个东西:场景、相机和渲染器,这样我们就可以用相机渲染场景。

【问题讨论】:

请编辑您的问题。您的代码不可读。 最好使用SO代码sn-ps。 【参考方案1】:

在您的 webglrenderer 构造函数中传递 alpha:true、preserveDrawingBuffer:true

确保您的画布元素没有设置 style.background。即确保将其设置为 ''

Transparent background with three.js

【讨论】:

以上是关于波浪js背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

chart.js 雷达图怎么修改背景颜色

求助js操作多ul li背景颜色问题

js中.style.background无法改变背景颜色

如何用js更改tr的背景颜色 新手求助

如何用JS随机背景颜色?

html中点击按钮怎样改变div背景颜色