1
我想知道我在做什么错了下面的代码。从中心抓取元素
SVG已经被放大和翻译,元素垂直拖拽很好,但是,它是疯狂的。
https://jsbin.com/gahoseneyu/edit?js,output
const svg = document.getElementsByTagName("svg")[ 0 ];
const w = svg.getAttribute("width");
const h = svg.getAttribute("height");
const cry = document.getElementById("cry");
const reg = /\d*\.\d[^\s]/g;
const viewBox = svg.getAttribute("viewBox").match(reg);
const wRatio = w/viewBox[ 2 ];
const hRatio = h/viewBox[ 3 ];
console.log("viewbox is...", viewBox, wRatio, hRatio);
cry.addEventListener("mousedown", function(){
this.addEventListener("mousemove", function(e){
let newX = (e.pageX/wRatio) + parseFloat(viewBox[ 0 ]) - 22,
newY = (e.pageY/hRatio) + parseFloat(viewBox[ 1 ]) - 22;
this.setAttribute("transform", `translate(${ newX },${ newY })`);
});
});
谢谢彼得。我回到审查scaleIn函数来修复方面的比例,然后希望它应该工作。精彩的代码有几个指针供我采用。再次感谢。 – Kayote