新聞中心
NEWS
當前位置: 首頁(yè) > 產(chǎn)品中心
canvas 繪圖時(shí)位置偏離的問(wèn)題解決
時(shí)間:2026-05-05 03:05:15這篇文章主要介紹了canvas 繪圖時(shí)位置偏離的繪圖問(wèn)題解決,文中通過(guò)示例代碼介紹的時(shí)位非常詳細,對大家的置??偏學(xué)習或者工作具有一定的參考學(xué)習價(jià)值,需要的問(wèn)題朋友們下面隨著(zhù)小編來(lái)一起??學(xué)習學(xué)習吧
使用 canvas 繪圖時(shí),指定的解決 div 大小一定不要超過(guò)該 div 所能獲得的最大范圍,否則繪制的繪圖點(diǎn)會(huì )跟實(shí)際位置發(fā)生偏離。
例如
<html>
<hea??d>
<meta ht(╯‵□′)╯t(????)p-equiv="Content-Type" content='text/html; charset=utf-8' />
<title>Untitled 1</title??>
<style type='text/css'>
.sty( ?° ?? ?°)le1 {
font-size: x-small;
}
</style>
</head>
<body >ヽ(′▽?zhuān)?ノ
<div style='margin:2%'>
<div id="test" style="width:800px;height(╬?益?):800px;background-color:#cbdad0d9">
<canvas id="container" onmousemove='ヾ(′▽?zhuān)??mousemove(event)' onmousedown='mousedown()' onmouseup=??"mouseup()"&g??t;</canvas>
</div>
</div>
<script type="text/javascript">
var paint = false;
var start = false;
var canvas = document.getElementById("container");
canvas.width = 800;
canvas.height = 800;
var offsetY = canvas.offsetTop;
var offsetX = canvas.offsetLeft;
var y;
var x;
var context = canvas.getContext("2d");
function mousemove(e) {
if (paint == true){
if (sta??rt == false){
context.moveTo(0,時(shí)位 0);
start = true;
} else {
context.mov(?Д?)eTo(x, y);
}
x = e.pageX - offsetX;
y = e.pageY - offsetY;
context.lin(╯°□°)╯︵ ┻━┻eTo(x, y);
context.stroke();
}
}
function mousedow(╯°□°)╯n(event) {
paint = true;
console.log("down")
}
function mouseup(event) {
paint = false;
console.log("up")
}
</script>
</body>
</html>
如果更改為:
<div style="margi(′▽?zhuān)?)n:20%">
<div id="tes??t" style="width:800px;h??eight:800px;background-c??olor:#cbdad0d9">
<canva(′▽?zhuān)?)s id="conta??iner" onmou??semove='mousemove(event)' onmou(╬ ò﹏ó)sedown='mousedown()' onmouseup="mouseup()"></canvas>
</div>
<??;/div>
由于canvas所需width?? 800px無(wú)法滿(mǎn)足,置偏因此??繪制線(xiàn)圖時(shí),問(wèn)題與實(shí)際鼠標位置發(fā)生偏離┐(′?`)┌。解決
到此這篇關(guān)于canvas 繪圖時(shí)位置偏離的繪圖問(wèn)題解決的文章就介紹到這了,更多相關(guān)canvas 繪圖位置偏離內容請搜索腳本之家以前的文章或繼續瀏覽下面的相(′_`)關(guān)文章,希望大家以后??多多??支持腳本之家!時(shí)位
置偏來(lái)源:腳本之家
置偏鏈接:https://www.jb51.net/html5/745325.html
置偏客服電話(huà)18928253011
Copyright ? 2012-2018 天津九安特機電工程有限公司 版權所有 備案號:
客服電話(huà)17794282821