【发布时间】:2020-11-18 21:17:38
【问题描述】:
在 pc chrome 上它工作正常,在 chrome 移动模拟器(或 android chroem)上,箭头跳过圆圈。
d3.pointers(event)[0] 返回的 x 和 y 对于 y 轴不是线性的 =>
156、100、157、101、158、102。
drawCompass({
radius: 120
})
function drawCompass(data) {
const compasColor = '#ccc'
// const { x, y } = map.latLngToLayerPoint([data.lat, data.long])
const x = y = 190;
const r = data.radius
svg = d3.select("svg")
svg
.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", r)
.style("fill", "red")
.attr("stroke", compasColor)
.attr("stroke-width", 3)
.attr("fill-opacity", .0)
drawArrow("steelblue", 0, "0", "");
function drawArrow(color, angle, text_inner, text_outer) {
const outer_id = "outer_" + color + Date.now(); //id for line text
const inner_id = "inner"
const arrowGroup = svg.append("g").attr("class", "arrowGroup");
const offset = 30;
//line radius
arrowGroup
.append("path")
.attr("id", inner_id) //normall
.attr("class", "compassLines")
.attr("d", d3.line()([
[x, y - r],
[x, y],
]))
.attr("stroke", compasColor)
.attr("stroke-width", 1)
.attr("fill", "none");
// line and background for text outside the compass circle
const dragger = arrowGroup
.append("path")
.attr("id", outer_id) //normall
.style("stroke", "black")
.attr("stroke-width", 20)
.attr("stroke-opacity", 0.5)
.attr("fill-opacity", 0.7)
.style("stroke-linecap", "round")
.attr("d", d3.line()([
[x, y - r],
[x, y - r - 30]
]))
.call(
d3.drag().on("drag", dragged)
)
function dragged(event) {
const arrow_position = d3.pointers(event)[0];
console.log('arrow_position', arrow_position);
var angle123 = geometric.lineAngle([
[x, y], arrow_position
]);
const tempAngle = Math.ceil(angle123 + 90)
const displayAngel = tempAngle < 0 ? 360 + tempAngle : tempAngle
const arrow = this.parentNode;
d3.select(arrow)
.attr('transform', `rotate(${angle123 + 90} ${x} ${y})`);
}
return arrowGroup
}
}
<!DOCTYPE html>
<html>
<head>
<style>
.compassText {
text-align: center;
font-size: 30px;
text-anchor: middle;
}
</style>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://unpkg.com/geometric@1.0.0/build/geometric.min.js"></script>
</head>
<body>
<svg width="380" height="380"></svg>
【问题讨论】:
标签: javascript android google-chrome d3.js