簡體   English   中英

HTML 5 Canvas-縮放時獲取真實坐標

[英]HTML 5 Canvas - Get real coordinate when zooming

我最終實現了使用此代碼作為基礎https://stackoverflow.com/a/3151987/5221943來實現可縮放和平移畫布。 現在,我需要檢測用戶在畫布內單擊的實際坐標。 考慮到用戶已經放大或平移了畫布,我不知道如何將鼠標頁面坐標轉換為現實世界坐標。 有什么建議嗎?

這是用於選通畫布坐標的代碼

 Call the DumpInfo function in the html canvas tag like <canvas id="canvas" width="600" height="200" onmousedown="DumpInfo(event)"></canvas> Bellow two functions defined <script type = "text/javascript" > function DumpInfo(event) { var info = document.getElementById("canvas"); //info.innerHTML += event.type + ", "; var pos = getMousePos(info, event); alert(pos.x); alert(pos.y); } function getMousePos(canvas, evt) { var rect = canvas.getBoundingClientRect(); return { x: evt.clientX - rect.left, y: evt.clientY - rect.top }; } </script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM