導航:首頁 > 編程語言 > d3js碰撞檢測

d3js碰撞檢測

發布時間:2021-12-08 11:03:34

① d3js做的圖表怎麼放在html中的指定位置啊

定義div的id,比如為id1,定義svg時用d3.select("#id1")而不是d3.select("body")

② 如何在Three.js中檢測兩個物體是否了發生碰撞

使用Raycaster進行碰撞檢測
用Raycaster來檢測碰撞的原理很簡單,我們需要以物體的中心為起點,向各個頂點(vertices)發出射線,然後檢查射線是否與其它的物體相交。如果出現了相交的情況,檢查最近的一個交點與射線起點間的距離,如果這個距離比射線起點至物體頂點間的距離要小,則說明發生了碰撞。
這個方法有一個 缺點 ,當物體的中心在另一個物體內部時,是不能夠檢測到碰撞的。而且當兩個物體能夠互相穿過,且有較大部分重合時,檢測效果也不理想。
還有需要 注意 的一點是:在Three.js中創建物體時,它的頂點(veritces)數目是與它的分段數目相關的,分段越多,頂點數目越多。為了檢測過程中的准確度考慮,需要適當增加物體的分段。

③ 如何使用d3.js製作可視化圖表

D3是目前最流行的javaScript可視化圖表庫之一,D3的圖表類型非常豐富,並且支持SVG格式,因此應用十分廣泛,也有很多圖表插件基於D3開發,比如MetricsGraphics.js,在D3上構建的數據圖表非常強大。


D3的特點

允許綁定任意數據到DOM,將數據驅動轉換應用到Document中。

不僅可以創建精美的HTML表格,而且可以繪制折線圖、柱形圖和餅圖等數據圖表。

支持SVG,在Web頁面上渲染毫無壓力。

回到頂部

D3的使用方法

關於D3的具體用法,可以看D3圖形庫API參考這篇文章。本文主要對介紹一些經典圖表的實現效果及代碼



<!DOCTYPEhtml>
<metacharset="utf-8">
<style>

svg{
font:10pxsans-serif;
}

.y.axispath{
display:none;
}

.y.axisline{
stroke:#fff;
stroke-opacity:.2;
shape-rendering:crispEdges;
}

.y.axis.zeroline{
stroke:#000;
stroke-opacity:1;
}

.title{
font:30078pxHelveticaNeue;
fill:#666;
}

.birthyear,
.age{
text-anchor:middle;
}

.birthyear{
fill:#fff;
}

rect{
fill-opacity:.6;
fill:#e377c2;
}

rect:first-child{
fill:#1f77b4;
}

</style>
<body>
<scriptsrc="http://d3js.org/d3.v3.min.js"></script>
<script>

varmargin={top:20,right:40,bottom:30,left:20},
width=960-margin.left-margin.right,
height=500-margin.top-margin.bottom,
barWidth=Math.floor(width/19)-1;

varx=d3.scale.linear()
.range([barWidth/2,width-barWidth/2]);

vary=d3.scale.linear()
.range([height,0]);

varyAxis=d3.svg.axis()
.scale(y)
.orient("right")
.tickSize(-width)
.tickFormat(function(d){returnMath.round(d/1e6)+"M";});

//AnSVGelementwithabottom-rightorigin.
varsvg=d3.select("body").append("svg")
.attr("width",width+margin.left+margin.right)
.attr("height",height+margin.top+margin.bottom)
.append("g")
.attr("transform","translate("+margin.left+","+margin.top+")");

//.
varbirthyears=svg.append("g")
.attr("class","birthyears");

//Alabelforthecurrentyear.
vartitle=svg.append("text")
.attr("class","title")
.attr("dy",".71em")
.text(2000);

d3.csv("population.csv",function(error,data){

//Convertstringstonumbers.
data.forEach(function(d){
d.people=+d.people;
d.year=+d.year;
d.age=+d.age;
});

//.
varage1=d3.max(data,function(d){returnd.age;}),
year0=d3.min(data,function(d){returnd.year;}),
year1=d3.max(data,function(d){returnd.year;}),
year=year1;

//Updatethescaledomains.
x.domain([year1-age1,year1]);
y.domain([0,d3.max(data,function(d){returnd.people;})]);

//[male,female].
data=d3.nest()
.key(function(d){returnd.year;})
.key(function(d){returnd.year-d.age;})
.rollup(function(v){returnv.map(function(d){returnd.people;});})
.map(data);

//.
svg.append("g")
.attr("class","yaxis")
.attr("transform","translate("+width+",0)")
.call(yAxis)
.selectAll("g")
.filter(function(value){return!value;})
.classed("zero",true);

//(sothatnoenterorexitisrequired).
varbirthyear=birthyears.selectAll(".birthyear")
.data(d3.range(year0-age1,year1+1,5))
.enter().append("g")
.attr("class","birthyear")
.attr("transform",function(birthyear){return"translate("+x(birthyear)+",0)";});

birthyear.selectAll("rect")
.data(function(birthyear){returndata[year][birthyear]||[0,0];})
.enter().append("rect")
.attr("x",-barWidth/2)
.attr("width",barWidth)
.attr("y",y)
.attr("height",function(value){returnheight-y(value);});

//Addlabelstoshowbirthyear.
birthyear.append("text")
.attr("y",height-4)
.text(function(birthyear){returnbirthyear;});

//Addlabelstoshowage(separate;notanimated).
svg.selectAll(".age")
.data(d3.range(0,age1+1,5))
.enter().append("text")
.attr("class","age")
.attr("x",function(age){returnx(year-age);})
.attr("y",height+4)
.attr("dy",".71em")
.text(function(age){returnage;});

//.
window.focus();
d3.select(window).on("keydown",function(){
switch(d3.event.keyCode){
case37:year=Math.max(year0,year-10);break;
case39:year=Math.min(year1,year+10);break;
}
update();
});

functionupdate(){
if(!(yearindata))return;
title.text(year);

birthyears.transition()
.ration(750)
.attr("transform","translate("+(x(year1)-x(year))+",0)");

birthyear.selectAll("rect")
.data(function(birthyear){returndata[year][birthyear]||[0,0];})
.transition()
.ration(750)
.attr("y",y)
.attr("height",function(value){returnheight-y(value);});
}
});

④ d3js的tree結構圖例怎麼做

如下過程:
使用d3.js
初始化d3和畫布大小,tree = d3.layout.cluster().size([h, w])
導入數據內,使用d3默認處理數據:容 root = tree.nodes(data)
處理數據(包括坐標的處理)
展示數據
思路是這樣的。

⑤ d3js和threejs能不能結合

D3.js是一個數據可視化的庫,看看他們的DEMO就可以知道,技術基礎是SVG。兼容性是IE9+。webgl是HTML5中提出的新技術,是一種3D繪圖標准,這種繪圖技術標准允許把JavaScript和OpenGLES2.0結合在一起,關於它的教程可以看看hiwebgl。目前兼容性

⑥ d3js怎麼配合react使用

一般js載入的元素放置在頁面中的位置,有兩種方法 1.一種是放在相應的容器裡面,利用js動態載入進來,但是元素的位置是固定了的,就是在頁面中容器原來放置在哪裡,就只能放置在哪裡

⑦ c#winform中能做出類似於d3js的效果的動態風向圖嗎

可以http://blog.csdn.net/xianfajushi/article/details/9469353

⑧ JS 檢測碰撞的方法有哪些

本文實例講述了JS實現判斷碰撞的方法。分享給大家供大家參考。具體如下:
JS判斷碰撞方法:

復制代碼代碼如下:
/** 判斷是否碰撞
* @param obj 原對象
* @param dobj 目標對象
*/
function impact(obj, dobj) {
var o = {
x: getDefaultStyle(obj, 'left'),
y: getDefaultStyle(obj, 'top'),
w: getDefaultStyle(obj, 'width'),
h: getDefaultStyle(obj, 'height')
}

var d = {
x: getDefaultStyle(dobj, 'left'),
y: getDefaultStyle(dobj, 'top'),
w: getDefaultStyle(dobj, 'width'),
h: getDefaultStyle(dobj, 'height')
}

var px, py;

px = o.x <= d.x ? d.x : o.x;
py = o.y <= d.y ? d.y : o.y;

// 判斷點是否都在兩個對象中
if (px >= o.x && px <= o.x + o.w && py >= o.y && py <= o.y + o.h && px >= d.x && px <= d.x + d.w && py >= d.y && py <= d.y + d.h) {
return true;
} else {
return false;
}
}

/** 獲取對象屬性
* @param obj 對象
* @param attribute 屬性
*/
function getDefaultStyle(obj, attribute) {
return parseInt(obj.currentStyle ? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj, false)[attribute]);
}
示例如下:

復制代碼代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> demo </title>
<style type="text/css">
body{margin:0px;}
.main{position:relative;}
#f1{position:absolute; background:#FF0000; top:100px; left:100px; width:200px; height:200px; z-index:999}
#f2{position:absolute; background:#FFFF00; top:0px; left:0px; width:600px; height:150px;}
</style>
</head>
<body>
<div class="main">
<div id="f1"></div>
<div id="f2"></div>
</div>
<script type="text/javascript">
var o = document.getElementById("f1");
var d = document.getElementById("f2");
alert(impact(o, d));
function impact(obj, dobj) {
var o = {
x: getDefaultStyle(obj, 'left'),
y: getDefaultStyle(obj, 'top'),
w: getDefaultStyle(obj, 'width'),
h: getDefaultStyle(obj, 'height')
}
var d = {
x: getDefaultStyle(dobj, 'left'),
y: getDefaultStyle(dobj, 'top'),
w: getDefaultStyle(dobj, 'width'),
h: getDefaultStyle(dobj, 'height')
}
var px, py;
px = o.x <= d.x ? d.x : o.x;
py = o.y <= d.y ? d.y : o.y;

// 判斷點是否都在兩個對象中
if (px >= o.x && px <= o.x + o.w && py >= o.y && py <= o.y + o.h && px >= d.x && px <= d.x + d.w && py >= d.y && py <= d.y + d.h) {
return true;
} else {
return false;
}
}
function getDefaultStyle(obj, attribute) {
return parseInt(obj.currentStyle ? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj, false)[attribute]);
}
</script>
</body>
</html>
希望本文所述對大家的javascript程序設計有所幫助。

⑨ d3js做的圖標怎麼放在html中的指定位置

1、利用抄js代碼首先創建一個襲div,document.createElement('div'); 2、確認div添加位置,可以在某個dom元素後面,或者通過css屬性控制具體位置,主要通過left/top等屬性控制。 3、確定位置之後,顯示div即可。 示例:比如html中有一個文本輸入框,

閱讀全文

與d3js碰撞檢測相關的資料

熱點內容
js獲取標簽內容 瀏覽:519
潘多拉文件是什麼意思 瀏覽:636
word修改批註時間 瀏覽:813
沒有軟肋鎧甲升級 瀏覽:835
教師職稱解聘文件格式 瀏覽:997
c讀取dwg文件 瀏覽:120
最大的大數據中心 瀏覽:881
wordf1快捷鍵 瀏覽:78
編程中的2c是什麼意思 瀏覽:747
在大網站上如何處理知識產權 瀏覽:51
彩色文件夾軟體 瀏覽:522
學編程的電腦軟體有哪些 瀏覽:894
部署javaweb服務 瀏覽:767
手機刪不掉空文件夾 瀏覽:691
excel伺服器連接資料庫sql的問題 瀏覽:576
女孩微信頭像 瀏覽:176
西安交黨費有哪些APP 瀏覽:967
中國加工貿易大數據 瀏覽:68
怎麼設置蜂窩數據 瀏覽:668
單機唱歌app哪個好 瀏覽:693

友情鏈接