第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Canvas HTML5:在 mousemove 上顯示直角坐標(biāo)

Canvas HTML5:在 mousemove 上顯示直角坐標(biāo)

holdtom 2023-02-24 17:47:12
我目前正在使用HTML5 Canvas開(kāi)發(fā)一個(gè)圖像編輯器,當(dāng)鼠標(biāo)懸停在畫(huà)布上時(shí),我在檢測(cè)圖像坐標(biāo)時(shí)遇到了問(wèn)題。我在用矩形剪斷的代碼中復(fù)制了這個(gè)問(wèn)題:什么時(shí)候:紅色部分是畫(huà)布的邊框。畫(huà)布大小為400x400 像素。綠色部分是模擬圖像的矩形。矩形大小為200x200 像素。在 mousemove 事件中,顯示在畫(huà)布的底部坐標(biāo)。藍(lán)色文字,是我想要的(現(xiàn)在坐標(biāo)不是那些,是畫(huà)布的)我想要什么,但我不知道該怎么做:在 mousemove 上,我想顯示矩形的坐標(biāo)而不是畫(huà)布坐標(biāo)。即(0, 0)畫(huà)布的將變?yōu)樨?fù)值。(0, 0)底部顯示的應(yīng)該與藍(lán)色文字相對(duì)應(yīng)。更改 ZOOM 屬性后它也應(yīng)該工作。示例中的 ZOOM 為1,但更改為0.5或后1.5應(yīng)該以相同的方式工作。代碼在下面,我分享了代碼片段,看看是否有人可以幫助我,因?yàn)槲矣悬c(diǎn)沮喪,我確信這很愚蠢。多謝!const RECT_SIZE = 200const ZOOM = 1const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')const svgPoint = svg.createSVGPoint()const xform = svg.createSVGMatrix()const canvas = document.querySelector('canvas')const ctx = canvas.getContext('2d')const res = document.querySelector('.res')const pt = transformedPoint(canvas.width / 2, canvas.height / 2)const X = canvas.width / 2 - RECT_SIZE / 2const Y = canvas.height / 2 - RECT_SIZE / 2function transformedPoint(x, y) {  svgPoint.x = x  svgPoint.y = y  return svgPoint.matrixTransform(xform.inverse())}function mousemove(e) {  const { left, top } = canvas.getBoundingClientRect()  res.textContent = `X: ${e.clientX - left} - Y: ${e.clientY - top}`}// SCALING CANVASctx.translate(pt.x, pt.y)ctx.scale(ZOOM, ZOOM)ctx.translate(-pt.x, -pt.y)// SETTING SOME DEFAULTSctx.lineWidth = 1ctx.strokeStyle = 'green'ctx.fillStyle = 'blue'// DRAWING A REACTANGLEctx.beginPath()ctx.strokeRect(X, Y, RECT_SIZE,  RECT_SIZE)ctx.font = "12px Arial";ctx.fillText("0,0", X - 5, Y-10);ctx.fillText("200,200", X + RECT_SIZE - 15, Y + RECT_SIZE + 15);ctx.closePath()canvas {  border: 1px solid red;}<canvas width="400" height="400" onmousemove="mousemove(event)"}></canvas><div class="res" />更改行:res.textContent = `X: ${e.clientX - left} - Y: ${e.clientY - top}`到res.textContent = `X: ${e.clientX - left - X} - Y: ${e.clientY - top - Y}`時(shí)顯示正確的坐標(biāo)ZOOM = 1。但是,更改縮放后未顯示正確的坐標(biāo)。
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

您必須將 ZOOM 值添加到 X 和 Y 計(jì)算中,如下例所示。我使用 Math.round 方法作為最終的 X 和 Y 值是一個(gè)長(zhǎng)分?jǐn)?shù)。


const RECT_SIZE = 200

const ZOOM = 1.4

const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')

const svgPoint = svg.createSVGPoint()

const xform = svg.createSVGMatrix()

const canvas = document.querySelector('canvas')

const ctx = canvas.getContext('2d')

const res = document.querySelector('.res')

const pt = transformedPoint(canvas.width / 2, canvas.height / 2)

const X = canvas.width / 2 - RECT_SIZE / 2;

const Y = canvas.height / 2 - RECT_SIZE / 2;


canvas.addEventListener('mousemove',(event)=>{

  const {top, left} = event.target.getBoundingClientRect();

  const zoomX = Math.round((event.clientX - left - (canvas.width / 2 - ((RECT_SIZE * ZOOM) / 2)))/ZOOM);

  const zoomY = Math.round((event.clientY - top - (canvas.height / 2 - ((RECT_SIZE * ZOOM) / 2)))/ZOOM);

  res.textContent = `X: ${zoomX} - Y: ${zoomY}`;


});


function transformedPoint(x, y) {

  svgPoint.x = x

  svgPoint.y = y

  return svgPoint.matrixTransform(xform.inverse())

}



// SCALING CANVAS

ctx.translate(pt.x, pt.y)

ctx.scale(ZOOM, ZOOM)

ctx.translate(-pt.x, -pt.y)


// SETTING SOME DEFAULTS

ctx.lineWidth = 1

ctx.strokeStyle = 'green'

ctx.fillStyle = 'blue'


// DRAWING A REACTANGLE

ctx.beginPath()

ctx.strokeRect(X, Y, RECT_SIZE,  RECT_SIZE)


ctx.font = "12px Arial";

ctx.fillText("0,0", X - 5, Y-10);

ctx.fillText("200,200", X + RECT_SIZE - 15, Y + RECT_SIZE + 15);


ctx.closePath()

canvas {

  border: 1px solid red;

}

<canvas width="400" height="400"}></canvas>

<div class="res" />


查看完整回答
反對(duì) 回復(fù) 2023-02-24
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)