国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 用 canvas 實現(xiàn)電子簽名,定位鼠標在 canvas 中 坐標

瀏覽:93日期:2022-12-23 18:34:02

問題描述

嘗試用 canvas 實現(xiàn)電子簽名時,不能準確獲取鼠標在 canvas 中的坐標。

let canvas = document.getElementById('canvas');let cxt = canvas.getContext(’2d’);canvas.onmousedown = function(ev){ var ev = ev || window.event; cxt.moveTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop); document.onmousemove = function(ev){ var ev = ev || window.event; cxt.lineTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop); cxt.stroke(); }; document.onmouseup = function(){ document.onmousemove = null; document.onmouseup = null; };};

用 ev.clientY 獲取了鼠標的坐標,但是 canvas.offsetTop 獲取的是 距離父元素的高度。而 canvas 在一個有滾動條的表單中,所以無法準確定位。

謝謝!

問題解答

回答1:

已經(jīng)找到解決方法了。直接調(diào)用 canvas.getBoundingClientRect() 可以獲取到 canvas 相對于視窗到位置。

標簽: JavaScript
主站蜘蛛池模板: 乐业县| 阳原县| 香河县| 营口市| 阳高县| 浑源县| 太仓市| 南通市| 沽源县| 甘孜| 山东| 汝州市| 普兰店市| 安宁市| 清流县| 浮梁县| 隆昌县| 龙井市| 石屏县| 定兴县| 彰武县| 兰考县| 清流县| 咸宁市| 清流县| 长白| 定襄县| 尼玛县| 綦江县| 芦山县| 平昌县| 巍山| 皮山县| 武冈市| 平阳县| 邵阳县| 佛坪县| 乌拉特前旗| 邯郸县| 邯郸市| 金平|