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