当前位置: 首页 > 工具软件 > Jedi-js > 使用案例 >

A WebGL context could not be created. Reason: Canvas has an existing context [uni-app使用Three.js报错]

魏朗
2023-12-01

uni-app 模版里创建的canvas经过加工默认获取了context2D 所以再使用webGL 获取上下文失败 所以报那个错误
解决方案就是使用js创建一个canvas元素 然后添加到页面中 再正常进行three的编写
或者直接向页面添加 renderer.domElement使用默认three生成的canvas元素

<template>
  <view class="content">

  </view>
</template>

<script>
export default {
  data() {
    return {
      title: "Hello",
    };
  },
  mounted() {
    const content = document.querySelector(".content");
    const canvas = document.createElement("canvas");
    content.appendChild(canvas);
    if (canvas) {
    //...Ï
	const renderer = new THREE.WebGLRenderer({canvas});
    }
 类似资料: