D3.js中的zoom.transform()函數用於將所選元素的當前縮放變換設置為指定的變換。
用法:
zoom.transform(selection, transform[, point]);
參數:該函數接受上述和以下描述的單個參數:
- selection:該參數可以是選擇或轉換。
- transform:該參數可以定義為縮放變換或函數。
返回值:此函數返回縮放變換。
以下示例程序旨在說明D3.js中的zoom.transform()函數
範例1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js">
</script>
<style>
svg text {
fill:green;
font:20px sans-serif;
text-anchor:center;
}
rect {
pointer-events:all;
}
</style>
</head>
<body>
<center>
<h1 style="color:green;">
Geeksforgeeks
</h1>
<h3>D3.js | zoom.transform() Function</h3>
<button id="reset">Reset</button><br/>
<svg></svg>
<script>
var width = 400;
var height = 200;
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
// The scale used to display the axis.
var scale = d3.scaleLinear()
.range([10, width-20])
.domain([0, 100]);
var shadowScale = scale.copy();
var axis = d3.axisBottom()
.scale(scale);
var g = svg.append("g")
.attr("transform", "translate(0, 50)")
.call(axis);
// Standard zoom behavior:
var zoom = d3.zoom()
.scaleExtent([1, 10])
.translateExtent([[0, 0], [width, height]])
.on("zoom", zoomed);
// Call the Zoom.
var rect = svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "none")
.call(zoom);
d3.select("#reset")
.on("click", function() {
// Create an identity transform
var transform = d3.zoomIdentity;
// Apply the transform:
rect.call(zoom.transform, transform);
})
function zoomed() {
var t = d3.event.transform;
scale.domain(t.rescaleX(shadowScale).domain());
g.call(axis);
}
</script>
</center>
</body>
</html>
輸出:
範例2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js">
</script>
<style>
rect {
cursor:pointer;
}
</style>
</head>
<body>
<center>
<h1 style="color:green;">
Geeksforgeeks
</h1>
<h3>D3.js | zoom.transform() Function</h3>
<button>Trigger Zoom</button> <br/>
<span> Zoom Level:</span>
<span id="GFG"></span><br/>
<svg></svg>
<script>
var scale = d3.scaleSqrt()
.range(["green", "purple", "blue"])
.domain([1, 40, 1600]);
var zoom = d3.zoom()
.on("zoom", zoomed)
.scaleExtent([1, 1600])
var rect = d3.select("svg")
.append("rect")
.attr("width", 400)
.attr("height", 300)
.attr("fill", "green")
.call(zoom);
rect.call(zoom.transform, d3.zoomIdentity);
d3.select("button").on("click", function() {
var newTransform = d3.zoomIdentity.scale(100);
rect.call(zoom.transform, newTransform);
})
// Zoom function:
function zoomed(){
var k = d3.event.transform.k;
rect.attr("fill", scale(k));
d3.select("#GFG").text(k);
}
</script>
</center>
</body>
</html>
輸出:
相關用法
- d3.js zoomTransform()用法及代碼示例
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 D3.js zoom.transform() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。