运行 没有服务器要求的 CesiumJS

Run CesiumJS with no server requirements

我下载了 Cesiumjs-1.11 和 运行 官方的 Hello World 教程。

我想 运行 相同的示例而不依赖 nodejs 作为服务器。我尝试使用 ./Build/Cesium 并将其与同一目录中的示例一起使用。

CesiumJS 运行s,但我得到这个错误:

SecurityError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at file:://path/to/Cesium/Assets/Textures/moonSmall.jpg may not be loaded.
Error: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at file:://path/to/Cesium/Assets/Textures/moonSmall.jpg may not be loaded.
    at Error (native)
    at new p (file:://path/to/Cesium/Cesium.js:433:19773)
    at et.createTexture2D (file:://path/to/Cesium/Cesium.js:449:19216)
    at H.update (file:://path/to/Cesium/Cesium.js:434:9600)
    at S.update (file:://path/to/Cesium/Cesium.js:452:1298)
    at m.update (file:://path/to/Cesium/Cesium.js:455:27828)
    at vt (file:://path/to/Cesium/Cesium.js:458:15322)
    at Ct (file:://path/to/Cesium/Cesium.js:458:18817)
    at bt.render (file://path/to/Cesium/Cesium.js:458:25057)
    at P.render (file:://path/to/Cesium/Cesium.js:464:4108)

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Use correct character set. -->
  <meta charset="utf-8">
  <!-- Tell IE to use the latest, best version (or Chrome Frame if pre-IE11). -->
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  <title>Hello World!</title>
  <script src="Cesium/Cesium.js"></script>
  <style>
      @import url(Cesium/Widgets/widgets.css);
      html, body, #cesiumContainer {
          width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
      }
  </style>
</head>
<body>
  <div id="cesiumContainer"></div>
  <script>
    var viewer = new Cesium.Viewer('cesiumContainer');
  </script>
</body>
</html>

这段代码有什么问题?

您的代码在技术上没有任何问题,Cesium 可以离线工作嵌入到应用程序中而不会出现任何问题。这里的问题是网络浏览器的安全性。如果您只是在浏览器中打开 HTML 文件,您仍然 运行 处于限制访问本地文件的沙箱中,尤其是在涉及网络工作者和 WebGL 等内容时。这会阻止加载所需的文件,从而导致错误。这在所有浏览器中都是如此,您可以通过暂时禁用浏览器安全性来自行验证。

例如,在 Chrome 中,您可以 运行 和 --disable-web-security 页面加载时不会出现任何错误。请注意,为了使其正常工作,您必须确保没有 Chrome 实例已经 运行ning。如果您在启动时收到警告,您就知道选择了该选项。在其他浏览器中有不同的选项(例如 IE 只是提示您允许阻止的内容)。

您仍然会遇到缺少图像的问题,因为服务器上的默认配置应该是 运行。要解决此问题,您可以显式定义图像提供程序。这是一个在禁用网络安全时有效的完整示例。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1,
        maximum-scale=1, minimum-scale=1, user-scalable=no">
  <title>Hello World!</title>
  <script src="../Build/Cesium/Cesium.js"></script>
  <style>
      @import url(../Build/Cesium/Widgets/widgets.css);
      html, body, #cesiumContainer {
          width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
      }
  </style>
</head>
<body>
  <div id="cesiumContainer"></div>
  <script>
    var viewer = new Cesium.Viewer('cesiumContainer', {
        baseLayerPicker: false,
        imageryProvider: new Cesium.BingMapsImageryProvider({
                    url : 'http://dev.virtualearth.net'
                })
    });
  </script>
</body>
</html>

我假设您的最终目标是尝试在应用程序中嵌入 Cesium?如果是这样,将应用 none 这些安全限制。许多人在嵌入式浏览器控件中成功使用 Cesium。