首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在mhtml5 svg中绘制球体

在mhtml5 svg中绘制球体
EN

Stack Overflow用户
提问于 2015-10-31 04:43:00
回答 1查看 1.3K关注 0票数 3

我想在html5中画一个气泡(实际上有很多气泡,但我们先用一个),我想使用svg,因为我想缩放气泡,而不需要对它们进行纹理处理,所以svg是更好的选择。

但是我没有找到任何关于在svg中用纯html5和javascript做球体的东西,我发现主要是画布像this these或webgl像this,这两个都是基于光栅的,基于矢量的对我来说更好。

有没有办法做到这一点?或者我被困在用圆圈代替?

EN

回答 1

Stack Overflow用户

发布于 2016-08-03 21:38:06

不幸的是,这有点晚了,但我希望它能帮助那些正在寻找这样的东西的人。你不会找到一个独立的解决方案(或者你必须编写自己的javascript实现来处理创建球体所需的所有数学运算),但你可以使用D3.js库和.geo (几何)扩展(默认情况下随D3.js一起提供),一个很好的例子是这样的:

代码语言:javascript
复制
var width = 500, height = width, 
    projection, path,
    svg, features, graticule;

projection = d3.geo.orthographic()
    .translate([width / 2, height / 2])
    .scale(250)
    .clipAngle(90)
    .precision(0.1)
    .rotate([0, -30]);

path = d3.geo.path()
    .projection(projection);

svg = d3.select('.globe')
    .append('svg')
    .attr('width', width)
    .attr('height', height)
    .attr('viewBox', '0, 0, ' + width + ', ' + height);

features = svg.append('g');

features.append('path')
    .datum({type: 'Sphere'})
    .attr('class', 'background')
    .attr('d', path);

graticule = d3.geo.graticule();

features.append('path')
  .datum(graticule)
  .attr('class', 'graticule')
  .attr('d', path);
代码语言:javascript
复制
svg {
    width: 100%
}

path {
    fill: none;
    stroke: black
}

.background {
  fill: rgba(200,212,220,.5);
  stroke-width: .8px;
  stroke: black;
}

.graticule {
    stroke: rgba(0,0,0, .2);
    stroke-width: .5px;
}

.country {
    cursor: pointer;
}

.country .land, .state .land {
    fill: white;
    stroke: rgba(0,0,0, .2);
    stroke-width .3px;
}

.state .overlay {
    fill: blue;
    fill-opacity: 0;
}

.country .overlay {
    fill: orange;
    fill-opacity: 0;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<div class="globe"></div>

我猜在下面的教程中,你会发现这个例子的版本2是你工作的最佳选择:

http://jsfiddle.net/GordyD/uwp5vu87/2/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33444603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档